1
1
"""
2
2
Object Dictionary module
3
3
"""
4
+ from __future__ import annotations
5
+
4
6
import struct
5
- from typing import Dict , Iterable , List , Optional , TextIO , Union
7
+ from typing import Dict , Iterator , List , Optional , TextIO , Union
6
8
from collections .abc import MutableMapping , Mapping
7
9
import logging
8
10
13
15
logger = logging .getLogger (__name__ )
14
16
15
17
16
- def export_od (od , dest :Union [str ,TextIO ,None ]= None , doc_type :Optional [str ]= None ):
18
+ def export_od (od , dest : Union [str , TextIO , None ] = None , doc_type : Optional [str ] = None ):
17
19
""" Export :class: ObjectDictionary to a file.
18
20
19
21
:param od:
@@ -55,7 +57,7 @@ def export_od(od, dest:Union[str,TextIO,None]=None, doc_type:Optional[str]=None)
55
57
def import_od (
56
58
source : Union [str , TextIO , None ],
57
59
node_id : Optional [int ] = None ,
58
- ) -> " ObjectDictionary" :
60
+ ) -> ObjectDictionary :
59
61
"""Parse an EDS, DCF, or EPF file.
60
62
61
63
:param source:
@@ -102,7 +104,7 @@ def __init__(self):
102
104
103
105
def __getitem__ (
104
106
self , index : Union [int , str ]
105
- ) -> Union [" ODArray" , " ODRecord" , " ODVariable" ]:
107
+ ) -> Union [ODArray , ODRecord , ODVariable ]:
106
108
"""Get object from object dictionary by name or index."""
107
109
item = self .names .get (index ) or self .indices .get (index )
108
110
if item is None :
@@ -113,7 +115,7 @@ def __getitem__(
113
115
return item
114
116
115
117
def __setitem__ (
116
- self , index : Union [int , str ], obj : Union [" ODArray" , " ODRecord" , " ODVariable" ]
118
+ self , index : Union [int , str ], obj : Union [ODArray , ODRecord , ODVariable ]
117
119
):
118
120
assert index == obj .index or index == obj .name
119
121
self .add_object (obj )
@@ -123,7 +125,7 @@ def __delitem__(self, index: Union[int, str]):
123
125
del self .indices [obj .index ]
124
126
del self .names [obj .name ]
125
127
126
- def __iter__ (self ) -> Iterable [int ]:
128
+ def __iter__ (self ) -> Iterator [int ]:
127
129
return iter (sorted (self .indices ))
128
130
129
131
def __len__ (self ) -> int :
@@ -132,7 +134,7 @@ def __len__(self) -> int:
132
134
def __contains__ (self , index : Union [int , str ]):
133
135
return index in self .names or index in self .indices
134
136
135
- def add_object (self , obj : Union [" ODArray" , " ODRecord" , " ODVariable" ]) -> None :
137
+ def add_object (self , obj : Union [ODArray , ODRecord , ODVariable ]) -> None :
136
138
"""Add object to the object dictionary.
137
139
138
140
:param obj:
@@ -147,7 +149,7 @@ def add_object(self, obj: Union["ODArray", "ODRecord", "ODVariable"]) -> None:
147
149
148
150
def get_variable (
149
151
self , index : Union [int , str ], subindex : int = 0
150
- ) -> Optional [" ODVariable" ]:
152
+ ) -> Optional [ODVariable ]:
151
153
"""Get the variable object at specified index (and subindex if applicable).
152
154
153
155
:return: ODVariable if found, else `None`
@@ -182,13 +184,13 @@ def __init__(self, name: str, index: int):
182
184
def __repr__ (self ) -> str :
183
185
return f"<{ type (self ).__qualname__ } { self .name !r} at { pretty_index (self .index )} >"
184
186
185
- def __getitem__ (self , subindex : Union [int , str ]) -> " ODVariable" :
187
+ def __getitem__ (self , subindex : Union [int , str ]) -> ODVariable :
186
188
item = self .names .get (subindex ) or self .subindices .get (subindex )
187
189
if item is None :
188
190
raise KeyError (f"Subindex { pretty_index (None , subindex )} was not found" )
189
191
return item
190
192
191
- def __setitem__ (self , subindex : Union [int , str ], var : " ODVariable" ):
193
+ def __setitem__ (self , subindex : Union [int , str ], var : ODVariable ):
192
194
assert subindex == var .subindex
193
195
self .add_member (var )
194
196
@@ -200,16 +202,16 @@ def __delitem__(self, subindex: Union[int, str]):
200
202
def __len__ (self ) -> int :
201
203
return len (self .subindices )
202
204
203
- def __iter__ (self ) -> Iterable [int ]:
205
+ def __iter__ (self ) -> Iterator [int ]:
204
206
return iter (sorted (self .subindices ))
205
207
206
208
def __contains__ (self , subindex : Union [int , str ]) -> bool :
207
209
return subindex in self .names or subindex in self .subindices
208
210
209
- def __eq__ (self , other : " ODRecord" ) -> bool :
211
+ def __eq__ (self , other : ODRecord ) -> bool :
210
212
return self .index == other .index
211
213
212
- def add_member (self , variable : " ODVariable" ) -> None :
214
+ def add_member (self , variable : ODVariable ) -> None :
213
215
"""Adds a :class:`~canopen.objectdictionary.ODVariable` to the record."""
214
216
variable .parent = self
215
217
self .subindices [variable .subindex ] = variable
@@ -241,7 +243,7 @@ def __init__(self, name: str, index: int):
241
243
def __repr__ (self ) -> str :
242
244
return f"<{ type (self ).__qualname__ } { self .name !r} at { pretty_index (self .index )} >"
243
245
244
- def __getitem__ (self , subindex : Union [int , str ]) -> " ODVariable" :
246
+ def __getitem__ (self , subindex : Union [int , str ]) -> ODVariable :
245
247
var = self .names .get (subindex ) or self .subindices .get (subindex )
246
248
if var is not None :
247
249
# This subindex is defined
@@ -264,13 +266,13 @@ def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
264
266
def __len__ (self ) -> int :
265
267
return len (self .subindices )
266
268
267
- def __iter__ (self ) -> Iterable [int ]:
269
+ def __iter__ (self ) -> Iterator [int ]:
268
270
return iter (sorted (self .subindices ))
269
271
270
- def __eq__ (self , other : " ODArray" ) -> bool :
272
+ def __eq__ (self , other : ODArray ) -> bool :
271
273
return self .index == other .index
272
274
273
- def add_member (self , variable : " ODVariable" ) -> None :
275
+ def add_member (self , variable : ODVariable ) -> None :
274
276
"""Adds a :class:`~canopen.objectdictionary.ODVariable` to the record."""
275
277
variable .parent = self
276
278
self .subindices [variable .subindex ] = variable
@@ -348,7 +350,7 @@ def qualname(self) -> str:
348
350
return f"{ self .parent .name } .{ self .name } "
349
351
return self .name
350
352
351
- def __eq__ (self , other : " ODVariable" ) -> bool :
353
+ def __eq__ (self , other : ODVariable ) -> bool :
352
354
return (self .index == other .index and
353
355
self .subindex == other .subindex )
354
356
0 commit comments