Skip to content

Commit 0f13cfb

Browse files
authored
Export Eds or Dcf (#254)
1 parent c46228f commit 0f13cfb

File tree

7 files changed

+399
-13
lines changed

7 files changed

+399
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ target/
7070
\.project
7171

7272
\.pydevproject
73+
74+
*.kdev4
75+
*.kate-swp

canopen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .network import Network, NodeScanner
22
from .node import RemoteNode, LocalNode
33
from .sdo import SdoCommunicationError, SdoAbortedError
4-
from .objectdictionary import import_od, ObjectDictionary, ObjectDictionaryError
4+
from .objectdictionary import import_od, export_od, ObjectDictionary, ObjectDictionaryError
55
from .profiles.p402 import BaseNode402
66
try:
77
from ._version import version as __version__

canopen/objectdictionary/__init__.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@
1414
logger = logging.getLogger(__name__)
1515

1616

17+
def export_od(od, dest:Union[str,TextIO,None]=None, doc_type:Optional[str]=None):
18+
""" Export :class: ObjectDictionary to a file.
19+
20+
:param od:
21+
:class: ObjectDictionary object to be exported
22+
:param dest:
23+
export destination. filename, or file-like object or None.
24+
if None, the document is returned as string
25+
:param doc_type: type of document to export.
26+
If a filename is given for dest, this default to the file extension.
27+
Otherwise, this defaults to "eds"
28+
:rtype: str or None
29+
"""
30+
31+
doctypes = {"eds", "dcf"}
32+
if type(dest) is str:
33+
if doc_type is None:
34+
for t in doctypes:
35+
if dest.endswith(f".{t}"):
36+
doc_type = t
37+
break
38+
39+
if doc_type is None:
40+
doc_type = "eds"
41+
dest = open(dest, 'w')
42+
assert doc_type in doctypes
43+
44+
if doc_type == "eds":
45+
from . import eds
46+
return eds.export_eds(od, dest)
47+
elif doc_type == "dcf":
48+
from . import eds
49+
return eds.export_dcf(od, dest)
50+
51+
1752
def import_od(
1853
source: Union[str, TextIO, None],
1954
node_id: Optional[int] = None,
@@ -54,10 +89,13 @@ class ObjectDictionary(MutableMapping):
5489
def __init__(self):
5590
self.indices = {}
5691
self.names = {}
92+
self.comments = ""
5793
#: Default bitrate if specified by file
5894
self.bitrate: Optional[int] = None
5995
#: Node ID if specified by file
6096
self.node_id: Optional[int] = None
97+
#: Some information about the device
98+
self.device_information = DeviceInformation()
6199

62100
def __getitem__(
63101
self, index: Union[int, str]
@@ -280,6 +318,9 @@ def __init__(self, name: str, index: int, subindex: int = 0):
280318
self.bit_definitions: Dict[str, List[int]] = {}
281319
#: Storage location of index
282320
self.storage_location = None
321+
#: Can this variable be mapped to a PDO
322+
self.pdo_mappable = False
323+
283324

284325
def __eq__(self, other: "Variable") -> bool:
285326
return (self.index == other.index and
@@ -418,5 +459,24 @@ def encode_bits(self, original_value: int, bits: List[int], bit_value: int):
418459
return temp
419460

420461

462+
class DeviceInformation:
463+
def __init__(self):
464+
self.allowed_baudrates = set()
465+
self.vendor_name:Optional[str] = None
466+
self.vendor_number:Optional[int] = None
467+
self.product_name:Optional[str] = None
468+
self.product_number:Optional[int] = None
469+
self.revision_number:Optional[int] = None
470+
self.order_code:Optional[str] = None
471+
self.simple_boot_up_master:Optional[bool] = None
472+
self.simple_boot_up_slave:Optional[bool] = None
473+
self.granularity:Optional[int] = None
474+
self.dynamic_channels_supported:Optional[bool] = None
475+
self.group_messaging:Optional[bool] = None
476+
self.nr_of_RXPDO:Optional[bool] = None
477+
self.nr_of_TXPDO:Optional[bool] = None
478+
self.LSS_supported:Optional[bool] = None
479+
480+
421481
class ObjectDictionaryError(Exception):
422482
"""Unsupported operation with the current Object Dictionary."""

0 commit comments

Comments
 (0)