more portable solution of saving in a temp file
This commit is contained in:
parent
907973a041
commit
46d00b4c79
1 changed files with 5 additions and 9 deletions
|
|
@ -18,7 +18,6 @@
|
|||
|
||||
from __future__ import with_statement
|
||||
|
||||
import sys
|
||||
import os
|
||||
import tempfile
|
||||
import logging
|
||||
|
|
@ -50,14 +49,11 @@ class YamlConfig(IConfig):
|
|||
self.values = {}
|
||||
|
||||
def save(self):
|
||||
if sys.version_info[:2] <= (2, 5):
|
||||
with open(self.path, 'w') as f:
|
||||
yaml.dump(self.values, f)
|
||||
else:
|
||||
# write in a temporary file to avoid corruption problems
|
||||
with tempfile.NamedTemporaryFile(delete=False) as f:
|
||||
yaml.dump(self.values, f)
|
||||
os.rename(f.name, self.path)
|
||||
# write in a temporary file to avoid corruption problems
|
||||
fd, path = tempfile.mkstemp(dir=os.path.dirname(self.path))
|
||||
with os.fdopen(fd, 'w') as f:
|
||||
yaml.dump(self.values, f)
|
||||
os.rename(path, self.path)
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
default = None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue