Skip to content

Commit 0285f58

Browse files
authored
Rename Variable, Record and Array (#363) (#368)
To create better distinction between the different Variable, Record and Array types used for different purposes. Helps development, as IDE/linters often only display base name of class.
1 parent 3585837 commit 0285f58

File tree

15 files changed

+150
-128
lines changed

15 files changed

+150
-128
lines changed

canopen/node/local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _find_object(self, index, subindex):
120120
# Index does not exist
121121
raise SdoAbortedError(0x06020000)
122122
obj = self.object_dictionary[index]
123-
if not isinstance(obj, objectdictionary.Variable):
123+
if not isinstance(obj, objectdictionary.ODVariable):
124124
# Group or array
125125
if subindex not in obj:
126126
# Subindex does not exist

canopen/node/remote.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from canopen.nmt import NmtMaster
66
from canopen.emcy import EmcyConsumer
77
from canopen.pdo import TPDO, RPDO, PDO
8-
from canopen.objectdictionary import Record, Array, Variable, ObjectDictionary
8+
from canopen.objectdictionary import ODRecord, ODArray, ODVariable, ObjectDictionary
99
from canopen.node.base import BaseNode
1010

1111
logger = logging.getLogger(__name__)
@@ -148,10 +148,10 @@ def __load_configuration_helper(self, index, subindex, name, value):
148148
def load_configuration(self):
149149
''' Load the configuration of the node from the object dictionary.'''
150150
for obj in self.object_dictionary.values():
151-
if isinstance(obj, Record) or isinstance(obj, Array):
151+
if isinstance(obj, ODRecord) or isinstance(obj, ODArray):
152152
for subobj in obj.values():
153-
if isinstance(subobj, Variable) and subobj.writable and (subobj.value is not None):
153+
if isinstance(subobj, ODVariable) and subobj.writable and (subobj.value is not None):
154154
self.__load_configuration_helper(subobj.index, subobj.subindex, subobj.name, subobj.value)
155-
elif isinstance(obj, Variable) and obj.writable and (obj.value is not None):
155+
elif isinstance(obj, ODVariable) and obj.writable and (obj.value is not None):
156156
self.__load_configuration_helper(obj.index, None, obj.name, obj.value)
157157
self.pdo.read() # reads the new configuration from the driver

canopen/objectdictionary/__init__.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __init__(self):
103103

104104
def __getitem__(
105105
self, index: Union[int, str]
106-
) -> Union["Array", "Record", "Variable"]:
106+
) -> Union["ODArray", "ODRecord", "ODVariable"]:
107107
"""Get object from object dictionary by name or index."""
108108
item = self.names.get(index) or self.indices.get(index)
109109
if item is None:
@@ -112,7 +112,7 @@ def __getitem__(
112112
return item
113113

114114
def __setitem__(
115-
self, index: Union[int, str], obj: Union["Array", "Record", "Variable"]
115+
self, index: Union[int, str], obj: Union["ODArray", "ODRecord", "ODVariable"]
116116
):
117117
assert index == obj.index or index == obj.name
118118
self.add_object(obj)
@@ -131,35 +131,35 @@ def __len__(self) -> int:
131131
def __contains__(self, index: Union[int, str]):
132132
return index in self.names or index in self.indices
133133

134-
def add_object(self, obj: Union["Array", "Record", "Variable"]) -> None:
134+
def add_object(self, obj: Union["ODArray", "ODRecord", "ODVariable"]) -> None:
135135
"""Add object to the object dictionary.
136136
137137
:param obj:
138138
Should be either one of
139-
:class:`~canopen.objectdictionary.Variable`,
140-
:class:`~canopen.objectdictionary.Record`, or
141-
:class:`~canopen.objectdictionary.Array`.
139+
:class:`~canopen.objectdictionary.ODVariable`,
140+
:class:`~canopen.objectdictionary.ODRecord`, or
141+
:class:`~canopen.objectdictionary.ODArray`.
142142
"""
143143
obj.parent = self
144144
self.indices[obj.index] = obj
145145
self.names[obj.name] = obj
146146

147147
def get_variable(
148148
self, index: Union[int, str], subindex: int = 0
149-
) -> Optional["Variable"]:
149+
) -> Optional["ODVariable"]:
150150
"""Get the variable object at specified index (and subindex if applicable).
151151
152-
:return: Variable if found, else `None`
152+
:return: ODVariable if found, else `None`
153153
"""
154154
obj = self.get(index)
155-
if isinstance(obj, Variable):
155+
if isinstance(obj, ODVariable):
156156
return obj
157-
elif isinstance(obj, (Record, Array)):
157+
elif isinstance(obj, (ODRecord, ODArray)):
158158
return obj.get(subindex)
159159

160160

161-
class Record(MutableMapping):
162-
"""Groups multiple :class:`~canopen.objectdictionary.Variable` objects using
161+
class ODRecord(MutableMapping):
162+
"""Groups multiple :class:`~canopen.objectdictionary.ODVariable` objects using
163163
subindices.
164164
"""
165165

@@ -178,13 +178,13 @@ def __init__(self, name: str, index: int):
178178
self.subindices = {}
179179
self.names = {}
180180

181-
def __getitem__(self, subindex: Union[int, str]) -> "Variable":
181+
def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
182182
item = self.names.get(subindex) or self.subindices.get(subindex)
183183
if item is None:
184184
raise KeyError("Subindex %s was not found" % subindex)
185185
return item
186186

187-
def __setitem__(self, subindex: Union[int, str], var: "Variable"):
187+
def __setitem__(self, subindex: Union[int, str], var: "ODVariable"):
188188
assert subindex == var.subindex
189189
self.add_member(var)
190190

@@ -202,18 +202,18 @@ def __iter__(self) -> Iterable[int]:
202202
def __contains__(self, subindex: Union[int, str]) -> bool:
203203
return subindex in self.names or subindex in self.subindices
204204

205-
def __eq__(self, other: "Record") -> bool:
205+
def __eq__(self, other: "ODRecord") -> bool:
206206
return self.index == other.index
207207

208-
def add_member(self, variable: "Variable") -> None:
209-
"""Adds a :class:`~canopen.objectdictionary.Variable` to the record."""
208+
def add_member(self, variable: "ODVariable") -> None:
209+
"""Adds a :class:`~canopen.objectdictionary.ODVariable` to the record."""
210210
variable.parent = self
211211
self.subindices[variable.subindex] = variable
212212
self.names[variable.name] = variable
213213

214214

215-
class Array(Mapping):
216-
"""An array of :class:`~canopen.objectdictionary.Variable` objects using
215+
class ODArray(Mapping):
216+
"""An array of :class:`~canopen.objectdictionary.ODVariable` objects using
217217
subindices.
218218
219219
Actual length of array must be read from the node using SDO.
@@ -234,7 +234,7 @@ def __init__(self, name: str, index: int):
234234
self.subindices = {}
235235
self.names = {}
236236

237-
def __getitem__(self, subindex: Union[int, str]) -> "Variable":
237+
def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
238238
var = self.names.get(subindex) or self.subindices.get(subindex)
239239
if var is not None:
240240
# This subindex is defined
@@ -243,7 +243,7 @@ def __getitem__(self, subindex: Union[int, str]) -> "Variable":
243243
# Create a new variable based on first array item
244244
template = self.subindices[1]
245245
name = "%s_%x" % (template.name, subindex)
246-
var = Variable(name, self.index, subindex)
246+
var = ODVariable(name, self.index, subindex)
247247
var.parent = self
248248
for attr in ("data_type", "unit", "factor", "min", "max", "default",
249249
"access_type", "description", "value_descriptions",
@@ -260,17 +260,17 @@ def __len__(self) -> int:
260260
def __iter__(self) -> Iterable[int]:
261261
return iter(sorted(self.subindices))
262262

263-
def __eq__(self, other: "Array") -> bool:
263+
def __eq__(self, other: "ODArray") -> bool:
264264
return self.index == other.index
265265

266-
def add_member(self, variable: "Variable") -> None:
267-
"""Adds a :class:`~canopen.objectdictionary.Variable` to the record."""
266+
def add_member(self, variable: "ODVariable") -> None:
267+
"""Adds a :class:`~canopen.objectdictionary.ODVariable` to the record."""
268268
variable.parent = self
269269
self.subindices[variable.subindex] = variable
270270
self.names[variable.name] = variable
271271

272272

273-
class Variable:
273+
class ODVariable:
274274
"""Simple variable."""
275275

276276
STRUCT_TYPES = {
@@ -289,8 +289,8 @@ class Variable:
289289

290290
def __init__(self, name: str, index: int, subindex: int = 0):
291291
#: The :class:`~canopen.ObjectDictionary`,
292-
#: :class:`~canopen.objectdictionary.Record` or
293-
#: :class:`~canopen.objectdictionary.Array` owning the variable
292+
#: :class:`~canopen.objectdictionary.ODRecord` or
293+
#: :class:`~canopen.objectdictionary.ODArray` owning the variable
294294
self.parent = None
295295
#: 16-bit address of the object in the dictionary
296296
self.index = index
@@ -328,7 +328,7 @@ def __init__(self, name: str, index: int, subindex: int = 0):
328328
self.pdo_mappable = False
329329

330330

331-
def __eq__(self, other: "Variable") -> bool:
331+
def __eq__(self, other: "ODVariable") -> bool:
332332
return (self.index == other.index and
333333
self.subindex == other.subindex)
334334

@@ -486,3 +486,9 @@ def __init__(self):
486486

487487
class ObjectDictionaryError(Exception):
488488
"""Unsupported operation with the current Object Dictionary."""
489+
490+
491+
# Compatibility for old names
492+
Record = ODRecord
493+
Array = ODArray
494+
Variable = ODVariable

canopen/objectdictionary/eds.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def import_eds(source, node_id):
101101
for i in range(1, 8):
102102
key = "Dummy%04d" % i
103103
if eds.getint(section, key) == 1:
104-
var = objectdictionary.Variable(key, i, 0)
104+
var = objectdictionary.ODVariable(key, i, 0)
105105
var.data_type = i
106106
var.access_type = "const"
107107
od.add_object(var)
@@ -127,20 +127,20 @@ def import_eds(source, node_id):
127127
var = build_variable(eds, section, node_id, index)
128128
od.add_object(var)
129129
elif object_type == ARR and eds.has_option(section, "CompactSubObj"):
130-
arr = objectdictionary.Array(name, index)
131-
last_subindex = objectdictionary.Variable(
130+
arr = objectdictionary.ODArray(name, index)
131+
last_subindex = objectdictionary.ODVariable(
132132
"Number of entries", index, 0)
133133
last_subindex.data_type = datatypes.UNSIGNED8
134134
arr.add_member(last_subindex)
135135
arr.add_member(build_variable(eds, section, node_id, index, 1))
136136
arr.storage_location = storage_location
137137
od.add_object(arr)
138138
elif object_type == ARR:
139-
arr = objectdictionary.Array(name, index)
139+
arr = objectdictionary.ODArray(name, index)
140140
arr.storage_location = storage_location
141141
od.add_object(arr)
142142
elif object_type == RECORD:
143-
record = objectdictionary.Record(name, index)
143+
record = objectdictionary.ODRecord(name, index)
144144
record.storage_location = storage_location
145145
od.add_object(record)
146146

@@ -152,8 +152,8 @@ def import_eds(source, node_id):
152152
index = int(match.group(1), 16)
153153
subindex = int(match.group(2), 16)
154154
entry = od[index]
155-
if isinstance(entry, (objectdictionary.Record,
156-
objectdictionary.Array)):
155+
if isinstance(entry, (objectdictionary.ODRecord,
156+
objectdictionary.ODArray)):
157157
var = build_variable(eds, section, node_id, index, subindex)
158158
entry.add_member(var)
159159

@@ -257,7 +257,7 @@ def build_variable(eds, section, node_id, index, subindex=0):
257257
:param subindex: Subindex of the CANOpen object (if presente, else 0)
258258
"""
259259
name = eds.get(section, "ParameterName")
260-
var = objectdictionary.Variable(name, index, subindex)
260+
var = objectdictionary.ODVariable(name, index, subindex)
261261
try:
262262
var.storage_location = eds.get(section, "StorageLocation")
263263
except NoOptionError:
@@ -344,11 +344,11 @@ def export_dcf(od, dest=None, fileInfo={}):
344344

345345
def export_eds(od, dest=None, file_info={}, device_commisioning=False):
346346
def export_object(obj, eds):
347-
if isinstance(obj, objectdictionary.Variable):
347+
if isinstance(obj, objectdictionary.ODVariable):
348348
return export_variable(obj, eds)
349-
if isinstance(obj, objectdictionary.Record):
349+
if isinstance(obj, objectdictionary.ODRecord):
350350
return export_record(obj, eds)
351-
if isinstance(obj, objectdictionary.Array):
351+
if isinstance(obj, objectdictionary.ODArray):
352352
return export_array(obj, eds)
353353

354354
def export_common(var, eds, section):
@@ -404,7 +404,7 @@ def export_record(var, eds):
404404
section = "%04X" % var.index
405405
export_common(var, eds, section)
406406
eds.set(section, "SubNumber", "0x%X" % len(var.subindices))
407-
ot = RECORD if isinstance(var, objectdictionary.Record) else ARR
407+
ot = RECORD if isinstance(var, objectdictionary.ODRecord) else ARR
408408
eds.set(section, "ObjectType", "0x%X" % ot)
409409
for i in var:
410410
export_variable(var[i], eds)

canopen/objectdictionary/epf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def import_epf(epf):
6161
od.add_object(var)
6262
elif len(parameters) == 2 and parameters[1].get("ObjectType") == "ARRAY":
6363
# Array
64-
arr = objectdictionary.Array(name, index)
64+
arr = objectdictionary.ODArray(name, index)
6565
for par_tree in parameters:
6666
var = build_variable(par_tree)
6767
arr.add_member(var)
@@ -71,7 +71,7 @@ def import_epf(epf):
7171
od.add_object(arr)
7272
else:
7373
# Complex record
74-
record = objectdictionary.Record(name, index)
74+
record = objectdictionary.ODRecord(name, index)
7575
for par_tree in parameters:
7676
var = build_variable(par_tree)
7777
record.add_member(var)
@@ -89,7 +89,7 @@ def build_variable(par_tree):
8989
name = par_tree.get("SymbolName")
9090
data_type = par_tree.get("DataType")
9191

92-
par = objectdictionary.Variable(name, index, subindex)
92+
par = objectdictionary.ODVariable(name, index, subindex)
9393
factor = par_tree.get("Factor", "1")
9494
par.factor = int(factor) if factor.isdigit() else float(factor)
9595
unit = par_tree.get("Unit")

canopen/pdo/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import logging
22

33
from canopen import node
4-
from canopen.pdo.base import PdoBase, Maps, Map, Variable
4+
from canopen.pdo.base import PdoBase, Maps
5+
6+
# Compatibility
7+
from .base import Variable
58

69
logger = logging.getLogger(__name__)
710

0 commit comments

Comments
 (0)