Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change how config file is saved #235

Merged
merged 4 commits into from
Oct 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 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 @@ -718,13 +719,19 @@ def save(self):
config_dir = get_config_dir()
if not os.path.isdir(config_dir):
os.makedirs(config_dir)

try:
temp_file = self.command_line_options.config + '.tmp'
if not os.path.isfile(self.command_line_options.config):
open(self.command_line_options.config, 'a').close()

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