Skip to content
Snippets Groups Projects
Commit 121f067c authored by Thomas Juerges's avatar Thomas Juerges
Browse files

Rafactor the logic in the wrapper functions on_*

The logic was backwards since it was tested if a condition was
not given.  Instead the logic should follow what the wrapper name
suggests:  test if the device is in a state or if the device is in
state on.
parent 204091f8
No related branches found
No related tags found
No related merge requests found
...@@ -6,41 +6,38 @@ __all__ = ["only_in_states", "only_when_on", "fault_on_error"] ...@@ -6,41 +6,38 @@ __all__ = ["only_in_states", "only_when_on", "fault_on_error"]
def only_in_states(func, allowed_states): def only_in_states(func, allowed_states):
""" """
Wrapper to return None when the device isn't in ON state. Wrapper to call and return the wrapped function if the device is
in one of the given states. Otherwise None is returned and nothing
If in ON state, calls & returns the wrapped function. will be called.
""" """
@wraps(func) @wraps(func)
def state_check_wrapper(self, *args, **kwargs): def state_check_wrapper(self, *args, **kwargs):
if self.get_state() not in allowed_states: if self.get_state() in allowed_states:
return None
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
return None
return state_check_wrapper return state_check_wrapper
def only_when_on(func): def only_when_on(func):
""" """
Wrapper to return None when the device isn't in ON state. Wrapper to call and return the wrapped function if the device is
in the ON state. Otherwise None is returned and nothing
If in ON state, calls & returns the wrapped function. will be called.
""" """
@wraps(func) @wraps(func)
def when_on_wrapper(self, *args, **kwargs): def when_on_wrapper(self, *args, **kwargs):
if self.get_state() != DevState.ON: if self.get_state() == DevState.ON:
return None
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
return None
return when_on_wrapper return when_on_wrapper
def fault_on_error(func): def fault_on_error(func):
""" """
Wrapper to catch exceptions. Sets the device in a FAULT state if any occurs. Wrapper to catch exceptions. Sets the device in a FAULT state if any occurs.
""" """
@wraps(func) @wraps(func)
def error_wrapper(self, *args, **kwargs): def error_wrapper(self, *args, **kwargs):
try: try:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment