Use pathlib replace() for saving config
This makes it hopefully more resilient on Windows
This commit is contained in:
parent
56e40954b7
commit
c5d2f8bdab
|
@ -27,6 +27,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from gajim.common import app
|
from gajim.common import app
|
||||||
from gajim.common import caps_cache
|
from gajim.common import caps_cache
|
||||||
|
@ -104,25 +105,22 @@ class OptionsParser:
|
||||||
fd.write(s + ' = ' + value + '\n')
|
fd.write(s + ' = ' + value + '\n')
|
||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
(base_dir, filename) = os.path.split(self.__filename)
|
config_path = Path(self.__filename)
|
||||||
self.__tempfile = os.path.join(base_dir, '.' + filename)
|
tempfile = 'temp_%s' % config_path.name
|
||||||
|
temp_filepath = config_path.parent / tempfile
|
||||||
try:
|
try:
|
||||||
with open(self.__tempfile, 'w', encoding='utf-8') as f:
|
with open(str(temp_filepath), 'w', encoding='utf-8') as file:
|
||||||
app.config.foreach(self.write_line, f)
|
app.config.foreach(self.write_line, file)
|
||||||
except IOError as e:
|
except IOError:
|
||||||
return str(e)
|
log.exception('Failed to write config file')
|
||||||
|
return
|
||||||
|
|
||||||
if os.path.exists(self.__filename):
|
|
||||||
if os.name == 'nt':
|
|
||||||
# win32 needs this
|
|
||||||
try:
|
try:
|
||||||
os.remove(self.__filename)
|
temp_filepath.replace(config_path)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
return str(e)
|
log.exception('Failed to replace config file')
|
||||||
try:
|
else:
|
||||||
os.rename(self.__tempfile, self.__filename)
|
log.info('Successful saved config file')
|
||||||
except IOError as e:
|
|
||||||
return str(e)
|
|
||||||
|
|
||||||
def update_config(self, old_version, new_version):
|
def update_config(self, old_version, new_version):
|
||||||
old_version_list = old_version.split('.') # convert '0.x.y' to (0, x, y)
|
old_version_list = old_version.split('.') # convert '0.x.y' to (0, x, y)
|
||||||
|
|
Loading…
Reference in New Issue