Skip to content

Commit 04d1766

Browse files
committed
[tools/dicparser] Handle sec cardinality tuples
1 parent 62a47de commit 04d1766

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

odml/tools/dict_parser.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ def get_sections(self, section_list):
114114
section_dict[attr] = sections
115115
else:
116116
tag = getattr(section, attr)
117-
118-
if tag:
117+
# Tuples have to be serialized as lists to avoid
118+
# nasty python code annotations when writing to yaml.
119+
if tag and isinstance(tag, tuple):
120+
section_dict[i] = list(tag)
121+
elif tag:
119122
# Always use the arguments key attribute name when saving
120123
section_dict[i] = tag
121124

@@ -143,6 +146,8 @@ def get_properties(props_list):
143146

144147
if hasattr(prop, attr):
145148
tag = getattr(prop, attr)
149+
# Tuples have to be serialized as lists to avoid
150+
# nasty python code annotations when writing to yaml.
146151
if isinstance(tag, tuple):
147152
prop_dict[attr] = list(tag)
148153
elif (tag == []) or tag: # Even if 'values' is empty, allow '[]'
@@ -266,8 +271,14 @@ def parse_sections(self, section_list):
266271
elif attr == 'sections':
267272
children_secs = self.parse_sections(section['sections'])
268273
elif attr:
274+
# Tuples had to be serialized as lists to support the yaml format.
275+
# Now convert cardinality lists back to tuples.
276+
content = section[attr]
277+
if attr.endswith("_cardinality"):
278+
content = parse_cardinality(content)
279+
269280
# Make sure to always use the correct odml format attribute name
270-
sec_attrs[odmlfmt.Section.map(attr)] = section[attr]
281+
sec_attrs[odmlfmt.Section.map(attr)] = content
271282

272283
sec = odmlfmt.Section.create(**sec_attrs)
273284
for prop in sec_props:
@@ -297,6 +308,8 @@ def parse_properties(self, props_list):
297308
attr = self.is_valid_attribute(i, odmlfmt.Property)
298309
if attr:
299310
content = _property[attr]
311+
# Tuples had to be serialized as lists to support the yaml format.
312+
# Now convert cardinality lists back to tuples.
300313
if attr.endswith("_cardinality"):
301314
content = parse_cardinality(content)
302315

0 commit comments

Comments
 (0)