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:
parent
fb4b262477
commit
877b754ef3
|
@ -111,24 +111,18 @@ class OptionsParser:
|
||||||
(base_dir, filename) = os.path.split(self.__filename)
|
(base_dir, filename) = os.path.split(self.__filename)
|
||||||
self.__tempfile = os.path.join(base_dir, '.' + filename)
|
self.__tempfile = os.path.join(base_dir, '.' + filename)
|
||||||
try:
|
try:
|
||||||
f = os.fdopen(os.open(self.__tempfile,
|
with open(self.__tempfile, 'w', encoding='utf-8') as f:
|
||||||
os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0o600), 'w', encoding='utf-8')
|
app.config.foreach(self.write_line, f)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
return str(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.path.exists(self.__filename):
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
# win32 needs this
|
# win32 needs this
|
||||||
try:
|
try:
|
||||||
os.remove(self.__filename)
|
os.remove(self.__filename)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
pass
|
return str(e)
|
||||||
try:
|
try:
|
||||||
os.rename(self.__tempfile, self.__filename)
|
os.rename(self.__tempfile, self.__filename)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
|
|
|
@ -2408,8 +2408,9 @@ class Interface:
|
||||||
print(err_str, file=sys.stderr)
|
print(err_str, file=sys.stderr)
|
||||||
# it is good to notify the user
|
# it is good to notify the user
|
||||||
# in case he or she cannot see the output of the console
|
# 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)
|
'preferences'), err_str)
|
||||||
|
error_dialog.run()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
Loading…
Reference in New Issue