Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_subindexes(self):
array = od.Array("Test Array", 0x1000)
last_subindex = od.Variable("Last subindex", 0x1000, 0)
last_subindex.data_type = od.UNSIGNED8
array.add_member(last_subindex)
array.add_member(od.Variable("Test Variable", 0x1000, 1))
array.add_member(od.Variable("Test Variable 2", 0x1000, 2))
self.assertEqual(array[0].name, "Last subindex")
self.assertEqual(array[1].name, "Test Variable")
self.assertEqual(array[2].name, "Test Variable 2")
self.assertEqual(array[3].name, "Test Variable_3")
def test_array_compact_subobj(self):
array = self.od[0x1003]
self.assertIsInstance(array, canopen.objectdictionary.Array)
self.assertEqual(array.index, 0x1003)
self.assertEqual(array.name, 'Pre-defined error field')
var = array[5]
self.assertIsInstance(var, canopen.objectdictionary.Variable)
self.assertEqual(var.name, 'Pre-defined error field_5')
self.assertEqual(var.index, 0x1003)
self.assertEqual(var.subindex, 5)
self.assertEqual(var.data_type, canopen.objectdictionary.UNSIGNED32)
self.assertEqual(var.access_type, 'ro')
def test_add_array(self):
test_od = od.ObjectDictionary()
array = od.Array("Test Array", 0x1002)
array.add_member(od.Variable("Last subindex", 0x1002, 0))
test_od.add_object(array)
self.assertEqual(test_od["Test Array"], array)
self.assertEqual(test_od[0x1002], array)
# "ObjectType=0x7" (=VAR).
object_type = VAR
if object_type in (VAR, DOMAIN):
var = build_variable(eds, section, node_id, index)
od.add_object(var)
elif object_type == ARR and eds.has_option(section, "CompactSubObj"):
arr = objectdictionary.Array(name, index)
last_subindex = objectdictionary.Variable(
"Number of entries", index, 0)
last_subindex.data_type = objectdictionary.UNSIGNED8
arr.add_member(last_subindex)
arr.add_member(build_variable(eds, section, node_id, index, 1))
od.add_object(arr)
elif object_type == ARR:
arr = objectdictionary.Array(name, index)
od.add_object(arr)
elif object_type == RECORD:
record = objectdictionary.Record(name, index)
od.add_object(record)
continue
# Match subindexes
match = re.match(r"^([0-9A-Fa-f]{4})[S|s]ub([0-9A-Fa-f]+)$", section)
if match is not None:
index = int(match.group(1), 16)
subindex = int(match.group(2), 16)
entry = od[index]
if isinstance(entry, (objectdictionary.Record,
objectdictionary.Array)):
var = build_variable(eds, section, node_id, index, subindex)
def __getitem__(self, index):
entry = self.od[index]
if isinstance(entry, objectdictionary.Variable):
return Variable(self, entry)
elif isinstance(entry, objectdictionary.Array):
return Array(self, entry)
elif isinstance(entry, objectdictionary.Record):
return Record(self, entry)
def __init__(self, od):
self.od = od
#: Description of this variable from Object Dictionary, overridable
self.name = od.name
if isinstance(od.parent, (objectdictionary.Record,
objectdictionary.Array)):
# Include the parent object's name for subentries
self.name = od.parent.name + "." + od.name
#: Holds a local, overridable copy of the Object Index
self.index = od.index
#: Holds a local, overridable copy of the Object Subindex
self.subindex = od.subindex
# Parse Object Dictionary
for group_tree in tree.iterfind("Dictionary/Parameters/Group"):
name = group_tree.get("SymbolName")
parameters = group_tree.findall("Parameter")
index = int(parameters[0].get("Index"), 0)
if len(parameters) == 1:
# Simple variable
var = build_variable(parameters[0])
# Use top level index name instead
var.name = name
od.add_object(var)
elif len(parameters) == 2 and parameters[1].get("ObjectType") == "ARRAY":
# Array
arr = objectdictionary.Array(name, index)
for par_tree in parameters:
var = build_variable(par_tree)
arr.add_member(var)
description = group_tree.find("Description")
if description is not None:
arr.description = description.text
od.add_object(arr)
else:
# Complex record
record = objectdictionary.Record(name, index)
for par_tree in parameters:
var = build_variable(par_tree)
record.add_member(var)
description = group_tree.find("Description")
if description is not None:
record.description = description.text
if match is not None:
index = int(section, 16)
name = eds.get(section, "ParameterName")
try:
object_type = int(eds.get(section, "ObjectType"), 0)
except NoOptionError:
# DS306 4.6.3.2 object description
# If the keyword ObjectType is missing, this is regarded as
# "ObjectType=0x7" (=VAR).
object_type = VAR
if object_type in (VAR, DOMAIN):
var = build_variable(eds, section, node_id, index)
od.add_object(var)
elif object_type == ARR and eds.has_option(section, "CompactSubObj"):
arr = objectdictionary.Array(name, index)
last_subindex = objectdictionary.Variable(
"Number of entries", index, 0)
last_subindex.data_type = objectdictionary.UNSIGNED8
arr.add_member(last_subindex)
arr.add_member(build_variable(eds, section, node_id, index, 1))
od.add_object(arr)
elif object_type == ARR:
arr = objectdictionary.Array(name, index)
od.add_object(arr)
elif object_type == RECORD:
record = objectdictionary.Record(name, index)
od.add_object(record)
continue
# Match subindexes