From 5c9c482843654f4ec0fb9051aae67f7e9de41c26 Mon Sep 17 00:00:00 2001 From: Matt Rose Date: Tue, 1 Dec 2020 14:38:29 -0500 Subject: [PATCH 1/2] load config from XDG_CONFIG_DIRS if user config file doesn't exist --- terminatorlib/config.py | 23 +++++++++++++---------- terminatorlib/util.py | 9 +++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 5e261a18..85a3c65e 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -76,7 +76,7 @@ from copy import copy from configobj import ConfigObj, flatten_errors from validate import Validator from .borg import Borg -from .util import dbg, err, DEBUG, get_config_dir, dict_diff +from .util import dbg, err, DEBUG, get_system_config_dir, get_config_dir, dict_diff from gi.repository import Gio @@ -606,13 +606,12 @@ class ConfigBase(Borg): dbg('ConfigBase::load: config already loaded') return - if self.command_line_options: - if not self.command_line_options.config: - self.command_line_options.config = os.path.join(get_config_dir(), 'config') + if 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') dbg('looking for config file: %s' % filename) try: configfile = open(filename, 'r') @@ -730,14 +729,18 @@ class ConfigBase(Borg): os.makedirs(config_dir) try: - if not os.path.isfile(self.command_line_options.config): - open(self.command_line_options.config, 'a').close() + if self.command_line_options.config: + filename = self.command_line_options.config + else: + filename = os.path.join(config_dir,'config') - backup_file = self.command_line_options.config + '~' + if not os.path.isfile(filename): + open(filename, 'a').close() - shutil.copy2(self.command_line_options.config, backup_file) + backup_file = filename + '~' + shutil.copy2(filename, backup_file) - with open(self.command_line_options.config, 'wb') as fh: + with open(filename, 'wb') as fh: parser.write(fh) os.remove(backup_file) diff --git a/terminatorlib/util.py b/terminatorlib/util.py index 877cf139..769343a5 100644 --- a/terminatorlib/util.py +++ b/terminatorlib/util.py @@ -185,6 +185,15 @@ def widget_pixbuf(widget, maxsize=None): return(scaledpixbuf) +def get_system_config_dir(): + system_config_dir = '/etc/xdg' + if 'XDG_CONFIG_DIRS' in os.environ.keys(): + for sysconfdir in os.environ['XDG_CONFIG_DIRS'].split(":"): + if os.path.isdir(sysconfdir): + system_config_dir = sysconfdir + break + return(os.path.join(system_config_dir,'terminator')) + def get_config_dir(): """Expand all the messy nonsense for finding where ~/.config/terminator really is""" From 890253ba2b7dcc684c5cdb171fd2ab0ee8d2695a Mon Sep 17 00:00:00 2001 From: Matt Rose Date: Tue, 1 Dec 2020 15:49:11 -0500 Subject: [PATCH 2/2] make tests pass --- terminatorlib/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 85a3c65e..3e069e21 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -606,7 +606,7 @@ class ConfigBase(Borg): dbg('ConfigBase::load: config already loaded') return - if self.command_line_options.config: + 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')