|
| 1 | +"""Cast strings values to the appropriate data type.""" |
| 2 | + |
| 3 | +import re |
| 4 | +from collections.abc import Callable |
| 5 | + |
| 6 | +from . import const, types, grammar |
| 7 | + |
| 8 | + |
| 9 | +def cast_value(text: str, type_id: str) -> types.DataType | None: |
| 10 | + if not text: |
| 11 | + return None |
| 12 | + payload = const.payloads.get(type_id) |
| 13 | + if not payload: |
| 14 | + return None |
| 15 | + cast_fuction = CAST_FUNCTIONS.get(payload) |
| 16 | + if not cast_fuction: |
| 17 | + return text |
| 18 | + return cast_fuction(text) |
| 19 | + |
| 20 | + |
| 21 | +def _cast_bool(value: str) -> bool: |
| 22 | + """Cast a string to a boolean.""" |
| 23 | + if value == "Y": |
| 24 | + return True |
| 25 | + if not value: |
| 26 | + return False |
| 27 | + else: |
| 28 | + raise ValueError(f"Cannot interpret {value} as boolean") |
| 29 | + |
| 30 | + |
| 31 | +def _cast_integer(value: str) -> int: |
| 32 | + """Cast a string to an integer.""" |
| 33 | + try: |
| 34 | + return int(value) |
| 35 | + except ValueError: |
| 36 | + raise ValueError(f"Cannot interpret {value} as integer") |
| 37 | + |
| 38 | + |
| 39 | +def _cast_list_text(value: str) -> list[str]: |
| 40 | + """Cast a string to a list of strings.""" |
| 41 | + return [el.strip() for el in value.split(",")] |
| 42 | + |
| 43 | + |
| 44 | +def _match(text: str, regex: str, type_name: str) -> re.Match: |
| 45 | + """Match a string and raise if not compatible.""" |
| 46 | + match = re.fullmatch(regex, text) |
| 47 | + if not match: |
| 48 | + raise ValueError(f"Cannot interpret {text} as type {type_name}") |
| 49 | + return match |
| 50 | + |
| 51 | + |
| 52 | +def _cast_personal_name(value: str) -> types.PersonalName: |
| 53 | + """Cast a string to a PersonalName.""" |
| 54 | + match = _match(value, grammar.personalname, "PersonalName") |
| 55 | + return types.PersonalName( |
| 56 | + fullname=value.replace("/", ""), |
| 57 | + surname=match.group("surname"), |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def _cast_time(value: str) -> types.Time: |
| 62 | + """Cast a string to a Time.""" |
| 63 | + match = _match(value, grammar.time, "Time") |
| 64 | + return types.Time( |
| 65 | + tz=match.group("tz"), |
| 66 | + hour=int(match.group("hour")), |
| 67 | + minute=int(match.group("minute")), |
| 68 | + second=int(match.group("second")) if match.group("second") else None, |
| 69 | + fraction=int(match.group("fraction")) if match.group("fraction") else None, |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def _cast_age(value: str) -> types.Age: |
| 74 | + """Cast a string to an Age.""" |
| 75 | + match = _match(value, grammar.age, "Age") |
| 76 | + res = { |
| 77 | + "agebound": match.group("agebound"), |
| 78 | + "years": match.group("years"), |
| 79 | + "months": match.group("months1") or match.group("months2"), |
| 80 | + "weeks": match.group("weeks1") |
| 81 | + or match.group("weeks2") |
| 82 | + or match.group("weeks3"), |
| 83 | + "days": match.group("days1") |
| 84 | + or match.group("days2") |
| 85 | + or match.group("days3") |
| 86 | + or match.group("days4"), |
| 87 | + } |
| 88 | + return types.Age( |
| 89 | + agebound=res["agebound"], |
| 90 | + years=int(res["years"].rstrip("y")) if res["years"] else None, |
| 91 | + months=int(res["months"].rstrip("m")) if res["months"] else None, |
| 92 | + weeks=int(res["weeks"].rstrip("w")) if res["weeks"] else None, |
| 93 | + days=int(res["days"].rstrip("d")) if res["days"] else None, |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def _cast_enum(value: str) -> str: |
| 98 | + """Cast a string to an Enum.""" |
| 99 | + match = _match(value, grammar.enum, "Enum") |
| 100 | + return match.group(0) |
| 101 | + |
| 102 | + |
| 103 | +def _cast_list_enum(value: str) -> list[str]: |
| 104 | + """Cast a string to a list of Enums.""" |
| 105 | + match = _match(value, grammar.list_enum, "ListEnum") |
| 106 | + return [el.strip() for el in match.group(0).split(",")] |
| 107 | + |
| 108 | + |
| 109 | +def _cast_mediatype(value: str) -> types.MediaType: |
| 110 | + """Cast a string to a MediaType.""" |
| 111 | + match = _match(value, grammar.mediatype, "MediaType") |
| 112 | + return types.MediaType(media_type=match.group(0)) |
| 113 | + |
| 114 | + |
| 115 | +def _cast_date_exact(value: str) -> types.DateExact: |
| 116 | + """Cast a string to a DateExact.""" |
| 117 | + match = _match(value, grammar.dateexact, "DateExact") |
| 118 | + return types.DateExact( |
| 119 | + day=int(match.group("day")), |
| 120 | + month=match.group("month"), |
| 121 | + year=int(match.group("year")), |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def _cast_date(value: str) -> types.Date: |
| 126 | + """Cast a string to a Date.""" |
| 127 | + match = _match(value, grammar.date_capture, "Date") |
| 128 | + return types.Date( |
| 129 | + calendar=match.group("calendar"), |
| 130 | + day=int(match.group("day")) if match.group("day") else None, |
| 131 | + month=match.group("month"), |
| 132 | + year=int(match.group("year")), |
| 133 | + epoch=match.group("epoch"), |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def _cast_date_period(value: str) -> types.DatePeriod: |
| 138 | + """Cast a string to a DatePeriod.""" |
| 139 | + match = _match(value, grammar.dateperiod, "DatePeriod") |
| 140 | + res = {} |
| 141 | + if match.group("todate1") or match.group("todate2"): |
| 142 | + to_date = match.group("todate1") or match.group("todate2") |
| 143 | + to_date_match = re.fullmatch(grammar.date_capture, to_date) |
| 144 | + if to_date_match: |
| 145 | + res["to"] = types.Date( |
| 146 | + calendar=to_date_match.group("calendar"), |
| 147 | + day=int(to_date_match.group("day")) if to_date_match.group("day") else None, |
| 148 | + month=to_date_match.group("month"), |
| 149 | + year=int(to_date_match.group("year")), |
| 150 | + epoch=to_date_match.group("epoch"), |
| 151 | + ) |
| 152 | + if match.group("fromdate"): |
| 153 | + from_date_match = re.fullmatch(grammar.date_capture, match.group("fromdate")) |
| 154 | + if from_date_match: |
| 155 | + res["from_"] = types.Date( |
| 156 | + calendar=from_date_match.group("calendar"), |
| 157 | + day=int(from_date_match.group("day")) if from_date_match.group("day") else None, |
| 158 | + month=from_date_match.group("month"), |
| 159 | + year=int(from_date_match.group("year")), |
| 160 | + epoch=from_date_match.group("epoch"), |
| 161 | + ) |
| 162 | + return types.DatePeriod(**res) |
| 163 | + |
| 164 | + |
| 165 | +CAST_FUNCTIONS: dict[str, Callable[[str], types.DataType] | None] = { |
| 166 | + "Y|<NULL>": _cast_bool, |
| 167 | + "http://www.w3.org/2001/XMLSchema#Language": None, |
| 168 | + "http://www.w3.org/2001/XMLSchema#nonNegativeInteger": _cast_integer, |
| 169 | + "http://www.w3.org/2001/XMLSchema#string": None, |
| 170 | + "http://www.w3.org/ns/dcat#mediaType": _cast_mediatype, |
| 171 | + "https://gedcom.io/terms/v7/type-Age": _cast_age, |
| 172 | + "https://gedcom.io/terms/v7/type-Date": _cast_date, |
| 173 | + "https://gedcom.io/terms/v7/type-Date#exact": _cast_date_exact, |
| 174 | + "https://gedcom.io/terms/v7/type-Date#period": _cast_date_period, |
| 175 | + "https://gedcom.io/terms/v7/type-Enum": _cast_enum, |
| 176 | + "https://gedcom.io/terms/v7/type-List#Enum": _cast_list_enum, |
| 177 | + "https://gedcom.io/terms/v7/type-List#Text": _cast_list_text, |
| 178 | + "https://gedcom.io/terms/v7/type-Name": _cast_personal_name, |
| 179 | + "https://gedcom.io/terms/v7/type-Time": _cast_time, |
| 180 | +} |
0 commit comments