Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions canopen/objectdictionary/eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ def copy_variable(eds, section, subindex, src_var):
return var


def export_dcf(od, dest=None, fileInfo={}):
def export_dcf(od, dest=None, fileInfo=None):
return export_eds(od, dest, fileInfo, True)


def export_eds(od, dest=None, file_info={}, device_commisioning=False):
def export_eds(od, dest=None, file_info=None, device_commisioning=False):
def export_object(obj, eds):
if isinstance(obj, ODVariable):
return export_variable(obj, eds)
Expand Down Expand Up @@ -507,6 +507,7 @@ def export_record(var, eds):
"EdsVersion": 4.2,
}

file_info = {} if file_info is None else file_info.copy()
file_info.setdefault("ModificationDate", defmtime.strftime("%m-%d-%Y"))
file_info.setdefault("ModificationTime", defmtime.strftime("%I:%m%p"))
for k, v in origFileInfo.items():
Expand Down
30 changes: 29 additions & 1 deletion test/test_eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from configparser import RawConfigParser

import canopen
from canopen.objectdictionary.eds import _signed_int_from_hex, build_variable
from canopen.objectdictionary.eds import (
_signed_int_from_hex,
build_variable,
export_dcf,
export_eds,
)
from canopen.utils import pretty_index

from .util import DATATYPES_EDS, SAMPLE_EDS, tmp_file
Expand Down Expand Up @@ -295,6 +300,29 @@ def test_export_without_raw_default_values(self):
with io.StringIO() as dest:
canopen.export_od(od, dest, 'eds')

def test_file_info_is_local_to_each_export(self):
for doctype in ("eds", "dcf"):
for source, filename, author in (
(SAMPLE_EDS, "test.dcf", "Christian Sandberg"),
(DATATYPES_EDS, "datatypes.eds", "objdictgen"),
):
with self.subTest(doctype=doctype, source=source), io.StringIO() as dest:
canopen.export_od(canopen.import_od(source), dest, doctype)
exported = RawConfigParser()
exported.read_string(dest.getvalue())
self.assertEqual(exported["FileInfo"]["FileName"], filename)
self.assertEqual(exported["FileInfo"]["CreatedBy"], author)

def test_file_info_overrides_are_preserved(self):
for export in (export_eds, export_dcf):
overrides = {"FileName": "custom.dcf"}
with self.subTest(export=export.__name__), io.StringIO() as dest:
export(self.od, dest, overrides)
exported = RawConfigParser()
exported.read_string(dest.getvalue())
self.assertEqual(exported["FileInfo"]["FileName"], "custom.dcf")
self.assertEqual(overrides, {"FileName": "custom.dcf"})

def test_reading_custom_options(self):
"""Custom options (unknown EDS keys) are collected in custom_options dict."""
var = self.od[0x3061]
Expand Down