From f06fa9faa24d24822a5266891e0d26c0ada68e03 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Thu, 19 Dec 2019 09:39:43 +0100 Subject: [PATCH] accept module properties without leading '.' in config files as module properties and parameters anyway share the same namespace, there is no need to distinguish in config files. + a parameter default value may be overriden just with a class attribute. Both improvements help to switch between parameters and properties more easily. Change-Id: Ieb5cf3121f37c7c04e63345d3e95dfaf42726455 Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/22054 Tested-by: JenkinsCodeReview Reviewed-by: Markus Zolliker --- secop/metaclass.py | 12 +++++++++++- secop/modules.py | 10 +++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/secop/metaclass.py b/secop/metaclass.py index 8aca3c8..7fda25a 100644 --- a/secop/metaclass.py +++ b/secop/metaclass.py @@ -25,7 +25,7 @@ import time from collections import OrderedDict -from secop.errors import ProgrammingError +from secop.errors import ProgrammingError, BadValueError from secop.params import Command, Override, Parameter from secop.datatypes import EnumType from secop.properties import PropertyMeta @@ -103,6 +103,16 @@ class ModuleMeta(PropertyMeta): # 2) for the describing message newtype.accessibles = OrderedDict(sorted(accessibles.items(), key=lambda item: item[1].ctr)) + # check for attributes overriding parameter values + for pname, pobj in newtype.accessibles.items(): + if pname in attrs: + try: + value = pobj.datatype(attrs[pname]) + except BadValueError: + raise ProgrammingError('parameter %s can not be set to %r' + % (pname, attrs[pname])) + newtype.accessibles[pname] = Override(default=value).apply(pobj) + # check validity of Parameter entries for pname, pobj in newtype.accessibles.items(): # XXX: create getters for the units of params ?? diff --git a/secop/modules.py b/secop/modules.py index d4e5571..9bdf187 100644 --- a/secop/modules.py +++ b/secop/modules.py @@ -100,6 +100,7 @@ class Module(HasProperties, metaclass=ModuleMeta): # 2) check and apply properties specified in cfgdict # specified as '. = ' + # (this is for legacy config files only) for k, v in list(cfgdict.items()): # keep list() as dict may change during iter if k[0] == '.': if k[1:] in self.__class__.properties: @@ -108,6 +109,12 @@ class Module(HasProperties, metaclass=ModuleMeta): raise ConfigError('Module %r has no property %r' % (self.name, k[1:])) + # 3) check and apply properties specified in cfgdict as + # ' = ' (without '.' prefix) + for k in self.__class__.properties: + if k in cfgdict: + self.setProperty(k, cfgdict.pop(k)) + # 4) set automatic properties mycls = self.__class__ myclassname = '%s.%s' % (mycls.__module__, mycls.__name__) @@ -175,7 +182,8 @@ class Module(HasProperties, metaclass=ModuleMeta): raise ConfigError( 'Module %s:config Parameter %r ' 'not understood! (use one of %s)' % - (self.name, k, ', '.join(self.parameters.keys()))) + (self.name, k, ', '.join(list(self.parameters) + + list(self.__class__.properties)))) # 4) complain if a Parameter entry has no default value and # is not specified in cfgdict and deal with parameters to be written.