lib: Fix GeneralConfig defaults handling

overwriting defaults before init() had no effect, as the values were
replaced there.
Now, only unset defaults are updated, keeping the overrides.
Also fix unconditionally taking the values from environment variables.

Change-Id: Idf1c5e2338403e061d20c11ed9c4803d535eb188
Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/34304
Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
Reviewed-by: Alexander Zaft <a.zaft@fz-juelich.de>
This commit is contained in:
Alexander Zaft
2024-08-06 11:41:22 +02:00
committed by Markus Zolliker
parent 96a388eae9
commit f0f12d6d96

View File

@ -72,16 +72,17 @@ class GeneralConfig:
mandatory = 'piddir', 'logdir', 'confdir' mandatory = 'piddir', 'logdir', 'confdir'
repodir = path.abspath(path.join(path.dirname(__file__), '..', '..')) repodir = path.abspath(path.join(path.dirname(__file__), '..', '..'))
# create default paths # create default paths
if path.splitext(sys.executable)[1] == ".exe" and not path.basename(sys.executable).startswith('python'): if (path.splitext(sys.executable)[1] == ".exe"
and not path.basename(sys.executable).startswith('python')):
# special MS windows environment # special MS windows environment
cfg.update(piddir='./', logdir='./log', confdir='./') self.update_defaults(piddir='./', logdir='./log', confdir='./')
elif path.exists(path.join(repodir, 'cfg')): elif path.exists(path.join(repodir, 'cfg')):
# running from git repo # running from git repo
cfg['confdir'] = path.join(repodir, 'cfg') self.set_default('confdir', path.join(repodir, 'cfg'))
# take logdir and piddir from <repodir>/cfg/generalConfig.cfg # take logdir and piddir from <repodir>/cfg/generalConfig.cfg
else: else:
# running on installed system (typically with systemd) # running on installed system (typically with systemd)
cfg.update(piddir='/var/run/frappy', logdir='/var/log', confdir='/etc/frappy') self.update_defaults(piddir='/var/run/frappy', logdir='/var/log', confdir='/etc/frappy')
if configfile is None: if configfile is None:
configfile = environ.get('FRAPPY_CONFIG_FILE') configfile = environ.get('FRAPPY_CONFIG_FILE')
if configfile: if configfile:
@ -89,7 +90,7 @@ class GeneralConfig:
if not path.exists(configfile): if not path.exists(configfile):
raise FileNotFoundError(configfile) raise FileNotFoundError(configfile)
else: else:
configfile = path.join(cfg['confdir'], 'generalConfig.cfg') configfile = path.join(self['confdir'], 'generalConfig.cfg')
if not path.exists(configfile): if not path.exists(configfile):
configfile = None configfile = None
if configfile: if configfile:
@ -106,12 +107,14 @@ class GeneralConfig:
if cfg.get('confdir') is None: if cfg.get('confdir') is None:
cfg['confdir'] = path.dirname(configfile) cfg['confdir'] = path.dirname(configfile)
for key in mandatory: for key in mandatory:
cfg[key] = environ.get(f'FRAPPY_{key.upper()}', cfg.get(key)) if (env := environ.get(f'FRAPPY_{key.upper()}')) is not None:
missing_keys = [key for key in mandatory if cfg[key] is None] cfg[key] = env
missing_keys = [key for key in mandatory if self[key] is None]
if missing_keys: if missing_keys:
if configfile: if configfile:
raise KeyError(f"missing value for {' and '.join(missing_keys)} in {configfile}") raise KeyError(f"missing value for {' and '.join(missing_keys)} in {configfile}")
raise KeyError('missing %s' % ' and '.join('FRAPPY_%s' % k.upper() for k in missing_keys)) raise KeyError('missing %s'
% ' and '.join('FRAPPY_%s' % k.upper() for k in missing_keys))
# this is not customizable # this is not customizable
cfg['basedir'] = repodir cfg['basedir'] = repodir
self._config = cfg self._config = cfg
@ -155,6 +158,11 @@ class GeneralConfig:
def initialized(self): def initialized(self):
return bool(self._config) return bool(self._config)
def update_defaults(self, **updates):
"""Set a default value, when there is not already one for each dict entry."""
for key, value in updates.items():
self.set_default(key, value)
def set_default(self, key, value): def set_default(self, key, value):
"""set a default value, in case not set already""" """set a default value, in case not set already"""
if key not in self.defaults: if key not in self.defaults: