forked from DavidMStraub/python-gedcom7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
43 lines (39 loc) · 1.3 KB
/
parser.py
File metadata and controls
43 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""GEDCOM 7 parser."""
import re
from . import cast, const, grammar
from .types import GedcomStructure
def loads(string: str) -> list[GedcomStructure]:
"""Load from a string."""
context: dict[int, GedcomStructure] = {}
records: list[GedcomStructure] = []
ext: dict[str, str] = {}
for match in re.finditer(grammar.line, string):
data = match.groupdict()
level = int(data["level"])
linestr = data["linestr"] or ""
# handle continuation lines
if data["tag"] == const.CONT:
context[level - 1].text += "\n" + linestr
continue
structure = GedcomStructure(
tag=ext.get(data["tag"]) or data["tag"],
pointer=data["pointer"],
xref=data["xref"],
text=linestr,
)
# handle extension tags
if (
structure.tag == const.TAG
and context[0].tag == const.HEAD
and context[1].tag == const.SCHMA
):
tag_name, tag_uri = structure.text.split(" ")
ext[tag_name] = tag_uri
context[level] = structure
# append structure to output
if level > 0:
parent = context[level - 1]
parent.append_child(structure)
else:
records.append(structure)
return records