gedcom7 0.2.1__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gedcom7-0.2.1 → gedcom7-0.3.0}/PKG-INFO +4 -2
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7/__init__.py +1 -1
- gedcom7-0.3.0/gedcom7/cast.py +196 -0
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7/const.py +19 -1
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7/grammar.py +52 -13
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7/parser.py +9 -7
- gedcom7-0.3.0/gedcom7/types.py +140 -0
- gedcom7-0.3.0/gedcom7/util.py +60 -0
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7.egg-info/PKG-INFO +4 -2
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7.egg-info/SOURCES.txt +4 -1
- {gedcom7-0.2.1 → gedcom7-0.3.0}/setup.cfg +1 -0
- gedcom7-0.3.0/test/test_cast.py +338 -0
- gedcom7-0.3.0/test/test_util.py +204 -0
- gedcom7-0.2.1/gedcom7/types.py +0 -325
- gedcom7-0.2.1/test/test_types.py +0 -330
- {gedcom7-0.2.1 → gedcom7-0.3.0}/LICENSE +0 -0
- {gedcom7-0.2.1 → gedcom7-0.3.0}/README.md +0 -0
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7.egg-info/dependency_links.txt +0 -0
- {gedcom7-0.2.1 → gedcom7-0.3.0}/gedcom7.egg-info/top_level.txt +0 -0
- {gedcom7-0.2.1 → gedcom7-0.3.0}/pyproject.toml +0 -0
- {gedcom7-0.2.1 → gedcom7-0.3.0}/test/test_parser.py +0 -0
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: gedcom7
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: GEDCOM 7 parser
|
|
5
|
+
Home-page: https://github.com/DavidMStraub/python-gedcom7
|
|
5
6
|
Author: David M. Straub
|
|
6
7
|
Author-email: straub@protonmail.com
|
|
7
8
|
License: MIT
|
|
8
9
|
Description-Content-Type: text/markdown
|
|
9
10
|
License-File: LICENSE
|
|
11
|
+
Dynamic: license-file
|
|
10
12
|
|
|
11
13
|
# gedcom7
|
|
12
14
|
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""Cast strings values to the appropriate data type."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import re
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
|
|
9
|
+
from . import const, grammar, types
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def cast_value(text: str, type_id: str) -> types.DataType | None:
|
|
15
|
+
if not text:
|
|
16
|
+
return None
|
|
17
|
+
payload = const.payloads.get(type_id)
|
|
18
|
+
if not payload:
|
|
19
|
+
logger.warning("Encountered unknown data type %s", type_id)
|
|
20
|
+
return None
|
|
21
|
+
cast_fuction = CAST_FUNCTIONS.get(payload)
|
|
22
|
+
if not cast_fuction:
|
|
23
|
+
return text
|
|
24
|
+
return cast_fuction(text)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _cast_bool(value: str) -> bool:
|
|
28
|
+
"""Cast a string to a boolean."""
|
|
29
|
+
if value == "Y":
|
|
30
|
+
return True
|
|
31
|
+
if not value:
|
|
32
|
+
return False
|
|
33
|
+
else:
|
|
34
|
+
raise ValueError(f"Cannot interpret {value} as boolean")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _cast_integer(value: str) -> int:
|
|
38
|
+
"""Cast a string to an integer."""
|
|
39
|
+
try:
|
|
40
|
+
return int(value)
|
|
41
|
+
except ValueError:
|
|
42
|
+
raise ValueError(f"Cannot interpret {value} as integer")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _cast_list_text(value: str) -> list[str]:
|
|
46
|
+
"""Cast a string to a list of strings."""
|
|
47
|
+
return [el.strip() for el in value.split(",")]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _match(text: str, regex: str, type_name: str) -> re.Match:
|
|
51
|
+
"""Match a string and raise if not compatible."""
|
|
52
|
+
match = re.fullmatch(regex, text)
|
|
53
|
+
if not match:
|
|
54
|
+
raise ValueError(f"Cannot interpret {text} as type {type_name}")
|
|
55
|
+
return match
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _cast_personal_name(value: str) -> types.PersonalName:
|
|
59
|
+
"""Cast a string to a PersonalName."""
|
|
60
|
+
match = _match(value, grammar.personalname, "PersonalName")
|
|
61
|
+
return types.PersonalName(
|
|
62
|
+
fullname=value.replace("/", ""),
|
|
63
|
+
surname=match.group("surname").strip() if match.group("surname") else None,
|
|
64
|
+
given=match.group("given").strip() if match.group("given") else None,
|
|
65
|
+
suffix=match.group("suffix").strip() if match.group("suffix") else None,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _cast_time(value: str) -> types.Time:
|
|
70
|
+
"""Cast a string to a Time."""
|
|
71
|
+
match = _match(value, grammar.time, "Time")
|
|
72
|
+
return types.Time(
|
|
73
|
+
tz=match.group("tz"),
|
|
74
|
+
hour=int(match.group("hour")),
|
|
75
|
+
minute=int(match.group("minute")),
|
|
76
|
+
second=int(match.group("second")) if match.group("second") else None,
|
|
77
|
+
fraction=int(match.group("fraction")) if match.group("fraction") else None,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _cast_age(value: str) -> types.Age:
|
|
82
|
+
"""Cast a string to an Age."""
|
|
83
|
+
match = _match(value, grammar.age, "Age")
|
|
84
|
+
res = {
|
|
85
|
+
"agebound": match.group("agebound"),
|
|
86
|
+
"years": match.group("years"),
|
|
87
|
+
"months": match.group("months1") or match.group("months2"),
|
|
88
|
+
"weeks": match.group("weeks1")
|
|
89
|
+
or match.group("weeks2")
|
|
90
|
+
or match.group("weeks3"),
|
|
91
|
+
"days": match.group("days1")
|
|
92
|
+
or match.group("days2")
|
|
93
|
+
or match.group("days3")
|
|
94
|
+
or match.group("days4"),
|
|
95
|
+
}
|
|
96
|
+
return types.Age(
|
|
97
|
+
agebound=res["agebound"],
|
|
98
|
+
years=int(res["years"].rstrip("y")) if res["years"] else None,
|
|
99
|
+
months=int(res["months"].rstrip("m")) if res["months"] else None,
|
|
100
|
+
weeks=int(res["weeks"].rstrip("w")) if res["weeks"] else None,
|
|
101
|
+
days=int(res["days"].rstrip("d")) if res["days"] else None,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _cast_enum(value: str) -> str:
|
|
106
|
+
"""Cast a string to an Enum."""
|
|
107
|
+
match = _match(value, grammar.enum, "Enum")
|
|
108
|
+
return match.group(0)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _cast_list_enum(value: str) -> list[str]:
|
|
112
|
+
"""Cast a string to a list of Enums."""
|
|
113
|
+
match = _match(value, grammar.list_enum, "ListEnum")
|
|
114
|
+
return [el.strip() for el in match.group(0).split(",")]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _cast_mediatype(value: str) -> types.MediaType:
|
|
118
|
+
"""Cast a string to a MediaType."""
|
|
119
|
+
match = _match(value, grammar.mediatype, "MediaType")
|
|
120
|
+
return types.MediaType(media_type=match.group(0))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _cast_date_exact(value: str) -> types.DateExact:
|
|
124
|
+
"""Cast a string to a DateExact."""
|
|
125
|
+
match = _match(value, grammar.dateexact, "DateExact")
|
|
126
|
+
return types.DateExact(
|
|
127
|
+
day=int(match.group("day")),
|
|
128
|
+
month=match.group("month"),
|
|
129
|
+
year=int(match.group("year")),
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _cast_date(value: str) -> types.Date:
|
|
134
|
+
"""Cast a string to a Date."""
|
|
135
|
+
match = _match(value, grammar.date_capture, "Date")
|
|
136
|
+
return types.Date(
|
|
137
|
+
calendar=match.group("calendar"),
|
|
138
|
+
day=int(match.group("day")) if match.group("day") else None,
|
|
139
|
+
month=match.group("month"),
|
|
140
|
+
year=int(match.group("year")),
|
|
141
|
+
epoch=match.group("epoch"),
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _cast_date_period(value: str) -> types.DatePeriod:
|
|
146
|
+
"""Cast a string to a DatePeriod."""
|
|
147
|
+
match = _match(value, grammar.dateperiod, "DatePeriod")
|
|
148
|
+
res = {}
|
|
149
|
+
if match.group("todate1") or match.group("todate2"):
|
|
150
|
+
to_date = match.group("todate1") or match.group("todate2")
|
|
151
|
+
to_date_match = re.fullmatch(grammar.date_capture, to_date)
|
|
152
|
+
if to_date_match:
|
|
153
|
+
res["to"] = types.Date(
|
|
154
|
+
calendar=to_date_match.group("calendar"),
|
|
155
|
+
day=(
|
|
156
|
+
int(to_date_match.group("day"))
|
|
157
|
+
if to_date_match.group("day")
|
|
158
|
+
else None
|
|
159
|
+
),
|
|
160
|
+
month=to_date_match.group("month"),
|
|
161
|
+
year=int(to_date_match.group("year")),
|
|
162
|
+
epoch=to_date_match.group("epoch"),
|
|
163
|
+
)
|
|
164
|
+
if match.group("fromdate"):
|
|
165
|
+
from_date_match = re.fullmatch(grammar.date_capture, match.group("fromdate"))
|
|
166
|
+
if from_date_match:
|
|
167
|
+
res["from_"] = types.Date(
|
|
168
|
+
calendar=from_date_match.group("calendar"),
|
|
169
|
+
day=(
|
|
170
|
+
int(from_date_match.group("day"))
|
|
171
|
+
if from_date_match.group("day")
|
|
172
|
+
else None
|
|
173
|
+
),
|
|
174
|
+
month=from_date_match.group("month"),
|
|
175
|
+
year=int(from_date_match.group("year")),
|
|
176
|
+
epoch=from_date_match.group("epoch"),
|
|
177
|
+
)
|
|
178
|
+
return types.DatePeriod(**res)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
CAST_FUNCTIONS: dict[str, Callable[[str], types.DataType] | None] = {
|
|
182
|
+
"Y|<NULL>": _cast_bool,
|
|
183
|
+
"http://www.w3.org/2001/XMLSchema#Language": None,
|
|
184
|
+
"http://www.w3.org/2001/XMLSchema#nonNegativeInteger": _cast_integer,
|
|
185
|
+
"http://www.w3.org/2001/XMLSchema#string": None,
|
|
186
|
+
"http://www.w3.org/ns/dcat#mediaType": _cast_mediatype,
|
|
187
|
+
"https://gedcom.io/terms/v7/type-Age": _cast_age,
|
|
188
|
+
"https://gedcom.io/terms/v7/type-Date": _cast_date,
|
|
189
|
+
"https://gedcom.io/terms/v7/type-Date#exact": _cast_date_exact,
|
|
190
|
+
"https://gedcom.io/terms/v7/type-Date#period": _cast_date_period,
|
|
191
|
+
"https://gedcom.io/terms/v7/type-Enum": _cast_enum,
|
|
192
|
+
"https://gedcom.io/terms/v7/type-List#Enum": _cast_list_enum,
|
|
193
|
+
"https://gedcom.io/terms/v7/type-List#Text": _cast_list_text,
|
|
194
|
+
"https://gedcom.io/terms/v7/type-Name": _cast_personal_name,
|
|
195
|
+
"https://gedcom.io/terms/v7/type-Time": _cast_time,
|
|
196
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"""Constants."""
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
CONT = "CONT"
|
|
5
4
|
HEAD = "HEAD"
|
|
6
5
|
ABBR = "ABBR"
|
|
@@ -143,6 +142,9 @@ WIFE = "WIFE"
|
|
|
143
142
|
WILL = "WILL"
|
|
144
143
|
WWW = "WWW"
|
|
145
144
|
|
|
145
|
+
MIME_TEXT = "text/plain"
|
|
146
|
+
MIME_HTML = "text/html"
|
|
147
|
+
|
|
146
148
|
payloads = {
|
|
147
149
|
"https://gedcom.io/terms/v7/ABBR": "http://www.w3.org/2001/XMLSchema#string",
|
|
148
150
|
"https://gedcom.io/terms/v7/ADDR": "http://www.w3.org/2001/XMLSchema#string",
|
|
@@ -1921,3 +1923,19 @@ substructures = {
|
|
|
1921
1923
|
"https://gedcom.io/terms/v7/EXID": {"TYPE": "https://gedcom.io/terms/v7/EXID-TYPE"},
|
|
1922
1924
|
"https://gedcom.io/terms/v7/GEDC": {"VERS": "https://gedcom.io/terms/v7/GEDC-VERS"},
|
|
1923
1925
|
}
|
|
1926
|
+
|
|
1927
|
+
|
|
1928
|
+
GEDCOM_MONTHS = {
|
|
1929
|
+
"JAN": 1,
|
|
1930
|
+
"FEB": 2,
|
|
1931
|
+
"MAR": 3,
|
|
1932
|
+
"APR": 4,
|
|
1933
|
+
"MAY": 5,
|
|
1934
|
+
"JUN": 6,
|
|
1935
|
+
"JUL": 7,
|
|
1936
|
+
"AUG": 8,
|
|
1937
|
+
"SEP": 9,
|
|
1938
|
+
"OCT": 10,
|
|
1939
|
+
"NOV": 11,
|
|
1940
|
+
"DEC": 12,
|
|
1941
|
+
}
|
|
@@ -11,6 +11,7 @@ ageduration = (
|
|
|
11
11
|
f'(?P<days2>{days}))?|(?P<weeks3>{weeks})({d}(?P<days3>{days}))?|(?P<days4>{days}))'
|
|
12
12
|
)
|
|
13
13
|
age = f'((?P<agebound>{agebound}){d})?{ageduration}'
|
|
14
|
+
alphanum = '[a-zA-Z0-9]'
|
|
14
15
|
anychar = '[\t-\\U0010ffff]'
|
|
15
16
|
atsign = '@'
|
|
16
17
|
banned = (
|
|
@@ -23,6 +24,16 @@ tagchar = f'({ucletter}|[0-9]|{underscore})'
|
|
|
23
24
|
exttag = f'{underscore}({tagchar})+'
|
|
24
25
|
daterestrict = 'FROM|TO|BET|AND|BEF|AFT|ABT|CAL|EST'
|
|
25
26
|
calendar = f'(?!{daterestrict})(GREGORIAN|JULIAN|FRENCH_R|HEBREW|{exttag})'
|
|
27
|
+
restricted_name_first = '[a-zA-Z0-9]'
|
|
28
|
+
restricted_name_chars = '[a-zA-Z0-9!#$&\\-^_.+]'
|
|
29
|
+
restricted_name = f'{restricted_name_first}({restricted_name_chars}){{,126}}'
|
|
30
|
+
type_name = f'{restricted_name}'
|
|
31
|
+
ietf_token = f'{type_name}'
|
|
32
|
+
tchar = "[!#$%&'*+\\-.^_`|~0-9a-zA-Z]"
|
|
33
|
+
token = f'({tchar})+'
|
|
34
|
+
x_token = f'x-{token}'
|
|
35
|
+
extension_token = f'({ietf_token}|{x_token})'
|
|
36
|
+
composite_type = f'(message|multipart|{extension_token})'
|
|
26
37
|
day = f'{integer}'
|
|
27
38
|
stdtag = f'{ucletter}({tagchar})*'
|
|
28
39
|
month = f'(?!{daterestrict})({stdtag}|{exttag})'
|
|
@@ -35,11 +46,38 @@ dateexact = f'(?P<day>{day}){d}(?P<month>{month}){d}(?P<year>{year})'
|
|
|
35
46
|
dateperiod = f'((TO{d}(?P<todate1>{date}))?|FROM{d}(?P<fromdate>{date})({d}TO{d}(?P<todate2>{date}))?)'
|
|
36
47
|
daterange = f'(BET{d}(?P<between>{date}){d}AND{d}(?P<and>{date})|AFT{d}(?P<after>{date})|BEF{d}(?P<before>{date}))'
|
|
37
48
|
datevalue = f'({date}|{dateperiod}|{daterange}|{dateapprox})?'
|
|
49
|
+
discrete_type = f'(text|image|audio|video|application|{extension_token})'
|
|
50
|
+
stdenum = f'({stdtag}|{integer})'
|
|
51
|
+
enum = f'({stdenum}|{exttag})'
|
|
38
52
|
tag = f'({stdtag}|{exttag})'
|
|
39
|
-
enum = f'{tag}'
|
|
40
53
|
eol = '(\\\r(\\\n)?|\\\n)'
|
|
54
|
+
singleton = '[0-9A-WY-Za-wy-z]'
|
|
55
|
+
extension = f'{singleton}(-({alphanum}){{2,8}})+'
|
|
56
|
+
extlang = '[a-zA-Z]{3}(-[a-zA-Z]{3}){,2}'
|
|
41
57
|
fraction = '[0-9]+'
|
|
58
|
+
irregular = (
|
|
59
|
+
'(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|'
|
|
60
|
+
'i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|'
|
|
61
|
+
'i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)'
|
|
62
|
+
)
|
|
63
|
+
regular = (
|
|
64
|
+
'(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|'
|
|
65
|
+
'zh-min|zh-min-nan|zh-xiang)'
|
|
66
|
+
)
|
|
67
|
+
grandfathered = f'({irregular}|{regular})'
|
|
42
68
|
hour = '([0-9]|[01][0-9]|2[0123])'
|
|
69
|
+
subtype_name = f'{restricted_name}'
|
|
70
|
+
iana_token = f'{subtype_name}'
|
|
71
|
+
language = f'([a-zA-Z]{{2,3}}(-{extlang})?|[a-zA-Z]{{4}}|[a-zA-Z]{{5,8}})'
|
|
72
|
+
script = '[a-zA-Z]{4}'
|
|
73
|
+
region = '([a-zA-Z]{2}|[0-9]{3})'
|
|
74
|
+
variant = f'(({alphanum}){{5,8}}|[0-9]({alphanum}){{3}})'
|
|
75
|
+
privateuse = f'[xX](-({alphanum}){{1,8}})+'
|
|
76
|
+
langtag = (
|
|
77
|
+
f'{language}(-{script})?(-{region})?(-{variant})*(-{extension})*(-'
|
|
78
|
+
f'{privateuse})?'
|
|
79
|
+
)
|
|
80
|
+
language_tag = f'({langtag}|{privateuse}|{grandfathered})'
|
|
43
81
|
nonzero = '[1-9]'
|
|
44
82
|
level = f'(?P<level>0|{nonzero}[0-9]*)'
|
|
45
83
|
xref = f'{atsign}({tagchar})+{atsign}'
|
|
@@ -57,21 +95,22 @@ listdelim = f'({d})*,({d})*'
|
|
|
57
95
|
list = f'{listitem}({listdelim}{listitem})*'
|
|
58
96
|
list_enum = f'{enum}({listdelim}{enum})*'
|
|
59
97
|
list_text = f'{list}'
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
98
|
+
type = f'({discrete_type}|{composite_type})'
|
|
99
|
+
subtype = f'({extension_token}|{iana_token})'
|
|
100
|
+
ows = '[ \t]*'
|
|
101
|
+
parameter_name = f'{token}'
|
|
102
|
+
obs_text = '[\\x80-\\xff]'
|
|
103
|
+
qdtext = f'([\t !#-\\[\\]-~]|{obs_text})'
|
|
104
|
+
quoted_pair = f'\\\\([\t !-~]|{obs_text})'
|
|
105
|
+
quoted_string = f'"({qdtext}|{quoted_pair})*"'
|
|
106
|
+
parameter_value = f'({token}|{quoted_string})'
|
|
107
|
+
parameter = f'{parameter_name}={parameter_value}'
|
|
108
|
+
parameters = f'({ows};{ows}({parameter})?)*'
|
|
109
|
+
mediatype = f'{type}/{subtype}{parameters}'
|
|
71
110
|
minute = '[012345][0-9]'
|
|
72
111
|
namechar = '[ -.0-\\U0010ffff]'
|
|
73
112
|
namestr = f'({namechar})+'
|
|
74
|
-
personalname = f'({namestr}|({namestr})?/(?P<surname>{namestr})?/({namestr})?)'
|
|
113
|
+
personalname = f'({namestr}|(?P<given>{namestr})?/(?P<surname>{namestr})?/(?P<suffix>{namestr})?)'
|
|
75
114
|
second = '[012345][0-9]'
|
|
76
115
|
text = f'({anychar})*'
|
|
77
116
|
special = f'{text}'
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
"""GEDCOM 7 parser."""
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
import re
|
|
5
|
-
from typing import Dict, List
|
|
6
4
|
|
|
7
|
-
from . import const, grammar
|
|
5
|
+
from . import cast, const, grammar
|
|
8
6
|
from .types import GedcomStructure
|
|
9
7
|
|
|
10
8
|
|
|
11
|
-
def loads(string: str) ->
|
|
9
|
+
def loads(string: str) -> list[GedcomStructure]:
|
|
12
10
|
"""Load from a string."""
|
|
13
|
-
context:
|
|
14
|
-
records:
|
|
15
|
-
ext:
|
|
11
|
+
context: dict[int, GedcomStructure] = {}
|
|
12
|
+
records: list[GedcomStructure] = []
|
|
13
|
+
ext: dict[str, str] = {}
|
|
16
14
|
for match in re.finditer(grammar.line, string):
|
|
17
15
|
data = match.groupdict()
|
|
18
16
|
level = int(data["level"])
|
|
@@ -39,6 +37,10 @@ def loads(string: str) -> List[GedcomStructure]:
|
|
|
39
37
|
if level > 0:
|
|
40
38
|
parent = context[level - 1]
|
|
41
39
|
structure.parent = parent
|
|
40
|
+
structure.value = cast.cast_value(
|
|
41
|
+
text=structure.text, type_id=structure.type_id
|
|
42
|
+
)
|
|
43
|
+
if level > 0:
|
|
42
44
|
parent.children.append(structure)
|
|
43
45
|
else:
|
|
44
46
|
records.append(structure)
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Classes and data types."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Literal, Union
|
|
7
|
+
|
|
8
|
+
from . import const
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class GedcomStructure:
|
|
13
|
+
"""Gedcom structure class."""
|
|
14
|
+
|
|
15
|
+
tag: str
|
|
16
|
+
pointer: str
|
|
17
|
+
text: str
|
|
18
|
+
xref: str
|
|
19
|
+
children: list["GedcomStructure"] = field(default_factory=list)
|
|
20
|
+
parent: "GedcomStructure | None" = None
|
|
21
|
+
value: "DataType" | None = None
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def type_id(self) -> str:
|
|
25
|
+
"""Get the structure type ID."""
|
|
26
|
+
if "://" in self.tag:
|
|
27
|
+
return self.tag
|
|
28
|
+
if self.parent is None:
|
|
29
|
+
if self.tag == const.HEAD:
|
|
30
|
+
return "HEAD pseudostructure"
|
|
31
|
+
if self.tag == const.TRLR:
|
|
32
|
+
return "TRLR pseudostructure"
|
|
33
|
+
return const.substructures[""][self.tag]
|
|
34
|
+
return const.substructures[self.parent.type_id][self.tag]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass
|
|
38
|
+
class PersonalName:
|
|
39
|
+
"""Personal name type."""
|
|
40
|
+
|
|
41
|
+
fullname: str
|
|
42
|
+
given: str | None = None
|
|
43
|
+
surname: str | None = None
|
|
44
|
+
suffix: str | None = None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class Time:
|
|
49
|
+
"""Time type."""
|
|
50
|
+
|
|
51
|
+
hour: int
|
|
52
|
+
minute: int
|
|
53
|
+
second: int | None = None
|
|
54
|
+
fraction: int | None = None
|
|
55
|
+
tz: Literal["Z"] | None = None
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class Age:
|
|
60
|
+
"""Age type."""
|
|
61
|
+
|
|
62
|
+
agebound: str | None = None
|
|
63
|
+
years: int | None = None
|
|
64
|
+
months: int | None = None
|
|
65
|
+
weeks: int | None = None
|
|
66
|
+
days: int | None = None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass
|
|
70
|
+
class MediaType:
|
|
71
|
+
"""Media type type."""
|
|
72
|
+
|
|
73
|
+
media_type: str
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass
|
|
77
|
+
class DateExact:
|
|
78
|
+
"""Exact date type."""
|
|
79
|
+
|
|
80
|
+
day: int
|
|
81
|
+
month: str
|
|
82
|
+
year: int
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass
|
|
86
|
+
class Date:
|
|
87
|
+
"""Date type."""
|
|
88
|
+
|
|
89
|
+
calendar: str | None = None
|
|
90
|
+
day: int | None = None
|
|
91
|
+
month: str | None = None
|
|
92
|
+
year: int | None = None
|
|
93
|
+
epoch: str | None = None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@dataclass
|
|
97
|
+
class DatePeriod:
|
|
98
|
+
"""Date period type."""
|
|
99
|
+
|
|
100
|
+
from_: Date | None = None
|
|
101
|
+
to: Date | None = None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass
|
|
105
|
+
class DateApprox:
|
|
106
|
+
"""Date approx type."""
|
|
107
|
+
|
|
108
|
+
date: Date
|
|
109
|
+
approx: str | None = None
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclass
|
|
113
|
+
class DateRange:
|
|
114
|
+
"""Date range type."""
|
|
115
|
+
|
|
116
|
+
start: Date
|
|
117
|
+
end: Date | None = None
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
DateValue = Union[
|
|
121
|
+
Date,
|
|
122
|
+
DatePeriod,
|
|
123
|
+
DateApprox,
|
|
124
|
+
DateRange,
|
|
125
|
+
]
|
|
126
|
+
|
|
127
|
+
DataType = Union[
|
|
128
|
+
str,
|
|
129
|
+
int,
|
|
130
|
+
list[str],
|
|
131
|
+
PersonalName,
|
|
132
|
+
Time,
|
|
133
|
+
Age,
|
|
134
|
+
MediaType,
|
|
135
|
+
DateExact,
|
|
136
|
+
DateApprox,
|
|
137
|
+
DateRange,
|
|
138
|
+
DatePeriod,
|
|
139
|
+
DateValue,
|
|
140
|
+
]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Utility functions for GEDCOM 7 processing."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import datetime
|
|
6
|
+
|
|
7
|
+
from . import const, types
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_first_child_with_tag(
|
|
11
|
+
structure: types.GedcomStructure, tag: str
|
|
12
|
+
) -> types.GedcomStructure | None:
|
|
13
|
+
"""Get the first child of a GEDCOM structure with a specific tag.
|
|
14
|
+
|
|
15
|
+
If no child with the specified tag is found, return None.
|
|
16
|
+
"""
|
|
17
|
+
for child in structure.children:
|
|
18
|
+
if child.tag == tag:
|
|
19
|
+
return child
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def date_exact_to_python_date(date: types.DateExact) -> datetime.date:
|
|
24
|
+
"""Convert a GEDCOM DateExact to a Python date."""
|
|
25
|
+
try:
|
|
26
|
+
month = const.GEDCOM_MONTHS[date.month.upper()]
|
|
27
|
+
except KeyError:
|
|
28
|
+
raise ValueError(f"Invalid month in date: {date.month}")
|
|
29
|
+
return datetime.date(year=date.year, month=month, day=date.day)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def time_to_python_time(time: types.Time) -> datetime.time:
|
|
33
|
+
"""Convert a GEDCOM Time to a Python time."""
|
|
34
|
+
second = 0
|
|
35
|
+
microsecond = 0
|
|
36
|
+
if time.second:
|
|
37
|
+
second = time.second
|
|
38
|
+
if time.fraction:
|
|
39
|
+
fraction = float(f"0.{time.fraction}")
|
|
40
|
+
microsecond = int(fraction * 1_000_000)
|
|
41
|
+
return datetime.time(
|
|
42
|
+
hour=time.hour,
|
|
43
|
+
minute=time.minute,
|
|
44
|
+
second=second,
|
|
45
|
+
microsecond=microsecond,
|
|
46
|
+
tzinfo=datetime.timezone.utc,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def date_exact_and_time_to_python_datetime(
|
|
51
|
+
date: types.DateExact, time: types.Time | None = None
|
|
52
|
+
) -> datetime.datetime:
|
|
53
|
+
"""Convert a GEDCOM date and optional time to a Python datetime."""
|
|
54
|
+
date_obj = date_exact_to_python_date(date)
|
|
55
|
+
if time:
|
|
56
|
+
time_obj = time_to_python_time(time)
|
|
57
|
+
return datetime.datetime.combine(date_obj, time_obj)
|
|
58
|
+
return datetime.datetime.combine(
|
|
59
|
+
date_obj, datetime.time.min, tzinfo=datetime.timezone.utc
|
|
60
|
+
)
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: gedcom7
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: GEDCOM 7 parser
|
|
5
|
+
Home-page: https://github.com/DavidMStraub/python-gedcom7
|
|
5
6
|
Author: David M. Straub
|
|
6
7
|
Author-email: straub@protonmail.com
|
|
7
8
|
License: MIT
|
|
8
9
|
Description-Content-Type: text/markdown
|
|
9
10
|
License-File: LICENSE
|
|
11
|
+
Dynamic: license-file
|
|
10
12
|
|
|
11
13
|
# gedcom7
|
|
12
14
|
|
|
@@ -3,13 +3,16 @@ README.md
|
|
|
3
3
|
pyproject.toml
|
|
4
4
|
setup.cfg
|
|
5
5
|
gedcom7/__init__.py
|
|
6
|
+
gedcom7/cast.py
|
|
6
7
|
gedcom7/const.py
|
|
7
8
|
gedcom7/grammar.py
|
|
8
9
|
gedcom7/parser.py
|
|
9
10
|
gedcom7/types.py
|
|
11
|
+
gedcom7/util.py
|
|
10
12
|
gedcom7.egg-info/PKG-INFO
|
|
11
13
|
gedcom7.egg-info/SOURCES.txt
|
|
12
14
|
gedcom7.egg-info/dependency_links.txt
|
|
13
15
|
gedcom7.egg-info/top_level.txt
|
|
16
|
+
test/test_cast.py
|
|
14
17
|
test/test_parser.py
|
|
15
|
-
test/
|
|
18
|
+
test/test_util.py
|