From b72a42bde74d77c1400f12bea91c6065175a5af6 Mon Sep 17 00:00:00 2001 From: TIM ANDERSON Date: Wed, 18 Nov 2026 13:07:50 +1000 Subject: [PATCH] Keep FileInfo local to each EDS and DCF export --- canopen/objectdictionary/eds.py | 5 +++-- test/test_eds.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index 608024f3..cc853c44 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -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) @@ -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(): diff --git a/test/test_eds.py b/test/test_eds.py index 4241d1e8..e8b35b38 100644 --- a/test/test_eds.py +++ b/test/test_eds.py @@ -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 @@ -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]