From ebae1e1440af5a2732e22ccd80152ca63644c9cd Mon Sep 17 00:00:00 2001 From: planet36 Date: Thu, 8 Oct 2020 17:11:32 -0400 Subject: [PATCH] Backup config before writing to it Fixes #234 --- terminatorlib/config.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index f58a0716..81e9c20a 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -71,6 +71,7 @@ KeyError: 'ConfigBase::get_item: unknown key algo' """ import os +import shutil from copy import copy from configobj import ConfigObj, flatten_errors from validate import Validator @@ -687,7 +688,7 @@ class ConfigBase(Borg): """Force a reload of the base config""" self.loaded = False self.load() - + def save(self): """Save the config to a file""" dbg('ConfigBase::save: saving config') @@ -719,12 +720,14 @@ class ConfigBase(Borg): if not os.path.isdir(config_dir): os.makedirs(config_dir) try: - temp_file = self.command_line_options.config + '.tmp' + backup_file = self.command_line_options.config + '~' - with open(temp_file, 'wb') as fh: + shutil.copy2(self.command_line_options.config, backup_file) + + with open(self.command_line_options.config, 'wb') as fh: parser.write(fh) - os.rename(temp_file, self.command_line_options.config) + os.remove(backup_file) except Exception as ex: err('ConfigBase::save: Unable to save config: %s' % ex)