From 2fe3ad8b08bdd449c873453b4fd432b5caee3288 Mon Sep 17 00:00:00 2001 From: Vishweshwar Saran Singh Deo Date: Mon, 4 Sep 2023 17:21:37 +0530 Subject: [PATCH 1/2] [bug 802] - Ability to undo or restore changes to the preferences #802 - added basic config restore option from preferences window - changes to glade made in addition to addition of destory signal - minor cleaup in config to have a single function return confilg filename via get_config_filename --- terminatorlib/config.py | 36 +++++++++++++++++++++++++++------ terminatorlib/preferences.glade | 20 ++++++++++++++++-- terminatorlib/prefseditor.py | 11 ++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 4fbb907d..2e555467 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -622,12 +622,7 @@ def load(self): dbg('config already loaded') return - if self.command_line_options and self.command_line_options.config: - filename = self.command_line_options.config - else: - filename = os.path.join(get_config_dir(), 'config') - if not os.path.exists(filename): - filename = os.path.join(get_system_config_dir(), 'config') + filename = self.get_config_filename() dbg('looking for config file: %s' % filename) try: # @@ -706,6 +701,35 @@ def load(self): self.loaded = True + def get_config_filename(self): + filename = '' + if self.command_line_options and self.command_line_options.config: + filename = self.command_line_options.config + else: + filename = os.path.join(get_config_dir(), 'config') + if not os.path.exists(filename): + filename = os.path.join(get_system_config_dir(), 'config') + + 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) + + 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) + + 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) + def reload(self): """Force a reload of the base config""" self.loaded = False diff --git a/terminatorlib/preferences.glade b/terminatorlib/preferences.glade index e0e8a2a5..83a90847 100644 --- a/terminatorlib/preferences.glade +++ b/terminatorlib/preferences.glade @@ -424,6 +424,7 @@ Terminator Preferences 640 400 + True @@ -1241,7 +1242,7 @@ - + True False @@ -4410,6 +4411,21 @@ Much of the behavior of Terminator is based on GNOME Terminal, and we are adding 0 + + + Discard Changes + False + True + True + True + + + + True + True + 1 + + gtk-close @@ -4423,7 +4439,7 @@ Much of the behavior of Terminator is based on GNOME Terminal, and we are adding True True - 1 + 2 diff --git a/terminatorlib/prefseditor.py b/terminatorlib/prefseditor.py index d5933c4b..72c14110 100755 --- a/terminatorlib/prefseditor.py +++ b/terminatorlib/prefseditor.py @@ -235,14 +235,25 @@ def __init__ (self, term, cur_page=0): nb = guiget('notebook1') nb.set_current_page(cur_page) + self.config.base.save_config_with_suffix('_cur') + + def on_destroy_event(self, _widget): + self.config.base.remove_config_with_suffix('_cur') + def on_closebutton_clicked(self, _button): """Close the window""" + self.config.base.remove_config_with_suffix('_cur') terminator = Terminator() terminator.reconfigure() self.window.destroy() self.calling_window.preventHide = False del(self) + def on_restoreconfigbutton_clicked(self, _button): + """restore config to load time""" + self.config.base.restore_config_with_suffix('_cur') + self.on_closebutton_clicked(_button) + def set_values(self): """Update the preferences window with all the configuration from Config()""" From 6fc6d7f65817ce2832ee2d6db8e9708a492f6cce Mon Sep 17 00:00:00 2001 From: Vishweshwar Saran Singh Deo Date: Mon, 11 Sep 2023 18:30:02 +0530 Subject: [PATCH 2/2] [bug 802] 802-Ability-to-undo-or-restore-changes-to-the-preferences #802 - fixed the errors in case of missing configs - restore file is stored in writable path --- terminatorlib/config.py | 60 ++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 2e555467..97583570 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -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""" @@ -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)