Skip to content

Commit a0f57ad

Browse files
committed
Make value a property, automatically set parent
1 parent e490053 commit a0f57ad

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

gedcom7/parser.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ def loads(string: str) -> list[GedcomStructure]:
3636
# append structure to output
3737
if level > 0:
3838
parent = context[level - 1]
39-
structure.parent = parent
40-
structure.value = cast.cast_value(
41-
text=structure.text, type_id=structure.type_id
42-
)
43-
if level > 0:
44-
parent.children.append(structure)
39+
parent.append_child(structure)
4540
else:
4641
records.append(structure)
4742
return records

gedcom7/types.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dataclasses import dataclass, field
66
from typing import Literal, Union
77

8-
from . import const
8+
from . import cast, const
99

1010

1111
@dataclass
@@ -18,7 +18,6 @@ class GedcomStructure:
1818
xref: str
1919
children: list["GedcomStructure"] = field(default_factory=list)
2020
parent: "GedcomStructure | None" = None
21-
value: "DataType" | None = None
2221

2322
@property
2423
def type_id(self) -> str:
@@ -33,6 +32,20 @@ def type_id(self) -> str:
3332
return const.substructures[""][self.tag]
3433
return const.substructures[self.parent.type_id][self.tag]
3534

35+
def __post_init__(self):
36+
"""Post-init steps: set parent on children."""
37+
for child in self.children:
38+
child.parent = self
39+
40+
def append_child(self, child: "GedcomStructure") -> None:
41+
"""Append a child to the structure and set the child's parent to self."""
42+
child.parent = self
43+
self.children.append(child)
44+
45+
@property
46+
def value(self) -> "DataType | None":
47+
return cast.cast_value(text=self.text, type_id=self.type_id)
48+
3649

3750
@dataclass
3851
class PersonalName:

0 commit comments

Comments
 (0)