adjust mechanism of write function with iohandler
if a write_<parameter> function is defined and <parameter> has an iohandler, the handlers write function is not called automatically. It has to be called explicitly in the write_<param> function, if needed. reasons: - the previous logic when a wrapped write function is already present, and a handler is defined on the specialized class, did not work, and is not easy to solve properly - it is probably anyway better to call the handlers write function explicitly instead of automatically depending on the return value Change-Id: I04f0849b6cc3fb9979c0f5ac8245a6ab4bf23072 Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/22565 Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de> Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
@ -49,9 +49,8 @@ def change_<group>(self, change):
|
||||
# which will be formatted by the handler, or None. The latter is used only in some
|
||||
# special cases, when nothing has to be written.
|
||||
|
||||
A write_<parameter> method may be implemented in addition. In that case, it is executed
|
||||
before change_<group>. write_<parameters> may return None or Done, in these cases
|
||||
change_<group> is not called.
|
||||
A write_<parameter> method may be implemented in addition. In that case, the handlers write
|
||||
method has to be called explicitly int the write_<parameter> method, if needed.
|
||||
"""
|
||||
import re
|
||||
|
||||
@ -263,6 +262,8 @@ class IOHandler(IOHandlerBase):
|
||||
if self._module_class != modclass:
|
||||
raise ProgrammingError("the handler '%s' for '%s.%s' is already used in module '%s'"
|
||||
% (self.group, modclass.__name__, pname, self._module_class.__name__))
|
||||
# self.change might be needed even when get_write_func was not called
|
||||
self.change = getattr(self._module_class, 'change_' + self.group, None)
|
||||
self.parameters.add(pname)
|
||||
self.analyze = getattr(modclass, 'analyze_' + self.group)
|
||||
return self.read
|
||||
@ -295,7 +296,6 @@ class IOHandler(IOHandlerBase):
|
||||
If pre_wfunc is given, it is to be called before change_<group>.
|
||||
May be overriden to return None, if not used
|
||||
"""
|
||||
self.change = getattr(self._module_class, 'change_' + self.group)
|
||||
|
||||
def wfunc(module, value, hdl=self, pname=pname):
|
||||
hdl.write(module, pname, value)
|
||||
|
@ -165,29 +165,27 @@ class ModuleMeta(PropertyMeta):
|
||||
|
||||
if not pobj.readonly:
|
||||
wfunc = attrs.get('write_' + pname, None)
|
||||
for base in bases:
|
||||
if wfunc is not None:
|
||||
break
|
||||
wfunc = getattr(base, 'write_' + pname, None)
|
||||
if wfunc is None: # ignore the handler, if a write function is present
|
||||
wfunc = pobj.handler.get_write_func(pname) if pobj.handler else None
|
||||
for base in bases:
|
||||
if wfunc is not None:
|
||||
break
|
||||
wfunc = getattr(base, 'write_' + pname, None)
|
||||
|
||||
# create wrapper except when write function is already wrapped
|
||||
if wfunc is None or getattr(wfunc, '__wrapped__', False) is False:
|
||||
|
||||
# append write function from handler, to be called after wfunc
|
||||
wfuncs = (wfunc, pobj.handler.get_write_func(pname) if pobj.handler else None)
|
||||
|
||||
def wrapped_wfunc(self, value, pname=pname, wfuncs=wfuncs):
|
||||
def wrapped_wfunc(self, value, pname=pname, wfunc=wfunc):
|
||||
self.log.debug("check validity of %s = %r" % (pname, value))
|
||||
pobj = self.accessibles[pname]
|
||||
value = pobj.datatype(value)
|
||||
for wfunc in filter(None, wfuncs):
|
||||
if wfunc:
|
||||
self.log.debug('calling %s %r(%r)' % (wfunc.__name__, wfunc, value))
|
||||
returned_value = wfunc(self, value)
|
||||
if returned_value is Done: # the setter is already triggered
|
||||
return getattr(self, pname)
|
||||
if returned_value is None: # goodie: accept missing return value
|
||||
break # handler is not called in this case
|
||||
value = returned_value
|
||||
if returned_value is not None: # goodie: accept missing return value
|
||||
value = returned_value
|
||||
setattr(self, pname, value)
|
||||
return value
|
||||
|
||||
|
Reference in New Issue
Block a user