Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code quality improvements to the TangoShutter Class #847

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5601846
Update of the TangoShutter class
Jan 10, 2024
4eaab9d
Refactoring of the TangoShutter class
Jan 11, 2024
e11507b
minor change to the class instantiation
Jan 11, 2024
7c7d2d8
Update mxcubecore/HardwareObjects/TangoShutter.py
HilbertCorsair Jan 11, 2024
adeebc1
minor change : super().init() instead of AbstractShutter.init(self)
Jan 12, 2024
60cb43e
code formatting with black
Jan 12, 2024
557ff3c
no change in code: pre-commit hooks test
Jan 15, 2024
113b623
Revert "no change in code: pre-commit hooks test"
Jan 16, 2024
019f8bd
cosmetic changes after pylint analysis: rm traing spaces, rm unused i…
Jan 16, 2024
80eea86
formating line endings to LF to address mixed line endings issue
Jan 16, 2024
6434c67
new linting
Jan 22, 2024
2a590f0
Merge remote-tracking branch 'upstream/develop' into develop
Jan 23, 2024
f4c2960
Merge remote-tracking branch 'upstream/develop' into develop
Jan 30, 2024
5800b6d
code quality improvements in the TangoShutter class
Jan 30, 2024
bad989a
Update mxcubecore/HardwareObjects/TangoShutter.py
HilbertCorsair Jan 31, 2024
a3bcdf0
adding logging and minor chenges in the comments
Jan 31, 2024
7ecf1fe
Merge remote-tracking branch 'origin/develop' into develop
Jan 31, 2024
e4b2434
minor corections in the comments
Feb 1, 2024
670efc0
Dropping json for ast.literal_eval in initalize values
Feb 6, 2024
15a282e
Merge remote-tracking branch 'upstream/develop' into develop
Feb 6, 2024
f01e33c
tests on HardwareObjects.TangoShutter hardware objects
elmjag Feb 1, 2024
3eb6cd7
Merge remote-tracking branch 'upstream/develop' into develop
Feb 22, 2024
cceeaa1
Minor refactoring of get_object_by_role() in HardwareObjectNode class
Feb 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions mxcubecore/BaseHardwareObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,31 +472,20 @@ def get_object_by_role(self, role: str) -> Union["HardwareObject", None]:
Returns:
Union[HardwareObject, None]: Hardware object.
"""
object = None
obj: Self = self
objects = []
role = str(role).lower()
objects = [o for o in self if o]

while True:
if role in obj._objects_by_role:
return obj._objects_by_role[role]
while objects:
if role in self._objects_by_role:
return self._objects_by_role[role]

for object in obj:
objects.append(object)
try :
self = objects.pop()

try:
obj = objects.pop()
except IndexError:
break
else:
object = obj.get_object_by_role(role)
if object is not None:
return object
except :
if self.get_object_by_role(role):
return self.get_object_by_role(role)

if len(objects) > 0:
obj = objects.pop()
else:
break

def objects_names(self) -> List[Union[str, None]]:
"""Return hardware object names.
Expand Down
68 changes: 38 additions & 30 deletions mxcubecore/HardwareObjects/TangoShutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,29 @@
<command type="tango" name="Open">Open</command>
<command type="tango" name="Close">Close</command>
<channel type="tango" name="State" polling="1000">State</channel>
<values>{"open": "OPEN", "cloded": "CLOSED", "DISABLE" : "DISABLE"}</values>
<values>{"OPEN": "MYOPEN", "NEWSTATE": ["MYSTATE", "BUSY"]}</values>
</object>

In this example the <values> tag contains a json dictionary that maps spectific tango shutter states to the
convantional states defined in the TangoShutter Class. This tag is not necessay in cases where the tango shutter states
are all covered by the TangoShuter class conventional states.
In the example the <values> property contains a dictionary that redefines or
adds specific tango shutter states.
When redefining a known state, only the VALUES Enum will be updated.
When defining a new state (new key), the dictionary value should be a
list. The new state is added to both the VALUES and the SPECIFIC_STATES Enum.
Attention:
- do not use tuples or the python json parser will fail!
- make sure only double quotes are used inside the values dictionary. No single quotes (') are allowed !
- the second element of the list should be a standard HardwareObjectState name
(UNKNOWN, WARNING, BUSY, READY, FAULT, OFF - see in BaseHardwareObjects.py)!
The <values> property is optional.
"""
import json

import logging
import ast
from enum import Enum, unique
from mxcubecore.HardwareObjects.abstract.AbstractShutter import AbstractShutter
from mxcubecore.BaseHardwareObjects import HardwareObjectState

__copyright__ = """ Copyright © 2023 by the MXCuBE collaboration """
__copyright__ = """ Copyright © by the MXCuBE collaboration """
__license__ = "LGPLv3+"


Expand All @@ -58,6 +68,7 @@ class TangoShutterStates(Enum):
AUTOMATIC = HardwareObjectState.READY, "RUNNING"
UNKNOWN = HardwareObjectState.UNKNOWN, "RUNNING"
FAULT = HardwareObjectState.WARNING, "FAULT"
STANDBY = HardwareObjectState.WARNING, "STANDBY"


class TangoShutter(AbstractShutter):
Expand All @@ -81,26 +92,18 @@ def init(self):
self.state_channel.connect_signal("update", self._update_value)
self.update_state()

try:
self.config_values = json.loads(self.get_property("values"))
except:
self.config_values = None

def _update_value(self, value):
"""Update the value.
Args:
value(str): The value reported by the state channel.
"""
if self.config_values:
value = self.config_values[str(value)]
else:
value = str(value)

super().update_value(self.value_to_enum(value))
super().update_value(self.value_to_enum(str(value)))

def _initialise_values(self):
"""Add the tango states to VALUES"""
"""Add specific tango states to VALUES and, if configured
in the xml file, to SPECIFIC_STATES"""
values_dict = {item.name: item.value for item in self.VALUES}
states_dict = {item.name: item.value for item in self.SPECIFIC_STATES}
values_dict.update(
{
"MOVING": "MOVING",
Expand All @@ -109,33 +112,38 @@ def _initialise_values(self):
"FAULT": "FAULT",
}
)
try:
config_values = ast.literal_eval(self.get_property("values"))
for key, val in config_values.items():
if isinstance(val, (tuple, list)):
values_dict.update({key: val[1]})
states_dict.update({key: (HardwareObjectState[val[1]], val[0])})
else:
values_dict.update({key: val})
except (ValueError, TypeError) as err:
logging.error(f"Exception in _initialise_values(): {err}")

self.VALUES = Enum("ValueEnum", values_dict)
self.SPECIFIC_STATES = Enum("TangoShutterStates", states_dict)

def get_state(self):
"""Get the device state.
Returns:
(enum 'HardwareObjectState'): Device state.
"""
try:
if self.config_values:
_state = self.config_values[str(self.state_channel.get_value())]
else:
_state = str(self.state_channel.get_value())

except (AttributeError, KeyError):
_state = self.get_value().name
return self.SPECIFIC_STATES[_state].value[0]
except (AttributeError, KeyError) as err:
logging.error(f"Exception in get_state(): {err}")
return self.STATES.UNKNOWN

return self.SPECIFIC_STATES[_state].value[0]

def get_value(self):
"""Get the device value
Returns:
(Enum): Enum member, corresponding to the 'VALUE' or UNKNOWN.
"""
if self.config_values:
_val = self.config_values[str(self.state_channel.get_value())]
else:
_val = str(self.state_channel.get_value())
_val = str(self.state_channel.get_value())
return self.value_to_enum(_val)

def _set_value(self, value):
Expand Down
119 changes: 119 additions & 0 deletions test/pytest/test_hwo_tango_shutter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from typing import Callable
import pytest
import gevent
from gevent import Timeout
from tango import DeviceProxy, DevState
from tango.server import Device, command
from tango.test_context import DeviceTestContext
from mxcubecore.HardwareObjects import TangoShutter

"""
Test the HardwareObjects.TangoShutter shutter hardware object.
"""


VALUES_JSON = """
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this variable to for example VALUES_LITERAL. The VALUES_JSON is now confusing, as technically speaking we are not supporting JSON strings but pythong literal expressions. Event if it so happens in this case that text is identical.

Copy link
Member

@beteva beteva Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @elmjag, I am a bit puzzled that we need a special test for the TangoShutter and not using the generic shutter test. Do we miss something in the generic shutter test or the ShutterMockup. If such is the case, maybe it is worth adding your work to the generic shutter test, so it would serve other shutter types, not just the Tango ones.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are tests to test code in TangoShutter.py. The generic shutter test code does not cover it, as it is using ShutterMockup hardware object.

More specifically these tests set-up an actual tango device and instantiate a TangoShutter hardware object that talks to that device. This way you check that opening/closing shutter work, with TangoShutter hwo, using real tango stack.

They way I read the code in test/pytest/test_shutter.py, it is testing the code in the abstract classes starting with AbstractShutter.

The tests in test_shutter.py and test_hwo_tango_shutter.py are testing related, but separate aspects of the code base.

{"OPEN": "OPEN", "CLOSED": "CLOSE", "MOVING" : "MOVING"}
"""


class Shutter(Device):
"""
Very simple tango shutter device, that only goes between 'open' and 'close' states.
"""

def __init__(self, *args, **kwargs):
self._is_open = False
super().__init__(*args, **kwargs)

@command()
def Open(self):
self._is_open = True

@command()
def Close(self):
self._is_open = False

def dev_state(self):
return DevState.OPEN if self._is_open else DevState.CLOSE


@pytest.fixture
def shutter():
tangods_test_context = DeviceTestContext(Shutter, process=True)
tangods_test_context.start()

#
# set up the TangoShutter hardware object
#
hwo_shutter = TangoShutter.TangoShutter("/random_name")
hwo_shutter.tangoname = tangods_test_context.get_device_access()
hwo_shutter.set_property("values", VALUES_JSON)
hwo_shutter.add_channel(
{
"name": "State",
"type": "tango",
},
"State",
True,
)
hwo_shutter.add_command(
{
"name": "Open",
"type": "tango",
},
"Open",
True,
)
hwo_shutter.add_command(
{
"name": "Close",
"type": "tango",
},
"Close",
True,
)

hwo_shutter.init()

yield hwo_shutter

tangods_test_context.stop()
tangods_test_context.join()


def _wait_until(condition: Callable, condition_desc: str):
with Timeout(1.2, Exception(f"timed out while waiting for {condition_desc}")):
while not condition():
gevent.sleep(0.01)


def test_open(shutter):
"""
test opening the shutter
"""
dev = DeviceProxy(shutter.tangoname)

assert dev.State() == DevState.CLOSE
assert not shutter.is_open

shutter.open()

_wait_until(lambda: shutter.is_open, "shutter to open")
assert dev.State() == DevState.OPEN


def test_close(shutter):
"""
test closing the shutter
"""
dev = DeviceProxy(shutter.tangoname)
dev.Open()

assert dev.State() == DevState.OPEN
assert shutter.is_open

shutter.close()

_wait_until(lambda: not shutter.is_open, "shutter to close")
assert dev.State() == DevState.CLOSE
Loading