Skip to content

Commit

Permalink
[bug 802] 802-Ability-to-undo-or-restore-changes-to-the-preferences #802
Browse files Browse the repository at this point in the history


- fixed the errors in case of missing configs
- restore file is stored in writable path
  • Loading branch information
vssdeo committed Sep 11, 2023
1 parent 2fe3ad8 commit 6fc6d7f
Showing 1 changed file with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions terminatorlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,22 +713,55 @@ def get_config_filename(self):
return filename

def save_config_with_suffix(self, suffix):
filename = self.get_config_filename()
#save the current config, to revert any changes make in preferences
cur_loaded_file = filename + suffix
shutil.copy2(filename, cur_loaded_file)
try:
filename = self.get_config_filename()

#save the current config, to revert any changes make in preferences
#save the current config to config_dir path which is at least writable
cfg_filename = os.path.join(get_config_dir(), 'config')
cur_loaded_file = cfg_filename + suffix

if os.path.exists(filename) and cur_loaded_file:
dbg('copy file:%s to' \
' file:%s' % (filename, cur_loaded_file))
shutil.copy2(filename, cur_loaded_file)
elif cur_loaded_file:
open(cur_loaded_file, 'a').close()
else:
err('ConfigBase:: Unable to get filename to save')
except Exception as ex:
err('ConfigBase::save_config_with_suffix' \
' Unable to save config: %s' % ex)

def restore_config_with_suffix(self, suffix):
filename = self.get_config_filename()
cur_loaded_file = filename + suffix
dbg("restoring from file:%s to file:%s" % (cur_loaded_file, filename))
shutil.copy2(cur_loaded_file, filename)
try:
filename = self.get_config_filename()

cfg_filename = os.path.join(get_config_dir(), 'config')
cur_loaded_file = cfg_filename + suffix
if os.path.exists(cur_loaded_file):
if not os.access(filename, os.W_OK):
dbg('path:%s not writable' \
' restoring to path:%s' % (filename,cfg_filename))
filename = cfg_filename

dbg('restore from file:%s to file:%s'
% (cur_loaded_file, filename))
shutil.copy2(cur_loaded_file, filename)
except Exception as ex:
err('ConfigBase::restore_config_with_suffix' \
' Unable to restore config: %s' % ex)

def remove_config_with_suffix(self, suffix):
filename = self.get_config_filename()
cur_loaded_file = filename + suffix
if os.path.exists(cur_loaded_file):
os.remove(cur_loaded_file)
try:
cfg_filename = os.path.join(get_config_dir(), 'config')
cur_loaded_file = cfg_filename + suffix
if os.path.exists(cur_loaded_file):
dbg('remove file:%s' % (cur_loaded_file))
os.remove(cur_loaded_file)
except Exception as ex:
err('ConfigBase::remove_config_with_suffix' \
' Unable to remove config: %s' % ex)

def reload(self):
"""Force a reload of the base config"""
Expand Down Expand Up @@ -795,7 +828,8 @@ def save(self):
open(filename, 'a').close()

backup_file = filename + '~'
shutil.copy2(filename, backup_file)
if os.path.exists(filename):
shutil.copy2(filename, backup_file)

with open(filename, 'wb') as fh:
parser.write(fh)
Expand Down

0 comments on commit 6fc6d7f

Please sign in to comment.