Skip to content

Commit d56b226

Browse files
committed
Cast value to the appropriate type
1 parent 4887e32 commit d56b226

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

gedcom7/cast.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
from __future__ import annotations
44

5+
import logging
56
import re
67
from collections.abc import Callable
78

89
from . import const, grammar, types
910

11+
logger = logging.getLogger(__name__)
12+
1013

1114
def cast_value(text: str, type_id: str) -> types.DataType | None:
1215
if not text:
1316
return None
1417
payload = const.payloads.get(type_id)
1518
if not payload:
19+
logger.warning("Encountered unknown data type %s", type_id)
1620
return None
1721
cast_fuction = CAST_FUNCTIONS.get(payload)
1822
if not cast_fuction:
@@ -146,7 +150,11 @@ def _cast_date_period(value: str) -> types.DatePeriod:
146150
if to_date_match:
147151
res["to"] = types.Date(
148152
calendar=to_date_match.group("calendar"),
149-
day=int(to_date_match.group("day")) if to_date_match.group("day") else None,
153+
day=(
154+
int(to_date_match.group("day"))
155+
if to_date_match.group("day")
156+
else None
157+
),
150158
month=to_date_match.group("month"),
151159
year=int(to_date_match.group("year")),
152160
epoch=to_date_match.group("epoch"),
@@ -156,7 +164,11 @@ def _cast_date_period(value: str) -> types.DatePeriod:
156164
if from_date_match:
157165
res["from_"] = types.Date(
158166
calendar=from_date_match.group("calendar"),
159-
day=int(from_date_match.group("day")) if from_date_match.group("day") else None,
167+
day=(
168+
int(from_date_match.group("day"))
169+
if from_date_match.group("day")
170+
else None
171+
),
160172
month=from_date_match.group("month"),
161173
year=int(from_date_match.group("year")),
162174
epoch=from_date_match.group("epoch"),

gedcom7/parser.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""GEDCOM 7 parser."""
22

3-
43
import re
54
from typing import Dict, List
65

7-
from . import const, grammar
6+
from . import cast, const, grammar
87
from .types import GedcomStructure
98

109

@@ -39,6 +38,10 @@ def loads(string: str) -> List[GedcomStructure]:
3938
if level > 0:
4039
parent = context[level - 1]
4140
structure.parent = parent
41+
structure.value = cast.cast_value(
42+
text=structure.text, type_id=structure.type_id
43+
)
44+
if level > 0:
4245
parent.children.append(structure)
4346
else:
4447
records.append(structure)

gedcom7/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class GedcomStructure:
1818
xref: str
1919
children: list["GedcomStructure"] = field(default_factory=list)
2020
parent: "GedcomStructure | None" = None
21+
value: "DataType" | None = None
2122

2223
@property
2324
def type_id(self) -> str:

0 commit comments

Comments
 (0)