Skip to content

Commit a816a98

Browse files
committed
tango command: use attribute_query() to check if attribute exists
Use `DeviceProxy.attribute_query()` method to check if an attribute exist. This way, we don't need look in the list of all device attributes. This list can potentionally be quite long. Move the code for checking if an attribute exists to it's own dedicated function, to improve code readability.
1 parent 24030ca commit a816a98

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

mxcubecore/Command/Tango.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ def __init__(self, event):
143143
self.event = event
144144

145145

146+
def _device_has_attribute(device: DeviceProxy, attribute_name: str) -> bool:
147+
"""Check if a tango device has an attribute."""
148+
149+
try:
150+
device.attribute_query(attribute_name)
151+
except PyTango.DevFailed as ex:
152+
if ex.args[0].reason == "API_AttrNotFound":
153+
# query failed with 'attribute not found' error
154+
return False
155+
156+
# unexpected exception, re-raise
157+
raise
158+
159+
# attribute query was successful, thus we know the attribute exits
160+
return True
161+
162+
146163
class TangoChannel(ChannelObject):
147164
_tangoEventsQueue = queue.Queue()
148165
_eventReceivers = {}
@@ -246,9 +263,7 @@ def init_device(self):
246263
self.device.set_timeout_millis(self.timeout)
247264

248265
# check that the attribute exists (to avoid Abort in PyTango grrr)
249-
if not self.attribute_name.lower() in [
250-
attr.name.lower() for attr in self.device.attribute_list_query()
251-
]:
266+
if not _device_has_attribute(self.device, self.attribute_name):
252267
logging.getLogger("HWR").error(
253268
"no attribute %s in Tango device %s",
254269
self.attribute_name,

ruff.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ convention = "google"
171171
"ARG002",
172172
"B904",
173173
"E501",
174-
"E713",
175174
"FBT003",
176175
"FIX002",
177176
"ICN001",

test/test_setup_commands_channels.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,6 @@
1717
from mxcubecore.protocols_config import setup_commands_channels
1818

1919

20-
@dataclass
21-
class _MockedAttribute:
22-
name: str
23-
24-
25-
class _MockedDeviceProxy:
26-
def __init__(self, _name):
27-
pass
28-
29-
def ping(self):
30-
pass
31-
32-
def set_timeout_millis(self, _timeout):
33-
pass
34-
35-
def attribute_list_query(self):
36-
return [
37-
_MockedAttribute("simple"),
38-
_MockedAttribute("tango_attr"),
39-
_MockedAttribute("red"),
40-
_MockedAttribute("green"),
41-
_MockedAttribute("cyan"),
42-
]
43-
44-
4520
@dataclass
4621
class _MockedEpicsCommand:
4722
pv_name: str
@@ -163,7 +138,7 @@ def test_tango_commands_channels(test_hwo):
163138

164139
config = _parse_yaml_config("tango_commands_channels.yaml")
165140

166-
with mock.patch("mxcubecore.Command.Tango.DeviceProxy", _MockedDeviceProxy):
141+
with mock.patch("mxcubecore.Command.Tango.DeviceProxy"):
167142
setup_commands_channels(test_hwo, config)
168143

169144
expected_channels = {
@@ -186,7 +161,7 @@ def test_tango_commands(test_hwo):
186161

187162
config = _parse_yaml_config("tango_commands.yaml")
188163

189-
with mock.patch("mxcubecore.Command.Tango.DeviceProxy", _MockedDeviceProxy):
164+
with mock.patch("mxcubecore.Command.Tango.DeviceProxy"):
190165
setup_commands_channels(test_hwo, config)
191166

192167
# there should be no channels
@@ -207,7 +182,7 @@ def test_tango_channels(test_hwo):
207182

208183
config = _parse_yaml_config("tango_channels.yaml")
209184

210-
with mock.patch("mxcubecore.Command.Tango.DeviceProxy", _MockedDeviceProxy):
185+
with mock.patch("mxcubecore.Command.Tango.DeviceProxy"):
211186
setup_commands_channels(test_hwo, config)
212187

213188
# there should be no commands
@@ -232,7 +207,7 @@ def test_tango_duo(test_hwo):
232207

233208
config = _parse_yaml_config("tango_duo.yaml")
234209

235-
with mock.patch("mxcubecore.Command.Tango.DeviceProxy", _MockedDeviceProxy):
210+
with mock.patch("mxcubecore.Command.Tango.DeviceProxy"):
236211
setup_commands_channels(test_hwo, config)
237212

238213
expected_channels = {

0 commit comments

Comments
 (0)