load config from XDG_CONFIG_DIRS if user config file doesn't exist

This commit is contained in:
Matt Rose 2020-12-01 14:38:29 -05:00
parent 40bd59481b
commit 5c9c482843
2 changed files with 22 additions and 10 deletions

View File

@ -76,7 +76,7 @@ from copy import copy
from configobj import ConfigObj, flatten_errors from configobj import ConfigObj, flatten_errors
from validate import Validator from validate import Validator
from .borg import Borg 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 from gi.repository import Gio
@ -606,13 +606,12 @@ class ConfigBase(Borg):
dbg('ConfigBase::load: config already loaded') dbg('ConfigBase::load: config already loaded')
return return
if self.command_line_options: if self.command_line_options.config:
if not self.command_line_options.config:
self.command_line_options.config = os.path.join(get_config_dir(), 'config')
filename = self.command_line_options.config filename = self.command_line_options.config
else: else:
filename = os.path.join(get_config_dir(), 'config') 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) dbg('looking for config file: %s' % filename)
try: try:
configfile = open(filename, 'r') configfile = open(filename, 'r')
@ -730,14 +729,18 @@ class ConfigBase(Borg):
os.makedirs(config_dir) os.makedirs(config_dir)
try: try:
if not os.path.isfile(self.command_line_options.config): if self.command_line_options.config:
open(self.command_line_options.config, 'a').close() 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) parser.write(fh)
os.remove(backup_file) os.remove(backup_file)

View File

@ -185,6 +185,15 @@ def widget_pixbuf(widget, maxsize=None):
return(scaledpixbuf) 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(): def get_config_dir():
"""Expand all the messy nonsense for finding where ~/.config/terminator """Expand all the messy nonsense for finding where ~/.config/terminator
really is""" really is"""