Skip to content

Commit efaa298

Browse files
committed
[tools/xmlparser] Add parse_cardinality method
1 parent 06e53bd commit efaa298

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

odml/tools/xmlparser.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,43 @@
4040
"""
4141

4242

43+
def parse_cardinality(val):
44+
"""
45+
Parses an odml specific cardinality from a string.
46+
47+
If the string content is valid, returns an appropriate tuple.
48+
Returns None if the string is empty or the content cannot be
49+
properly parsed.
50+
51+
:param val: string
52+
:return: None or 2-tuple
53+
"""
54+
if not val:
55+
return None
56+
57+
# Remove parenthesis and split on comma
58+
parsed_vals = val.strip()[1:-1].split(",")
59+
if len(parsed_vals) == 2:
60+
min_val = parsed_vals[0].strip()
61+
max_val = parsed_vals[1].strip()
62+
63+
min_int = min_val.isdigit() and int(min_val) >= 0
64+
max_int = max_val.isdigit() and int(max_val) >= 0
65+
66+
if min_int and max_int and int(max_val) > int(min_val):
67+
return int(min_val), int(max_val)
68+
69+
if min_int and max_val == "None":
70+
return int(min_val), None
71+
72+
if max_int and min_val == "None":
73+
return None, int(max_val)
74+
75+
# Todo we were not able to properly parse the current cardinality
76+
# add an appropriate Error/Warning
77+
return None
78+
79+
4380
def to_csv(val):
4481
"""
4582
Modifies odML values for serialization to strings and files.
@@ -410,6 +447,9 @@ def parse_tag(self, root, fmt, insert_children=True):
410447
if tag == "values" and curr_text:
411448
content = from_csv(node.text)
412449
arguments[tag] = content
450+
# Special handling of cardinality
451+
elif tag.endswith("_cardinality") and curr_text:
452+
arguments[tag] = parse_cardinality(node.text)
413453
else:
414454
arguments[tag] = curr_text
415455
else:

0 commit comments

Comments
 (0)