From aee99df2d0a6ba2a9e956c116a22d45d31f65d79 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 29 Oct 2024 14:03:19 +0100 Subject: [PATCH] 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 Tested-by: Jenkins Automated Tests Reviewed-by: Alexander Zaft Reviewed-by: Enrico Faulhaber --- bin/frappy-server | 10 ++++++---- frappy/config.py | 8 ++++---- frappy/gui/cfg_editor/config_file.py | 3 ++- frappy/playground.py | 1 + frappy/server.py | 8 ++++---- test/test_config.py | 2 +- test/test_server.py | 4 ++-- 7 files changed, 20 insertions(+), 16 deletions(-) diff --git a/bin/frappy-server b/bin/frappy-server index 18e83a0..7fcf369 100755 --- a/bin/frappy-server +++ b/bin/frappy-server @@ -68,9 +68,9 @@ def parseArgv(argv): action='store', help="comma separated list of cfg files,\n" "defaults to .\n" - "cfgfiles given without '.cfg' extension are searched" - " in the configuration directory," - " else they are treated as path names", + "If a config file contains a slash, it is treated as a" + "path, otherwise the file is searched for in the " + "configuration directory.", default=None) parser.add_argument('-g', '--gencfg', @@ -104,7 +104,9 @@ def main(argv=None): generalConfig.init(args.gencfg) 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) if args.daemonize: diff --git a/frappy/config.py b/frappy/config.py index 0ef7d83..f157fcc 100644 --- a/frappy/config.py +++ b/frappy/config.py @@ -172,8 +172,8 @@ def load_config(cfgfiles, log): Only the node-section of the first config file will be returned. The others will be discarded. Arguments - - cfgfiles : str - Comma separated list of config-files + - cfgfiles : list + List of config file paths - log : frappy.logging.Mainlogger Logger aquired from frappy.logging Returns @@ -181,8 +181,8 @@ def load_config(cfgfiles, log): merged configuration """ config = None - for cfgfile in cfgfiles.split(','): - filename = to_config_path(cfgfile, log) + for cfgfile in cfgfiles: + filename = to_config_path(str(cfgfile), log) log.debug('Parsing config file %s...', filename) cfg = process_file(filename, log) if config: diff --git a/frappy/gui/cfg_editor/config_file.py b/frappy/gui/cfg_editor/config_file.py index 74a9fdc..16ead9b 100644 --- a/frappy/gui/cfg_editor/config_file.py +++ b/frappy/gui/cfg_editor/config_file.py @@ -218,8 +218,9 @@ def write_config(file_name, tree_widget): with open(file_name, 'w', encoding='utf-8') as configfile: configfile.write('\n'.join(lines)) + def read_config(file_path, log): - config = load_config(file_path, log) + config = load_config([file_path], log) node = TreeWidgetItem(NODE) ifs = TreeWidgetItem(name='Interfaces') mods = TreeWidgetItem(name='Modules') diff --git a/frappy/playground.py b/frappy/playground.py index 4dbd15c..8cb18c4 100644 --- a/frappy/playground.py +++ b/frappy/playground.py @@ -108,6 +108,7 @@ class Playground(Server): if cfgfiles: if not generalConfig.initialized: generalConfig.init() + cfgfiles = [s.strip() for s in cfgfiles.split(',')] merged_cfg = load_config(cfgfiles, self.log) merged_cfg.pop('node', None) self.module_cfg = merged_cfg diff --git a/frappy/server.py b/frappy/server.py index eb748b2..ba925cc 100644 --- a/frappy/server.py +++ b/frappy/server.py @@ -69,9 +69,9 @@ class Server: - name: the node name - parent_logger: the logger to inherit from. a handler is installed by the server to provide remote logging - - cfgfiles: if not given, defaults to name - may be a comma separated list of cfg files - items ending with .cfg are taken as paths, else .cfg is appended and + - cfgfiles: if not given, defaults to [name] + may be a list of cfg files + 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 - interface: an uri of the from tcp:// or a bare port number for tcp if not given, the interface is taken from the config file. In case of @@ -95,7 +95,7 @@ class Server: self._testonly = testonly if not cfgfiles: - cfgfiles = name + cfgfiles = [name] # sanitize name (in case it is a cfgfile) name = os.path.splitext(os.path.basename(name))[0] if isinstance(parent_logger, mlzlog.MLZLogger): diff --git a/test/test_config.py b/test/test_config.py index 19bad80..085b877 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -137,5 +137,5 @@ def test_process_file(direc, log): def test_full(direc, log): - ret = load_config('pyfile_cfg.py', log) + ret = load_config(['pyfile_cfg.py'], log) do_asserts(ret) diff --git a/test/test_server.py b/test/test_server.py index 903b8dd..746f3c3 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -57,13 +57,13 @@ def test_name_only(direc, log): def test_file(direc, log): """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() def test_basic_description(direc, log): """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() desc = s.secnode.get_descriptive_data('') # secnode properties correctly exported