-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathged2print.py
More file actions
164 lines (155 loc) · 7.55 KB
/
ged2print.py
File metadata and controls
164 lines (155 loc) · 7.55 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from ged4py import GedcomReader
import graphviz
import os
import textwrap
from collections import defaultdict
debug = 0
# Initiate the graph
dot = graphviz.Digraph(comment="Inferred Spouse Pairs", format='pdf')
dot.attr(fontsize='10', arrowhead='none', nodesep='1', ranksep='2',rankdir='TB',size = "33.11,23.39")
# Path of the GEDCOM file
path = "/Users/oncel/Desktop/tayyarnew.ged"
#path = "/Users/oncel/Desktop/eminesitki.ged"
#path = "/Users/oncel/Desktop/kennedy.ged"
# Parse the GEDCOM file
with GedcomReader(path) as parser:
obje_records = {rec.xref_id: rec for rec in parser.records0("OBJE")}
# All individuals are listed
individuals = {i.xref_id: i for i in parser.records0("INDI")}
indi_image_paths = {}
# Create a reverse index: FAMS/FAMC ID → [individuals]
fams_to_people = defaultdict(list)
famc_to_people = defaultdict(list)
# Loop over individuals according to FAMS/FAMC label
for indi in individuals.values():
for sub in indi.sub_records:
if sub.tag == "FAMS":
fams_to_people[sub.value].append(indi)
if sub.tag == "FAMC":
famc_to_people[sub.value].append(indi)
if sub.tag == "OBJE":
# Inline OBJE or pointer to OBJE?
if sub.value and sub.value.startswith('@'):
obj = obje_records.get(sub.value)
if obj:
image_path = obj.sub_tag_value("FILE")
if debug>0:
print('indi:',indi,'img path2:',image_path)
else: # It's inline OBJE
image_path = sub.sub_tag_value("FILE")
if image_path:
indi_image_paths[indi.xref_id] = image_path
# Add individual nodes
for i_id, indi in individuals.items():
name = indi.sub_tag_value("NAME")
if name and '/' in name:
parts = name.split('/')
given = parts[0].strip().strip("'\"")
surname = parts[1].strip().strip("'\"")
else:
given = name
surname = ""
gender = indi.sub_tag_value("SEX")
occup = indi.sub_tag_value("OCCU")
label = " ".join(part for part in name if part).replace("/", "") if isinstance(name, tuple) else name
#if occup:
# occup='<BR/>'.join(occup[i:i+len(label)] for i in range(0, len(occup), len(label)))
if occup:
mult = 1
new_str = ""
for i, char in enumerate(occup):
if i > len(label) * mult and char == ",":
new_str += "<BR/>"
mult += 1
else:
new_str += char
occup = new_str
print(occup)
#print('newoccup:',occup)
birth_date = indi.sub_tag_value("BIRT/DATE")
birth_place = indi.sub_tag_value("BIRT/PLAC")
death_date = indi.sub_tag_value("DEAT/DATE")
death_place = indi.sub_tag_value("DEAT/PLAC")
if not birth_date:
birth_date=' '
if not birth_place:
birth_place=' '
if not death_date:
death_date=' '
if not death_place:
death_place=' '
shape = "ellipse" if gender == "F" else "box"
image_path = indi_image_paths.get(i_id)
if debug>0:
print('img path:',image_path)
if image_path and os.path.isfile(image_path) and occup:
dot.node(i_id, label=f'''<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR> <TD FIXEDSIZE="TRUE" WIDTH="600" HEIGHT="600"><IMG SRC="{image_path}" SCALE="TRUE"/></TD></TR>
<TR><TD><FONT POINT-SIZE="100">{label}<BR/>{birth_date},{birth_place}-{death_date},{death_place}<BR/><i>{occup}</i></FONT></TD></TR>
</TABLE>
>''', shape='none')
elif image_path and os.path.isfile(image_path) and not occup:
dot.node(i_id, label=f'''<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR> <TD FIXEDSIZE="TRUE" WIDTH="600" HEIGHT="600"><IMG SRC="{image_path}" SCALE="TRUE"/></TD></TR>
<TR><TD><FONT POINT-SIZE="100">{label}<BR/>{birth_date},{birth_place}-{death_date},{death_place}<BR/></FONT></TD></TR>
</TABLE>
>''', shape='none')
else:
#dot.node(i_id, label=label, shape=shape)
dot.node(i_id, label=f'''<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR> <TD FIXEDSIZE="TRUE" WIDTH="600" HEIGHT="600"></TD></TR>
<TR><TD><FONT POINT-SIZE="100">{label}<BR/>{birth_date},{birth_place}-{death_date},{death_place}<BR/></FONT></TD></TR>
</TABLE>
>''', shape='none')
# Infer couples: anyone who shares a FAMS is likely a couple
for fam_id, people in fams_to_people.items():
if debug>0:
print('famId:',fam_id)
marriage_node = f"m_{fam_id}"
dot.node(marriage_node, label="", shape="point", width="1")
if len(people) == 2:
p1, p2 = people
with dot.subgraph() as s:
s.attr(rank='same')
s.node(p1.xref_id)
s.node(p2.xref_id)
#if fam_id=='@F0010@' or fam_id in {f"@F{str(i).zfill(4)}@" for i in range(34, 40)} or fam_id in {f"@F{str(i).zfill(4)}@" for i in range(5, 10)}:
if fam_id in {f"@F{str(i).zfill(4)}@" for i in range(34, 36)} or fam_id =='@F0039@' or fam_id in {f"@F{str(i).zfill(4)}@" for i in range(5, 8)}:
minlen_parents_str='0'
elif fam_id =='@F0009@':
minlen_parents_str='0'
elif fam_id =='@F0033@':
minlen_parents_str='0'
else:
minlen_parents_str='1'
dot.edge(p1.xref_id, marriage_node, dir="none", style="solid",minlen=minlen_parents_str)
dot.edge(marriage_node, p2.xref_id, dir="none", style="solid",minlen=minlen_parents_str)
if debug>0:
print('parents:',p1.xref_id,p2.xref_id)
for fam_idchild, children in famc_to_people.items():
if fam_id==fam_idchild:
if debug>0:
print('n children:',len(children))
if fam_id in {f"@F{str(i).zfill(4)}@" for i in range(34, 36)} or fam_id =='@F0039@' or fam_id in {f"@F{str(i).zfill(4)}@" for i in range(5, 7)}:
#if fam_id in {f"@F{str(i).zfill(4)}@" for i in range(34, 40)} or fam_id in {f"@F{str(i).zfill(4)}@" for i in range(5, 10)}:
minlen_child_str='2'
elif fam_id =='@F0009@': #m. izzet ebeveyn
minlen_child_str='2'
elif fam_id =='@F0008@':
minlen_child_str='1'
elif fam_id =='@F0033@': # m. izzet- cerkes seref
minlen_child_str='3'
else:
minlen_child_str='1'
for c in range(0,len(children)):
c1 = children[c]
dot.edge(marriage_node, c1.xref_id, arrowhead='none',concentrate='true',minlen=minlen_child_str)
elif len(people) > 2:
print(f"Family {fam_id} has more than 2 spouses?! -> {[p.xref_id for p in people]}")
print("done.")
#dot.unflatten(stagger=3)
dot.render("hede", view=True)
print("hede.pdf")