Skip to content

Commit

Permalink
Backup config before writing to it
Browse files Browse the repository at this point in the history
Fixes #234
  • Loading branch information
planet36 committed Oct 8, 2020
1 parent 296e125 commit ebae1e1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions terminatorlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"""

import os
import shutil
from copy import copy
from configobj import ConfigObj, flatten_errors
from validate import Validator
Expand Down Expand Up @@ -687,7 +688,7 @@ def reload(self):
"""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')
Expand Down Expand Up @@ -719,12 +720,14 @@ def save(self):
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 + '~'

shutil.copy2(self.command_line_options.config, backup_file)

with open(temp_file, 'wb') as fh:
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)

Expand Down

0 comments on commit ebae1e1

Please sign in to comment.