From 54ebbd45a8084f664c21be270fae0f7a1a658355 Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Thu, 16 May 2024 17:52:28 +0200 Subject: [PATCH] Add support for using dot (.) to get subobjects from the OD (#426) --- canopen/objectdictionary/__init__.py | 3 +++ doc/od.rst | 3 ++- doc/sdo.rst | 5 ++++- test/test_od.py | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index a3bcc527..19196951 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -105,6 +105,9 @@ def __getitem__( """Get object from object dictionary by name or index.""" item = self.names.get(index) or self.indices.get(index) if item is None: + if isinstance(index, str) and '.' in index: + idx, sub = index.split('.', maxsplit=1) + return self[idx][sub] name = "0x%X" % index if isinstance(index, int) else index raise KeyError("%s was not found in Object Dictionary" % name) return item diff --git a/doc/od.rst b/doc/od.rst index 6e7eb3b5..f31c7813 100644 --- a/doc/od.rst +++ b/doc/od.rst @@ -48,7 +48,8 @@ You can access the objects using either index/subindex or names:: device_name_obj = node.object_dictionary['ManufacturerDeviceName'] vendor_id_obj = node.object_dictionary[0x1018][1] - + actual_speed = node.object_dictionary['ApplicationStatus.ActualSpeed'] + command_all = node.object_dictionary['ApplicationCommands.CommandAll'] API --- diff --git a/doc/sdo.rst b/doc/sdo.rst index 7b06118e..8634d28c 100644 --- a/doc/sdo.rst +++ b/doc/sdo.rst @@ -30,11 +30,14 @@ Examples -------- SDO objects can be accessed using the ``.sdo`` member which works like a Python -dictionary. Indexes and subindexes can be identified by either name or number. +dictionary. Indexes can be identified by either name or number. +There are two ways to idenity subindexes, either by using the index and subindex +as separate arguments or by using a combined syntax using a dot. The code below only creates objects, no messages are sent or received yet:: # Complex records command_all = node.sdo['ApplicationCommands']['CommandAll'] + command_all = node.sdo['ApplicationCommands.CommandAll'] actual_speed = node.sdo['ApplicationStatus']['ActualSpeed'] control_mode = node.sdo['ApplicationSetupParameters']['RequestedControlMode'] diff --git a/test/test_od.py b/test/test_od.py index a5f00985..9c25bfcb 100644 --- a/test/test_od.py +++ b/test/test_od.py @@ -136,6 +136,20 @@ def test_add_array(self): self.assertEqual(test_od["Test Array"], array) self.assertEqual(test_od[0x1002], array) + def test_get_item_dot(self): + test_od = od.ObjectDictionary() + array = od.ODArray("Test Array", 0x1000) + last_subindex = od.ODVariable("Last subindex", 0x1000, 0) + last_subindex.data_type = od.UNSIGNED8 + member1 = od.ODVariable("Test Variable", 0x1000, 1) + member2 = od.ODVariable("Test Variable 2", 0x1000, 2) + array.add_member(last_subindex) + array.add_member(member1) + array.add_member(member2) + test_od.add_object(array) + self.assertEqual(test_od["Test Array.Last subindex"], last_subindex) + self.assertEqual(test_od["Test Array.Test Variable"], member1) + self.assertEqual(test_od["Test Array.Test Variable 2"], member2) class TestArray(unittest.TestCase):