server: better handling of cfgfile argument

No reason to keep stringly-typed data on that level

Change-Id: Iba8d88301bf36ef6051031d1916d1bac84ede546
Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/34893
Reviewed-by: Georg Brandl <g.brandl@fz-juelich.de>
Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de>
Reviewed-by: Alexander Zaft <a.zaft@fz-juelich.de>
Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
This commit is contained in:
Georg Brandl 2024-10-29 14:03:19 +01:00 committed by Markus Zolliker
parent 880d472a4a
commit 1851c0ac43
7 changed files with 20 additions and 16 deletions

View File

@ -68,9 +68,9 @@ def parseArgv(argv):
action='store', action='store',
help="comma separated list of cfg files,\n" help="comma separated list of cfg files,\n"
"defaults to <name_of_the_instance>.\n" "defaults to <name_of_the_instance>.\n"
"cfgfiles given without '.cfg' extension are searched" "If a config file contains a slash, it is treated as a"
" in the configuration directory," "path, otherwise the file is searched for in the "
" else they are treated as path names", "configuration directory.",
default=None) default=None)
parser.add_argument('-g', parser.add_argument('-g',
'--gencfg', '--gencfg',
@ -104,7 +104,9 @@ def main(argv=None):
generalConfig.init(args.gencfg) generalConfig.init(args.gencfg)
logger.init(loglevel) logger.init(loglevel)
srv = Server(args.name, logger.log, cfgfiles=args.cfgfiles, cfgfiles = [s.strip() for s in args.cfgfiles.split(',')]
srv = Server(args.name, logger.log, cfgfiles=cfgfiles,
interface=args.port, testonly=args.test) interface=args.port, testonly=args.test)
if args.daemonize: if args.daemonize:

View File

@ -172,8 +172,8 @@ def load_config(cfgfiles, log):
Only the node-section of the first config file will be returned. Only the node-section of the first config file will be returned.
The others will be discarded. The others will be discarded.
Arguments Arguments
- cfgfiles : str - cfgfiles : list
Comma separated list of config-files List of config file paths
- log : frappy.logging.Mainlogger - log : frappy.logging.Mainlogger
Logger aquired from frappy.logging Logger aquired from frappy.logging
Returns Returns
@ -181,8 +181,8 @@ def load_config(cfgfiles, log):
merged configuration merged configuration
""" """
config = None config = None
for cfgfile in cfgfiles.split(','): for cfgfile in cfgfiles:
filename = to_config_path(cfgfile, log) filename = to_config_path(str(cfgfile), log)
log.debug('Parsing config file %s...', filename) log.debug('Parsing config file %s...', filename)
cfg = process_file(filename, log) cfg = process_file(filename, log)
if config: if config:

View File

@ -218,8 +218,9 @@ def write_config(file_name, tree_widget):
with open(file_name, 'w', encoding='utf-8') as configfile: with open(file_name, 'w', encoding='utf-8') as configfile:
configfile.write('\n'.join(lines)) configfile.write('\n'.join(lines))
def read_config(file_path, log): def read_config(file_path, log):
config = load_config(file_path, log) config = load_config([file_path], log)
node = TreeWidgetItem(NODE) node = TreeWidgetItem(NODE)
ifs = TreeWidgetItem(name='Interfaces') ifs = TreeWidgetItem(name='Interfaces')
mods = TreeWidgetItem(name='Modules') mods = TreeWidgetItem(name='Modules')

View File

@ -108,6 +108,7 @@ class Playground(Server):
if cfgfiles: if cfgfiles:
if not generalConfig.initialized: if not generalConfig.initialized:
generalConfig.init() generalConfig.init()
cfgfiles = [s.strip() for s in cfgfiles.split(',')]
merged_cfg = load_config(cfgfiles, self.log) merged_cfg = load_config(cfgfiles, self.log)
merged_cfg.pop('node', None) merged_cfg.pop('node', None)
self.module_cfg = merged_cfg self.module_cfg = merged_cfg

View File

@ -69,9 +69,9 @@ class Server:
- name: the node name - name: the node name
- parent_logger: the logger to inherit from. a handler is installed by - parent_logger: the logger to inherit from. a handler is installed by
the server to provide remote logging the server to provide remote logging
- cfgfiles: if not given, defaults to name - cfgfiles: if not given, defaults to [name]
may be a comma separated list of cfg files may be a list of cfg files
items ending with .cfg are taken as paths, else .cfg is appended and items ending with .py are taken as paths, else _cfg.py is appended and
files are looked up in the config path retrieved from the general config files are looked up in the config path retrieved from the general config
- interface: an uri of the from tcp://<port> or a bare port number for tcp - interface: an uri of the from tcp://<port> or a bare port number for tcp
if not given, the interface is taken from the config file. In case of if not given, the interface is taken from the config file. In case of
@ -95,7 +95,7 @@ class Server:
self._testonly = testonly self._testonly = testonly
if not cfgfiles: if not cfgfiles:
cfgfiles = name cfgfiles = [name]
# sanitize name (in case it is a cfgfile) # sanitize name (in case it is a cfgfile)
name = os.path.splitext(os.path.basename(name))[0] name = os.path.splitext(os.path.basename(name))[0]
if isinstance(parent_logger, mlzlog.MLZLogger): if isinstance(parent_logger, mlzlog.MLZLogger):

View File

@ -137,5 +137,5 @@ def test_process_file(direc, log):
def test_full(direc, log): def test_full(direc, log):
ret = load_config('pyfile_cfg.py', log) ret = load_config(['pyfile_cfg.py'], log)
do_asserts(ret) do_asserts(ret)

View File

@ -57,13 +57,13 @@ def test_name_only(direc, log):
def test_file(direc, log): def test_file(direc, log):
"""only see that this does not throw. get config from cfgfiles.""" """only see that this does not throw. get config from cfgfiles."""
s = Server('foo', log, cfgfiles='pyfile_cfg.py') s = Server('foo', log, cfgfiles=['pyfile_cfg.py'])
s._processCfg() s._processCfg()
def test_basic_description(direc, log): def test_basic_description(direc, log):
"""only see that this does not throw. get config from cfgfiles.""" """only see that this does not throw. get config from cfgfiles."""
s = Server('foo', log, cfgfiles='pyfile_cfg.py') s = Server('foo', log, cfgfiles=['pyfile_cfg.py'])
s._processCfg() s._processCfg()
desc = s.secnode.get_descriptive_data('') desc = s.secnode.get_descriptive_data('')
# secnode properties correctly exported # secnode properties correctly exported