Commit 877b754e authored by Philipp Hörist's avatar Philipp Hörist

Simplfy writing to config file

- use `with` statement and open() instead of os.open()
- execute run() on dialog, otherwise sys.exit() is called immediatly
parent fb4b2624
Pipeline #959 passed with stages
in 2 minutes and 4 seconds
......@@ -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:
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment