Simplfy writing to config file

- use `with` statement and open() instead of os.open()
- execute run() on dialog, otherwise sys.exit() is called immediatly
This commit is contained in:
Philipp Hörist 2018-02-12 23:39:50 +01:00
parent fb4b262477
commit 877b754ef3
2 changed files with 7 additions and 12 deletions

View File

@ -111,24 +111,18 @@ class OptionsParser:
(base_dir, filename) = os.path.split(self.__filename)
self.__tempfile = os.path.join(base_dir, '.' + filename)
try:
f = os.fdopen(os.open(self.__tempfile,
os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0o600), 'w', encoding='utf-8')
with open(self.__tempfile, 'w', encoding='utf-8') as f:
app.config.foreach(self.write_line, f)
except IOError as e:
return str(e)
try:
app.config.foreach(self.write_line, f)
except IOError as e:
return str(e)
f.flush()
os.fsync(f.fileno())
f.close()
if os.path.exists(self.__filename):
if os.name == 'nt':
# win32 needs this
try:
os.remove(self.__filename)
except Exception:
pass
except Exception as e:
return str(e)
try:
os.rename(self.__tempfile, self.__filename)
except IOError as e:

View File

@ -2408,8 +2408,9 @@ class Interface:
print(err_str, file=sys.stderr)
# it is good to notify the user
# in case he or she cannot see the output of the console
dialogs.ErrorDialog(_('Could not save your settings and '
error_dialog = dialogs.ErrorDialog(_('Could not save your settings and '
'preferences'), err_str)
error_dialog.run()
sys.exit()
@staticmethod