|
22 | 22 |
|
23 | 23 | logger = logging.getLogger(__name__) |
24 | 24 |
|
| 25 | + |
25 | 26 | def import_eds(source, node_id): |
26 | 27 | eds = RawConfigParser(inline_comment_prefixes=(';',)) |
27 | 28 | eds.optionxform = str |
@@ -133,20 +134,22 @@ def import_eds(source, node_id): |
133 | 134 | od.add_object(var) |
134 | 135 | elif object_type == objectcodes.ARRAY and eds.has_option(section, "CompactSubObj"): |
135 | 136 | arr = ODArray(name, index) |
136 | | - last_subindex = ODVariable( |
137 | | - "Number of entries", index, 0) |
| 137 | + last_subindex = ODVariable("Number of entries", index, 0) |
138 | 138 | last_subindex.data_type = datatypes.UNSIGNED8 |
139 | 139 | arr.add_member(last_subindex) |
140 | 140 | arr.add_member(build_variable(eds, section, node_id, object_type, index, 1)) |
141 | 141 | arr.storage_location = storage_location |
| 142 | + arr.custom_options = _get_custom_options(eds, section) |
142 | 143 | od.add_object(arr) |
143 | 144 | elif object_type == objectcodes.ARRAY: |
144 | 145 | arr = ODArray(name, index) |
145 | 146 | arr.storage_location = storage_location |
| 147 | + arr.custom_options = _get_custom_options(eds, section) |
146 | 148 | od.add_object(arr) |
147 | 149 | elif object_type == objectcodes.RECORD: |
148 | 150 | record = ODRecord(name, index) |
149 | 151 | record.storage_location = storage_location |
| 152 | + record.custom_options = _get_custom_options(eds, section) |
150 | 153 | od.add_object(record) |
151 | 154 |
|
152 | 155 | continue |
@@ -257,6 +260,24 @@ def _revert_variable(var_type, value): |
257 | 260 | else: |
258 | 261 | return f"0x{value:02X}" |
259 | 262 |
|
| 263 | +_STANDARD_OPTIONS = { |
| 264 | + "ObjectType", "ParameterName", "DataType", "AccessType", |
| 265 | + "PDOMapping", "LowLimit", "HighLimit", "DefaultValue", |
| 266 | + "ParameterValue", "Factor", "Description", "Unit", |
| 267 | + "StorageLocation", "CompactSubObj", |
| 268 | + # CiA 306 fields parsed explicitly: |
| 269 | + "SubNumber", |
| 270 | + # ObjFlags and Denotation are intentionally absent here so they are |
| 271 | + # preserved via custom_options until proper support is added in #654. |
| 272 | +} |
| 273 | + |
| 274 | +def _get_custom_options(eds, section): |
| 275 | + custom_options = {} |
| 276 | + for option, value in eds.items(section): |
| 277 | + if option not in _STANDARD_OPTIONS: |
| 278 | + custom_options[option] = value |
| 279 | + return custom_options |
| 280 | + |
260 | 281 |
|
261 | 282 | def build_variable( |
262 | 283 | eds: RawConfigParser, |
@@ -350,6 +371,8 @@ def build_variable( |
350 | 371 | var.unit = eds.get(section, "Unit") |
351 | 372 | except ValueError: |
352 | 373 | pass |
| 374 | + |
| 375 | + var.custom_options = _get_custom_options(eds, section) |
353 | 376 | return var |
354 | 377 |
|
355 | 378 |
|
@@ -425,12 +448,17 @@ def export_variable(var, eds): |
425 | 448 | if getattr(var, 'unit', '') != '': |
426 | 449 | eds.set(section, "Unit", var.unit) |
427 | 450 |
|
| 451 | + for option, value in var.custom_options.items(): |
| 452 | + eds.set(section, option, str(value)) |
| 453 | + |
428 | 454 | def export_record(var, eds): |
429 | 455 | section = f"{var.index:04X}" |
430 | 456 | export_common(var, eds, section) |
431 | 457 | eds.set(section, "SubNumber", f"0x{len(var.subindices):X}") |
432 | 458 | ot = objectcodes.RECORD if isinstance(var, ODRecord) else objectcodes.ARRAY |
433 | 459 | eds.set(section, "ObjectType", f"0x{ot:X}") |
| 460 | + for option, value in var.custom_options.items(): |
| 461 | + eds.set(section, option, str(value)) |
434 | 462 | for i in var: |
435 | 463 | export_variable(var[i], eds) |
436 | 464 |
|
|
0 commit comments