|
40 | 40 | """ |
41 | 41 |
|
42 | 42 |
|
| 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 | + |
43 | 80 | def to_csv(val): |
44 | 81 | """ |
45 | 82 | Modifies odML values for serialization to strings and files. |
@@ -410,6 +447,9 @@ def parse_tag(self, root, fmt, insert_children=True): |
410 | 447 | if tag == "values" and curr_text: |
411 | 448 | content = from_csv(node.text) |
412 | 449 | arguments[tag] = content |
| 450 | + # Special handling of cardinality |
| 451 | + elif tag.endswith("_cardinality") and curr_text: |
| 452 | + arguments[tag] = parse_cardinality(node.text) |
413 | 453 | else: |
414 | 454 | arguments[tag] = curr_text |
415 | 455 | else: |
|
0 commit comments