Skip to content

Commit 1f2a3f4

Browse files
committed
Minor housekeeping updates
* Manual merge in improments from master * Remove opinionated set_*() and get_*() for non-async calls * Comment updates
1 parent 67420a1 commit 1f2a3f4

File tree

17 files changed

+168
-185
lines changed

17 files changed

+168
-185
lines changed

canopen/emcy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def on_emcy(self, can_id, data, timestamp):
3333
code, register, data = EMCY_STRUCT.unpack(data)
3434
entry = EmcyError(code, register, data, timestamp)
3535

36-
# NOTE: Blocking call
36+
# NOTE: Blocking lock
3737
with self.emcy_received:
3838
if code & 0xFF00 == 0:
3939
# Error reset
@@ -95,7 +95,7 @@ def wait(
9595
"""
9696
end_time = time.time() + timeout
9797
while True:
98-
# NOTE: Blocking call
98+
# NOTE: Blocking lock
9999
with self.emcy_received:
100100
prev_log_size = len(self.log)
101101
# NOTE: Blocking call

canopen/lss.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import time
44
import struct
55
import asyncio
6-
try:
7-
import queue
8-
except ImportError:
9-
import Queue as queue
6+
import queue
107

118
from canopen.async_guard import ensure_not_async
129

@@ -376,6 +373,7 @@ def __send_configure(self, req_cs, value1=0, value2=0):
376373
raise LssError(error_msg)
377374

378375
# FIXME: Make async implementation "__asend_command"
376+
379377
@ensure_not_async # NOTE: Safeguard for accidental async use
380378
def __send_command(self, message):
381379
"""Send a LSS operation code to the network

canopen/network.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from __future__ import annotations
2-
try:
3-
from collections.abc import MutableMapping
4-
except ImportError:
5-
from collections import MutableMapping
2+
from collections.abc import MutableMapping
63
import logging
74
import threading
85
from typing import Callable, Dict, Iterable, List, Optional, Union, TYPE_CHECKING
@@ -42,8 +39,8 @@ class Network(MutableMapping):
4239

4340
def __init__(
4441
self,
45-
bus: can.BusABC | None = None,
46-
loop: AbstractEventLoop | None = None
42+
bus: Optional[BusABC] = None,
43+
loop: Optional[AbstractEventLoop] = None
4744
):
4845
"""
4946
:param can.BusABC bus:

canopen/nmt.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ def state(self) -> str:
101101

102102
@state.setter
103103
def state(self, new_state: str):
104-
logger.warning("Accessing NmtBase.state setter is deprecated, use set_state()")
105-
self.set_state(new_state)
106-
107-
def set_state(self, new_state: str):
108104
if new_state in NMT_COMMANDS:
109105
code = NMT_COMMANDS[new_state]
110106
else:
@@ -129,7 +125,7 @@ def __init__(self, node_id: int):
129125
@ensure_not_async # NOTE: Safeguard for accidental async use
130126
def on_heartbeat(self, can_id, data, timestamp):
131127
# NOTE: Callback. Called from another thread unless async
132-
# NOTE: Blocking call
128+
# NOTE: Blocking lock
133129
with self.state_update:
134130
self.timestamp = timestamp
135131
new_state, = struct.unpack_from("B", data)
@@ -180,7 +176,7 @@ def send_command(self, code: int):
180176
@ensure_not_async # NOTE: Safeguard for accidental async use
181177
def wait_for_heartbeat(self, timeout: float = 10):
182178
"""Wait until a heartbeat message is received."""
183-
# NOTE: Blocking call
179+
# NOTE: Blocking lock
184180
with self.state_update:
185181
self._state_received = None
186182
# NOTE: Blocking call
@@ -205,7 +201,7 @@ def wait_for_bootup(self, timeout: float = 10) -> None:
205201
end_time = time.time() + timeout
206202
while True:
207203
now = time.time()
208-
# NOTE: Blocking call
204+
# NOTE: Blocking lock
209205
with self.state_update:
210206
self._state_received = None
211207
# NOTE: Blocking call
@@ -286,7 +282,7 @@ def send_command(self, code: int) -> None:
286282
# between INITIALIZING and PRE-OPERATIONAL state
287283
if old_state == 0 and self._state == 127:
288284
# NOTE: Blocking - OK. Protected in SdoClient
289-
heartbeat_time_ms = self._local_node.sdo[0x1017].get_raw()
285+
heartbeat_time_ms = self._local_node.sdo[0x1017].raw
290286
self.start_heartbeat(heartbeat_time_ms)
291287
else:
292288
self.update_heartbeat()

canopen/node/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TextIO, Union, TYPE_CHECKING
1+
from typing import TextIO, Union, Optional, TYPE_CHECKING
22

33
from canopen.objectdictionary import ObjectDictionary, import_od
44

@@ -21,7 +21,7 @@ def __init__(
2121
node_id: int,
2222
object_dictionary: Union[ObjectDictionary, str, TextIO],
2323
):
24-
self.network: Network | None = None
24+
self.network: Optional[Network] = None
2525

2626
if not isinstance(object_dictionary, ObjectDictionary):
2727
object_dictionary = import_od(object_dictionary, node_id)

canopen/node/remote.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22
import logging
3-
from typing import Union, TextIO, TYPE_CHECKING
3+
from typing import Union, TextIO, List, TYPE_CHECKING
44

55
from canopen.sdo import SdoClient, SdoCommunicationError, SdoAbortedError
66
from canopen.nmt import NmtMaster
@@ -39,7 +39,7 @@ def __init__(
3939
#: Enable WORKAROUND for reversed PDO mapping entries
4040
self.curtis_hack = False
4141

42-
self.sdo_channels: list[SdoClient] = []
42+
self.sdo_channels: List[SdoClient] = []
4343
self.sdo = self.add_sdo(0x600 + self.id, 0x580 + self.id)
4444
self.tpdo = TPDO(self)
4545
self.rpdo = RPDO(self)
@@ -147,11 +147,11 @@ def __load_configuration_helper(self, index, subindex, name, value):
147147
subindex=subindex,
148148
name=name,
149149
value=value)))
150-
# NOTE: Blocking - OK. Protected in SdoClient
151-
self.sdo[index][subindex].set_raw(value)
150+
# NOTE: Blocking call - OK. Protected in SdoClient
151+
self.sdo[index][subindex].raw = value
152152
else:
153-
# FIXME: Blocking - OK. Protected in SdoClient
154-
self.sdo[index].set_raw(value)
153+
# NOTE: Blocking call - OK. Protected in SdoClient
154+
self.sdo[index].raw = value
155155
logger.info(str('SDO [{index:#06x}]: {name}: {value:#06x}'.format(
156156
index=index,
157157
name=name,

canopen/objectdictionary/__init__.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"""
44
import struct
55
from typing import Dict, Iterable, List, Optional, TextIO, Union
6-
try:
7-
from collections.abc import MutableMapping, Mapping
8-
except ImportError:
9-
from collections import MutableMapping, Mapping
6+
from collections.abc import MutableMapping, Mapping
107
import logging
118

129
from canopen.objectdictionary.datatypes import *
@@ -183,7 +180,7 @@ def __init__(self, name: str, index: int):
183180
self.names = {}
184181

185182
def __repr__(self) -> str:
186-
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04x}>"
183+
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}>"
187184

188185
def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
189186
item = self.names.get(subindex) or self.subindices.get(subindex)
@@ -242,7 +239,7 @@ def __init__(self, name: str, index: int):
242239
self.names = {}
243240

244241
def __repr__(self) -> str:
245-
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04x}>"
242+
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}>"
246243

247244
def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
248245
var = self.names.get(subindex) or self.subindices.get(subindex)
@@ -340,11 +337,13 @@ def __init__(self, name: str, index: int, subindex: int = 0):
340337
self.pdo_mappable = False
341338

342339
def __repr__(self) -> str:
343-
suffix = f":{self.subindex:02x}" if isinstance(self.parent, (ODRecord, ODArray)) else ""
344-
return f"<{type(self).__qualname__} {self.qualname!r} at 0x{self.index:04x}{suffix}>"
340+
suffix = f":{self.subindex:02X}" if isinstance(self.parent, (ODRecord, ODArray)) else ""
341+
return f"<{type(self).__qualname__} {self.qualname!r} at 0x{self.index:04X}{suffix}>"
345342

346343
@property
347344
def qualname(self) -> str:
345+
"""Fully qualified name of the variable. If the variable is a subindex
346+
of a record or array, the name will be prefixed with the parent's name."""
348347
if isinstance(self.parent, (ODRecord, ODArray)):
349348
return f"{self.parent.name}.{self.name}"
350349
return self.name

canopen/objectdictionary/eds.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import copy
22
import logging
33
import re
4-
5-
try:
6-
from configparser import RawConfigParser, NoOptionError, NoSectionError
7-
except ImportError:
8-
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
4+
from configparser import RawConfigParser, NoOptionError, NoSectionError
95

106
from canopen import objectdictionary
117
from canopen.objectdictionary import ObjectDictionary, datatypes

canopen/objectdictionary/epf.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
try:
2-
import xml.etree.cElementTree as etree
3-
except ImportError:
4-
import xml.etree.ElementTree as etree
1+
import xml.etree.ElementTree as etree
52
import logging
63

74
from canopen import objectdictionary

canopen/pdo/base.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
import threading
33
import math
44
from typing import Callable, Dict, Iterable, List, Optional, Union, TYPE_CHECKING
5-
try:
6-
from collections.abc import Mapping
7-
except ImportError:
8-
from collections import Mapping
5+
from collections.abc import Mapping
96
import logging
107
import binascii
118
import asyncio
@@ -140,7 +137,7 @@ def stop(self):
140137
pdo_map.stop()
141138

142139

143-
class PdoMaps(Mapping[int, "PdoMap"]):
140+
class PdoMaps(Mapping):
144141
"""A collection of transmit or receive maps."""
145142

146143
def __init__(self, com_offset, map_offset, pdo_node: PdoBase, cob_base=None):
@@ -212,7 +209,7 @@ def __init__(self, pdo_node: PdoBase, com_record, map_array):
212209
self._task = None
213210

214211
def __repr__(self) -> str:
215-
return f"<{type(self).__qualname__} {self.name!r} at COB-ID 0x{self.cob_id}>"
212+
return f"<{type(self).__qualname__} {self.name!r} at COB-ID 0x{self.cob_id:X}>"
216213

217214
def __getitem_by_index(self, value):
218215
valid_values = []
@@ -316,7 +313,7 @@ def on_message(self, can_id, data, timestamp):
316313
# NOTE: Callback. Called from another thread unless async
317314
is_transmitting = self._task is not None
318315
if can_id == self.cob_id and not is_transmitting:
319-
# NOTE: Blocking call
316+
# NOTE: Blocking lock
320317
with self.receive_condition:
321318
self.is_received = True
322319
self.data = data
@@ -413,8 +410,8 @@ def read(self, from_od=False) -> None:
413410
value = var.od.default
414411
else:
415412
# Get value from SDO
416-
# NOTE: Blocking
417-
value = var.get_raw()
413+
# NOTE: Blocking call
414+
value = var.raw
418415
try:
419416
# Deliver value into read_generator and wait for next object
420417
var = gen.send(value)
@@ -507,10 +504,10 @@ def save(self) -> None:
507504
for sdo, value in self.save_generator():
508505
if value == '@@get':
509506
# NOTE: Sync implementation of the WORKAROUND in save_generator()
510-
# NOTE: Blocking
511-
self._fill_map(sdo.get_raw())
507+
# NOTE: Blocking call
508+
self._fill_map(sdo.raw)
512509
else:
513-
# NOTE: Blocking
510+
# NOTE: Blocking call
514511
sdo.set_raw(value)
515512

516513
async def asave(self) -> None:
@@ -630,7 +627,7 @@ def wait_for_reception(self, timeout: float = 10) -> float:
630627
:param float timeout: Max time to wait in seconds.
631628
:return: Timestamp of message received or None if timeout.
632629
"""
633-
# NOTE: Blocking call
630+
# NOTE: Blocking lock
634631
with self.receive_condition:
635632
self.is_received = False
636633
# NOTE: Blocking call
@@ -692,7 +689,7 @@ def get_data(self) -> bytes:
692689
return data
693690

694691
async def aget_data(self) -> bytes:
695-
# As long as get_data() is not making any IO, it can be called
692+
# Since get_data() is not making any IO, it can be called
696693
# directly with no special async variant
697694
return self.get_data()
698695

@@ -730,7 +727,7 @@ def set_data(self, data: bytes):
730727
self.pdo_parent.update()
731728

732729
async def aset_data(self, data: bytes):
733-
# As long as get_data() is not making any IO, it can be called
730+
# Since get_data() is not making any IO, it can be called
734731
# directly with no special async variant
735732
return self.set_data(data)
736733

0 commit comments

Comments
 (0)