Skip to content

Commit

Permalink
Create config file if it doesn't exist
Browse files Browse the repository at this point in the history
Use pathlib.Path for config file and backup file
  • Loading branch information
planet36 committed Oct 10, 2020
1 parent ebae1e1 commit 310f70a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 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 pathlib
import shutil
from copy import copy
from configobj import ConfigObj, flatten_errors
Expand Down Expand Up @@ -719,15 +720,19 @@ def save(self):
config_dir = get_config_dir()
if not os.path.isdir(config_dir):
os.makedirs(config_dir)

config_file = pathlib.Path(self.command_line_options.config)
if not config_file.is_file():
config_file.touch()
try:
backup_file = self.command_line_options.config + '~'
backup_file = config_file.with_suffix('.bak')

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

with open(self.command_line_options.config, 'wb') as fh:
with open(config_file, 'wb') as fh:
parser.write(fh)

os.remove(backup_file)
backup_file.unlink()
except Exception as ex:
err('ConfigBase::save: Unable to save config: %s' % ex)

Expand Down

0 comments on commit 310f70a

Please sign in to comment.