From 140a3f5334cfd8d9f0e762c750aa191f31de554b Mon Sep 17 00:00:00 2001 From: Jan David Mol <mol@astron.nl> Date: Mon, 12 Apr 2021 10:32:46 +0200 Subject: [PATCH] Use uniform calling convention for function wrappers (always with brackets) --- devices/PCC.py | 28 +++++++++++----------- devices/util/attribute_wrapper.py | 12 +++++----- devices/util/hardware_device.py | 4 ++-- devices/util/wrappers.py | 40 ++++++++++++++++++------------- 4 files changed, 45 insertions(+), 39 deletions(-) diff --git a/devices/PCC.py b/devices/PCC.py index e2d3383f3..d660ff56d 100644 --- a/devices/PCC.py +++ b/devices/PCC.py @@ -160,8 +160,8 @@ class PCC(hardware_device): # -------- @command() @DebugIt() - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def RCU_off(self): """ @@ -171,8 +171,8 @@ class PCC(hardware_device): @command() @DebugIt() - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def RCU_on(self): """ @@ -182,8 +182,8 @@ class PCC(hardware_device): @command() @DebugIt() - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def ADC_on(self): """ @@ -193,8 +193,8 @@ class PCC(hardware_device): @command() @DebugIt() - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def RCU_update(self): """ @@ -204,8 +204,8 @@ class PCC(hardware_device): @command() @DebugIt() - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def CLK_off(self): """ @@ -215,8 +215,8 @@ class PCC(hardware_device): @command() @DebugIt() - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def CLK_on(self): """ @@ -226,8 +226,8 @@ class PCC(hardware_device): @command() @DebugIt() - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def CLK_PLL_setup(self): """ diff --git a/devices/util/attribute_wrapper.py b/devices/util/attribute_wrapper.py index a303cfddb..9949c9dde 100644 --- a/devices/util/attribute_wrapper.py +++ b/devices/util/attribute_wrapper.py @@ -58,8 +58,8 @@ class attribute_wrapper(attribute): if access == AttrWriteType.READ_WRITE: """ if the attribute is of READ_WRITE type, assign the RW and write function to it""" - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def read_RW(device): # print("read_RW {}, {}x{}, {}, {}".format(me.name, me.dim_x, me.dim_y, me.attr_type, me.value)) """ @@ -71,8 +71,8 @@ class attribute_wrapper(attribute): raise Exception("Attribute read_RW function error, attempted to read value_dict with key: `%s`, are you sure this exists?", self) from e - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def write_RW(device, value): """ _write_RW writes a value to this attribute @@ -87,8 +87,8 @@ class attribute_wrapper(attribute): else: """ if the attribute is of READ type, assign the read function to it""" - @only_when_on - @fault_on_error + @only_when_on() + @fault_on_error() def read_R(device): """ _read_R reads the attribute value, stores it and returns it" diff --git a/devices/util/hardware_device.py b/devices/util/hardware_device.py index ce6a9797d..749ba17ac 100644 --- a/devices/util/hardware_device.py +++ b/devices/util/hardware_device.py @@ -76,7 +76,7 @@ class hardware_device(Device): @command() @only_in_states([DevState.FAULT, DevState.OFF]) @DebugIt() - @fault_on_error + @fault_on_error() @log_exceptions() def Initialise(self): """ @@ -104,7 +104,7 @@ class hardware_device(Device): @command() @only_in_states([DevState.STANDBY]) @DebugIt() - @fault_on_error + @fault_on_error() @log_exceptions() def On(self): """ diff --git a/devices/util/wrappers.py b/devices/util/wrappers.py index 9dbc45a68..5300ba2a0 100644 --- a/devices/util/wrappers.py +++ b/devices/util/wrappers.py @@ -22,32 +22,38 @@ def only_in_states(allowed_states): return wrapper -def only_when_on(func): +def only_when_on(): """ Wrapper to call and return the wrapped function if the device is in the ON state. Otherwise None is returned and nothing will be called. """ - @wraps(func) - def when_on_wrapper(self, *args, **kwargs): - if self.get_state() == DevState.ON: - return func(self, *args, **kwargs) + def inner(func): + @wraps(func) + def when_on_wrapper(self, *args, **kwargs): + if self.get_state() == DevState.ON: + return func(self, *args, **kwargs) + + return None - return None + return when_on_wrapper - return when_on_wrapper + return inner -def fault_on_error(func): +def fault_on_error(): """ Wrapper to catch exceptions. Sets the device in a FAULT state if any occurs. """ - @wraps(func) - def error_wrapper(self, *args, **kwargs): - try: - return func(self, *args, **kwargs) - except Exception as e: - self.error_stream("Function failed. Trace: %s", traceback.format_exc()) - self.Fault() - return None + def inner(func): + @wraps(func) + def error_wrapper(self, *args, **kwargs): + try: + return func(self, *args, **kwargs) + except Exception as e: + self.error_stream("Function failed. Trace: %s", traceback.format_exc()) + self.Fault() + return None + + return error_wrapper - return error_wrapper + return inner -- GitLab