Skip to content

Commit 9e303f2

Browse files
authored
Import cleanups (#362)
* Use absolute `canopen` instead of relative imports * Ensure canopen imports after system imports * Fix isinstance bug in TPDO.stop() * Change const from objectdictionary.* to datatypes.* in objectdictionary.eds.py for consistency * Save typing by using ObjectDictionary instead of objectdictionary.ObjectDictionary
1 parent 9cdfe9b commit 9e303f2

File tree

19 files changed

+91
-98
lines changed

19 files changed

+91
-98
lines changed

canopen/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from .network import Network, NodeScanner
2-
from .node import RemoteNode, LocalNode
3-
from .sdo import SdoCommunicationError, SdoAbortedError
4-
from .objectdictionary import import_od, export_od, ObjectDictionary, ObjectDictionaryError
5-
from .profiles.p402 import BaseNode402
1+
from canopen.network import Network, NodeScanner
2+
from canopen.node import RemoteNode, LocalNode
3+
from canopen.sdo import SdoCommunicationError, SdoAbortedError
4+
from canopen.objectdictionary import import_od, export_od, ObjectDictionary, ObjectDictionaryError
5+
from canopen.profiles.p402 import BaseNode402
66
try:
7-
from ._version import version as __version__
7+
from canopen._version import version as __version__
88
except ImportError:
99
# package is not installed
1010
__version__ = "unknown"

canopen/network.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
class Listener:
1818
""" Dummy listener """
1919

20-
from .node import RemoteNode, LocalNode
21-
from .sync import SyncProducer
22-
from .timestamp import TimeProducer
23-
from .nmt import NmtMaster
24-
from .lss import LssMaster
25-
from .objectdictionary.eds import import_from_node
26-
from .objectdictionary import ObjectDictionary
20+
from canopen.node import RemoteNode, LocalNode
21+
from canopen.sync import SyncProducer
22+
from canopen.timestamp import TimeProducer
23+
from canopen.nmt import NmtMaster
24+
from canopen.lss import LssMaster
25+
from canopen.objectdictionary.eds import import_from_node
26+
from canopen.objectdictionary import ObjectDictionary
2727

2828
logger = logging.getLogger(__name__)
2929

canopen/nmt.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import time
55
from typing import Callable, Optional
66

7-
from .network import CanError
8-
97
logger = logging.getLogger(__name__)
108

119
NMT_STATES = {

canopen/node/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .remote import RemoteNode
2-
from .local import LocalNode
1+
from canopen.node.remote import RemoteNode
2+
from canopen.node.local import LocalNode

canopen/node/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import TextIO, Union
2-
from .. import objectdictionary
2+
from canopen.objectdictionary import ObjectDictionary, import_od
33

44

55
class BaseNode:
@@ -15,14 +15,12 @@ class BaseNode:
1515
def __init__(
1616
self,
1717
node_id: int,
18-
object_dictionary: Union[objectdictionary.ObjectDictionary, str, TextIO],
18+
object_dictionary: Union[ObjectDictionary, str, TextIO],
1919
):
2020
self.network = None
2121

22-
if not isinstance(object_dictionary,
23-
objectdictionary.ObjectDictionary):
24-
object_dictionary = objectdictionary.import_od(
25-
object_dictionary, node_id)
22+
if not isinstance(object_dictionary, ObjectDictionary):
23+
object_dictionary = import_od(object_dictionary, node_id)
2624
self.object_dictionary = object_dictionary
2725

2826
self.id = node_id or self.object_dictionary.node_id

canopen/node/local.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import logging
22
from typing import Dict, Union
33

4-
from .base import BaseNode
5-
from ..sdo import SdoServer, SdoAbortedError
6-
from ..pdo import PDO, TPDO, RPDO
7-
from ..nmt import NmtSlave
8-
from ..emcy import EmcyProducer
9-
from .. import objectdictionary
4+
from canopen.node.base import BaseNode
5+
from canopen.sdo import SdoServer, SdoAbortedError
6+
from canopen.pdo import PDO, TPDO, RPDO
7+
from canopen.nmt import NmtSlave
8+
from canopen.emcy import EmcyProducer
9+
from canopen.objectdictionary import ObjectDictionary
10+
from canopen import objectdictionary
1011

1112
logger = logging.getLogger(__name__)
1213

@@ -16,7 +17,7 @@ class LocalNode(BaseNode):
1617
def __init__(
1718
self,
1819
node_id: int,
19-
object_dictionary: Union[objectdictionary.ObjectDictionary, str],
20+
object_dictionary: Union[ObjectDictionary, str],
2021
):
2122
super(LocalNode, self).__init__(node_id, object_dictionary)
2223

canopen/node/remote.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import logging
22
from typing import Union, TextIO
33

4-
from ..sdo import SdoClient
5-
from ..nmt import NmtMaster
6-
from ..emcy import EmcyConsumer
7-
from ..pdo import TPDO, RPDO, PDO
8-
from ..objectdictionary import Record, Array, Variable
9-
from .base import BaseNode
10-
11-
import canopen
12-
13-
from canopen import objectdictionary
4+
from canopen.sdo import SdoClient, SdoCommunicationError, SdoAbortedError
5+
from canopen.nmt import NmtMaster
6+
from canopen.emcy import EmcyConsumer
7+
from canopen.pdo import TPDO, RPDO, PDO
8+
from canopen.objectdictionary import Record, Array, Variable, ObjectDictionary
9+
from canopen.node.base import BaseNode
1410

1511
logger = logging.getLogger(__name__)
1612

@@ -31,7 +27,7 @@ class RemoteNode(BaseNode):
3127
def __init__(
3228
self,
3329
node_id: int,
34-
object_dictionary: Union[objectdictionary.ObjectDictionary, str, TextIO],
30+
object_dictionary: Union[ObjectDictionary, str, TextIO],
3531
load_od: bool = False,
3632
):
3733
super(RemoteNode, self).__init__(node_id, object_dictionary)
@@ -138,9 +134,9 @@ def __load_configuration_helper(self, index, subindex, name, value):
138134
index=index,
139135
name=name,
140136
value=value)))
141-
except canopen.SdoCommunicationError as e:
137+
except SdoCommunicationError as e:
142138
logger.warning(str(e))
143-
except canopen.SdoAbortedError as e:
139+
except SdoAbortedError as e:
144140
# WORKAROUND for broken implementations: the SDO is set but the error
145141
# "Attempt to write a read-only object" is raised any way.
146142
if e.code != 0x06010002:

canopen/objectdictionary/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections import MutableMapping, Mapping
1010
import logging
1111

12-
from .datatypes import *
12+
from canopen.objectdictionary.datatypes import *
1313

1414
logger = logging.getLogger(__name__)
1515

@@ -42,10 +42,10 @@ def export_od(od, dest:Union[str,TextIO,None]=None, doc_type:Optional[str]=None)
4242
assert doc_type in doctypes
4343

4444
if doc_type == "eds":
45-
from . import eds
45+
from canopen.objectdictionary import eds
4646
return eds.export_eds(od, dest)
4747
elif doc_type == "dcf":
48-
from . import eds
48+
from canopen.objectdictionary import eds
4949
return eds.export_dcf(od, dest)
5050

5151
# If dest is opened in this fn, it should be closed
@@ -78,10 +78,10 @@ def import_od(
7878
filename = source
7979
suffix = filename[filename.rfind("."):].lower()
8080
if suffix in (".eds", ".dcf"):
81-
from . import eds
81+
from canopen.objectdictionary import eds
8282
return eds.import_eds(source, node_id)
8383
elif suffix == ".epf":
84-
from . import epf
84+
from canopen.objectdictionary import epf
8585
return epf.import_epf(source)
8686
else:
8787
raise NotImplementedError("No support for this format")

canopen/objectdictionary/eds.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import logging
33
import re
44

5-
from canopen.objectdictionary import datatypes
6-
75
try:
86
from configparser import RawConfigParser, NoOptionError, NoSectionError
97
except ImportError:
108
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
9+
1110
from canopen import objectdictionary
11+
from canopen.objectdictionary import ObjectDictionary, datatypes
1212
from canopen.sdo import SdoClient
1313

1414
logger = logging.getLogger(__name__)
@@ -38,7 +38,7 @@ def import_eds(source, node_id):
3838
if not hasattr(source, "read"):
3939
fp.close()
4040

41-
od = objectdictionary.ObjectDictionary()
41+
od = ObjectDictionary()
4242

4343
if eds.has_section("FileInfo"):
4444
od.__edsFileInfo = {
@@ -130,7 +130,7 @@ def import_eds(source, node_id):
130130
arr = objectdictionary.Array(name, index)
131131
last_subindex = objectdictionary.Variable(
132132
"Number of entries", index, 0)
133-
last_subindex.data_type = objectdictionary.UNSIGNED8
133+
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
@@ -179,7 +179,7 @@ def import_from_node(node_id, network):
179179
:param network: network object
180180
"""
181181
# Create temporary SDO client
182-
sdo_client = SdoClient(0x600 + node_id, 0x580 + node_id, objectdictionary.ObjectDictionary())
182+
sdo_client = SdoClient(0x600 + node_id, 0x580 + node_id, ObjectDictionary())
183183
sdo_client.network = network
184184
# Subscribe to SDO responses
185185
network.subscribe(0x580 + node_id, sdo_client.on_response)
@@ -219,11 +219,11 @@ def _signed_int_from_hex(hex_str, bit_length):
219219

220220

221221
def _convert_variable(node_id, var_type, value):
222-
if var_type in (objectdictionary.OCTET_STRING, objectdictionary.DOMAIN):
222+
if var_type in (datatypes.OCTET_STRING, datatypes.DOMAIN):
223223
return bytes.fromhex(value)
224-
elif var_type in (objectdictionary.VISIBLE_STRING, objectdictionary.UNICODE_STRING):
224+
elif var_type in (datatypes.VISIBLE_STRING, datatypes.UNICODE_STRING):
225225
return value
226-
elif var_type in objectdictionary.FLOAT_TYPES:
226+
elif var_type in datatypes.FLOAT_TYPES:
227227
return float(value)
228228
else:
229229
# COB-ID can contain '$NODEID+' so replace this with node_id before converting
@@ -237,11 +237,11 @@ def _convert_variable(node_id, var_type, value):
237237
def _revert_variable(var_type, value):
238238
if value is None:
239239
return None
240-
if var_type in (objectdictionary.OCTET_STRING, objectdictionary.DOMAIN):
240+
if var_type in (datatypes.OCTET_STRING, datatypes.DOMAIN):
241241
return bytes.hex(value)
242-
elif var_type in (objectdictionary.VISIBLE_STRING, objectdictionary.UNICODE_STRING):
242+
elif var_type in (datatypes.VISIBLE_STRING, datatypes.UNICODE_STRING):
243243
return value
244-
elif var_type in objectdictionary.FLOAT_TYPES:
244+
elif var_type in datatypes.FLOAT_TYPES:
245245
return value
246246
else:
247247
return "0x%02X" % value
@@ -273,14 +273,14 @@ def build_variable(eds, section, node_id, index, subindex=0):
273273
except NoSectionError:
274274
logger.warning("%s has an unknown or unsupported data type (%X)", name, var.data_type)
275275
# Assume DOMAIN to force application to interpret the byte data
276-
var.data_type = objectdictionary.DOMAIN
276+
var.data_type = datatypes.DOMAIN
277277

278278
var.pdo_mappable = bool(int(eds.get(section, "PDOMapping", fallback="0"), 0))
279279

280280
if eds.has_option(section, "LowLimit"):
281281
try:
282282
min_string = eds.get(section, "LowLimit")
283-
if var.data_type in objectdictionary.SIGNED_TYPES:
283+
if var.data_type in datatypes.SIGNED_TYPES:
284284
var.min = _signed_int_from_hex(min_string, _calc_bit_length(var.data_type))
285285
else:
286286
var.min = int(min_string, 0)
@@ -289,7 +289,7 @@ def build_variable(eds, section, node_id, index, subindex=0):
289289
if eds.has_option(section, "HighLimit"):
290290
try:
291291
max_string = eds.get(section, "HighLimit")
292-
if var.data_type in objectdictionary.SIGNED_TYPES:
292+
if var.data_type in datatypes.SIGNED_TYPES:
293293
var.max = _signed_int_from_hex(max_string, _calc_bit_length(var.data_type))
294294
else:
295295
var.max = int(max_string, 0)
@@ -341,7 +341,7 @@ def export_common(var, eds, section):
341341
eds.set(section, "StorageLocation", var.storage_location)
342342

343343
def export_variable(var, eds):
344-
if isinstance(var.parent, objectdictionary.ObjectDictionary):
344+
if isinstance(var.parent, ObjectDictionary):
345345
# top level variable
346346
section = "%04X" % var.index
347347
else:

canopen/objectdictionary/epf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
except ImportError:
44
import xml.etree.ElementTree as etree
55
import logging
6+
67
from canopen import objectdictionary
8+
from canopen.objectdictionary import ObjectDictionary
79

810
logger = logging.getLogger(__name__)
911

@@ -32,7 +34,7 @@ def import_epf(epf):
3234
The Object Dictionary.
3335
:rtype: canopen.ObjectDictionary
3436
"""
35-
od = objectdictionary.ObjectDictionary()
37+
od = ObjectDictionary()
3638
if etree.iselement(epf):
3739
tree = epf
3840
else:

0 commit comments

Comments
 (0)