|
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, 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 |
@@ -251,6 +254,24 @@ def _revert_variable(var_type, value): |
251 | 254 | else: |
252 | 255 | return f"0x{value:02X}" |
253 | 256 |
|
| 257 | +_STANDARD_OPTIONS = { |
| 258 | + "ObjectType", "ParameterName", "DataType", "AccessType", |
| 259 | + "PDOMapping", "LowLimit", "HighLimit", "DefaultValue", |
| 260 | + "ParameterValue", "Factor", "Description", "Unit", |
| 261 | + "StorageLocation", "CompactSubObj", |
| 262 | + # CiA 306 fields parsed explicitly: |
| 263 | + "SubNumber", |
| 264 | + # ObjFlags and Denotation are intentionally absent here so they are |
| 265 | + # preserved via custom_options until proper support is added in #654. |
| 266 | +} |
| 267 | + |
| 268 | +def _get_custom_options(eds, section): |
| 269 | + custom_options = {} |
| 270 | + for option, value in eds.items(section): |
| 271 | + if option not in _STANDARD_OPTIONS: |
| 272 | + custom_options[option] = value |
| 273 | + return custom_options |
| 274 | + |
254 | 275 |
|
255 | 276 | def build_variable(eds, section, node_id, index, subindex=0): |
256 | 277 | """Creates a object dictionary entry. |
@@ -330,6 +351,8 @@ def build_variable(eds, section, node_id, index, subindex=0): |
330 | 351 | var.unit = eds.get(section, "Unit") |
331 | 352 | except ValueError: |
332 | 353 | pass |
| 354 | + |
| 355 | + var.custom_options = _get_custom_options(eds, section) |
333 | 356 | return var |
334 | 357 |
|
335 | 358 |
|
@@ -404,12 +427,17 @@ def export_variable(var, eds): |
404 | 427 | if getattr(var, 'unit', '') != '': |
405 | 428 | eds.set(section, "Unit", var.unit) |
406 | 429 |
|
| 430 | + for option, value in var.custom_options.items(): |
| 431 | + eds.set(section, option, str(value)) |
| 432 | + |
407 | 433 | def export_record(var, eds): |
408 | 434 | section = f"{var.index:04X}" |
409 | 435 | export_common(var, eds, section) |
410 | 436 | eds.set(section, "SubNumber", f"0x{len(var.subindices):X}") |
411 | 437 | ot = objectcodes.RECORD if isinstance(var, ODRecord) else objectcodes.ARRAY |
412 | 438 | eds.set(section, "ObjectType", f"0x{ot:X}") |
| 439 | + for option, value in var.custom_options.items(): |
| 440 | + eds.set(section, option, str(value)) |
413 | 441 | for i in var: |
414 | 442 | export_variable(var[i], eds) |
415 | 443 |
|
|
0 commit comments