-
Notifications
You must be signed in to change notification settings - Fork 18
/
ged2dot.py
executable file
·696 lines (584 loc) · 24.8 KB
/
ged2dot.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
#!/usr/bin/env python3
#
# Copyright Miklos Vajna
#
# SPDX-License-Identifier: MPL-2.0
#
"""A version of ged2dot that uses breadth-first search to traverse the gedcom graph."""
from typing import BinaryIO
from typing import Dict
from typing import List
from typing import Optional
from typing import cast
import argparse
import configparser
import os
import sys
class Ged2DotException(Exception):
"""An exception that is intentionally raised by ged2dot."""
class Config:
"""Stores options from a config file or from cmdline args."""
def __init__(self) -> None:
self.input = "-"
self.output = "-"
self.rootfamily = "F1"
# Could be 0, but defaulting to something that can easily explode on large input is not
# helpful.
self.familydepth = "3"
self.imagedir = "images"
self.nameorder = "little"
self.direction = "both"
self.birthformat = "{}-"
self.relpath = "false"
def read_config(self, config_file: str) -> None:
"""Reads config from a provided file."""
if not config_file:
return
config_parser = configparser.ConfigParser()
config_parser.read(config_file)
for section in config_parser.sections():
if section != "ged2dot":
continue
for option in config_parser.options(section):
setattr(self, option, config_parser.get(section, option))
def read_args(self, args: argparse.Namespace) -> None:
"""Reads config from cmdline args."""
if args.input:
self.input = args.input
if args.output:
self.output = args.output
if args.rootfamily:
self.rootfamily = args.rootfamily
if args.familydepth:
self.familydepth = args.familydepth
if args.imagedir:
self.imagedir = args.imagedir
if args.nameorder:
self.nameorder = args.nameorder
if args.direction:
self.direction = args.direction
if args.birthformat:
self.birthformat = args.birthformat
if args.relpath:
self.relpath = "true"
def get_dict(self) -> Dict[str, str]:
"""Gets the config as a dict."""
config = {
"input": self.input,
"output": self.output,
"rootfamily": self.rootfamily,
"familydepth": self.familydepth,
"imagedir": self.imagedir,
"nameorder": self.nameorder,
"direction": self.direction,
"birthformat": self.birthformat,
"relpath": self.relpath,
}
return config
class Node:
"""Base class for an individual or family."""
def get_identifier(self) -> str: # pragma: no cover
"""Gets the ID of this node."""
return str()
def set_depth(self, depth: int) -> None: # pragma: no cover
"""Set the depth of this node, during one graph traversal."""
# pylint: disable=unused-argument
def get_depth(self) -> int: # pragma: no cover
"""Get the depth of this node, during one graph traversal."""
return 0
def get_neighbours(self, direction: str) -> List["Node"]: # pragma: no cover
"""Get the neighbour nodes of this node."""
return []
def resolve(self, graph: List["Node"]) -> None: # pragma: no cover
"""Resolve string IDs to node objects."""
# pylint: disable=unused-argument
def graph_find(graph: List[Node], identifier: str) -> Optional[Node]:
"""Find identifier in graph."""
if not identifier:
return None
results = [node for node in graph if node.get_identifier() == identifier]
if not results:
return None
assert len(results) == 1
return results[0]
def get_abspath(path: str) -> str:
"""Make a path absolute, taking the repo root as a base dir."""
if os.path.isabs(path):
return path
return os.path.join(os.path.dirname(os.path.realpath(__file__)), path)
def get_data_abspath(gedcom: str, path: str) -> str:
"""Make a path absolute, taking the gedcom file's dir as a base dir."""
if os.path.isabs(path):
return path
return os.path.join(os.path.dirname(os.path.realpath(gedcom)), path)
def to_bytes(string: str) -> bytes:
"""Encodes the string to UTF-8."""
return string.encode("utf-8")
class IndividualConfig:
"""Key-value pairs on an individual."""
def __init__(self) -> None:
self.__note = ""
self.__birth = ""
self.__death = ""
def set_note(self, note: str) -> None:
"""Sets a note."""
self.__note = note
def get_note(self) -> str:
"""Gets a note."""
return self.__note
def set_birth(self, birth: str) -> None:
"""Sets the birth date."""
self.__birth = birth
def get_birth(self) -> str:
"""Gets the birth date."""
return self.__birth
def set_death(self, death: str) -> None:
"""Sets the death date."""
self.__death = death
def get_death(self) -> str:
"""Gets the death date."""
return self.__death
class Individual(Node):
"""An individual is always a child in a family, and is an adult in 0..* families."""
def __init__(self) -> None:
self.__dict: Dict[str, str] = {}
self.__dict["identifier"] = ""
self.__dict["famc_id"] = ""
self.famc: Optional[Family] = None
self.fams_ids: List[str] = []
self.fams_list: List["Family"] = []
self.depth = 0
self.__dict["forename"] = ""
self.__dict["surname"] = ""
self.__dict["sex"] = ""
self.__config = IndividualConfig()
def __str__(self) -> str:
# Intentionally only print the famc/fams IDs, not the whole object to avoid not wanted
# recursion.
ret = "Individual(__dict=" + str(self.__dict)
ret += ", fams_ids: " + str(self.fams_ids)
ret += ", depth: " + str(self.depth) + ")"
return ret
def resolve(self, graph: List[Node]) -> None:
self.famc = cast(Optional["Family"], graph_find(graph, self.get_famc_id()))
for fams_id in self.fams_ids:
fams = graph_find(graph, fams_id)
assert fams
self.fams_list.append(cast("Family", fams))
def get_neighbours(self, direction: str) -> List[Node]:
ret: List[Node] = []
if self.famc and direction != "child":
ret.append(self.famc)
ret += self.fams_list
return ret
def get_config(self) -> IndividualConfig:
"""Returns key-value pairs of individual."""
return self.__config
def set_identifier(self, identifier: str) -> None:
"""Sets the ID of this individual."""
self.__dict["identifier"] = identifier
def get_identifier(self) -> str:
return self.__dict["identifier"]
def set_sex(self, sex: str) -> None:
"""Sets the sex of this individual."""
self.__dict["sex"] = sex
def get_sex(self) -> str:
"""Gets the sex of this individual."""
return self.__dict["sex"]
def set_forename(self, forename: str) -> None:
"""Sets the first name of this individual."""
self.__dict["forename"] = forename
def get_forename(self) -> str:
"""Gets the first name of this individual."""
return self.__dict["forename"]
def set_surname(self, surname: str) -> None:
"""Sets the family name of this individual."""
self.__dict["surname"] = surname
def get_surname(self) -> str:
"""Gets the family name of this individual."""
return self.__dict["surname"]
def set_depth(self, depth: int) -> None:
self.depth = depth
def get_depth(self) -> int:
return self.depth
def set_famc_id(self, famc_id: str) -> None:
"""Sets the child family ID."""
self.__dict["famc_id"] = famc_id
def get_famc_id(self) -> str:
"""Gets the child family ID."""
return self.__dict["famc_id"]
def get_label(self, image_dir: str, name_order: str, birth_format: str, basepath: str) -> str:
"""Gets the graphviz label."""
image_path = os.path.join(image_dir, self.get_forename() + " " + self.get_surname())
image_path += " " + self.get_config().get_birth() + ".jpg"
if not os.path.exists(to_bytes(image_path)):
image_path = os.path.join(image_dir, self.get_forename() + " " + self.get_surname()) \
+ ".jpg"
if not os.path.exists(to_bytes(image_path)):
if self.get_sex():
sex = self.get_sex().lower()
else:
sex = 'u'
image_path = get_abspath(f"placeholder-{sex}.svg")
if basepath:
image_path = os.path.relpath(image_path, basepath)
label = "<table border=\"0\" cellborder=\"0\"><tr><td>"
label += "<img scale=\"true\" src=\"" + image_path + "\"/>"
# State the font face explicitly to help correct centering.
label += "</td></tr><tr><td><font face=\"Times\">"
if name_order == "big":
# Big endian: family name first.
label += self.get_surname() + "<br/>"
label += self.get_forename() + "<br/>"
else:
# Little endian: given name first.
label += self.get_forename() + "<br/>"
label += self.get_surname() + "<br/>"
if self.get_config().get_birth() and not self.get_config().get_death():
label += birth_format.format(self.get_config().get_birth())
elif not self.get_config().get_birth() and self.get_config().get_death():
label += "† " + self.get_config().get_death()
else:
label += self.get_config().get_birth() + "-" + self.get_config().get_death()
label += "</font></td></tr></table>"
return label
def get_color(self) -> str:
"""Gets the color around the node."""
if not self.get_sex():
sex = 'U'
else:
sex = self.get_sex().upper()
color = {'M': 'blue', 'F': 'pink', 'U': 'black'}[sex]
return color
class Family(Node):
"""Family has exactly one wife and husband, 0..* children."""
def __init__(self) -> None:
self.__dict: Dict[str, str] = {}
self.__dict["identifier"] = ""
self.__dict["marr"] = ""
self.__dict["wife_id"] = ""
self.wife: Optional["Individual"] = None
self.__dict["husb_id"] = ""
self.husb: Optional["Individual"] = None
self.child_ids: List[str] = []
self.child_list: List["Individual"] = []
self.depth = 0
def __str__(self) -> str:
# Intentionally only print the wife/husband/child IDs, not the whole object to avoid not
# wanted recursion.
ret = "Family(__dict=" + str(self.__dict)
ret += ", child_ids: " + str(self.child_ids)
ret += ", depth: " + str(self.depth) + ")"
return ret
def resolve(self, graph: List[Node]) -> None:
self.wife = cast(Optional["Individual"], graph_find(graph, self.get_wife_id()))
self.husb = cast(Optional["Individual"], graph_find(graph, self.get_husb_id()))
for child_id in self.child_ids:
child = graph_find(graph, child_id)
assert child
self.child_list.append(cast("Individual", child))
def get_neighbours(self, direction: str) -> List[Node]:
ret: List[Node] = []
if self.wife:
ret.append(self.wife)
if self.husb:
ret.append(self.husb)
ret += self.child_list
return ret
def set_identifier(self, identifier: str) -> None:
"""Sets the ID of this family."""
self.__dict["identifier"] = identifier
def get_identifier(self) -> str:
return self.__dict["identifier"]
def set_marr(self, marr: str) -> None:
"""Sets the marriage date."""
self.__dict["marr"] = marr
def get_marr(self) -> str:
"""Gets the marriage date."""
return self.__dict["marr"]
def set_depth(self, depth: int) -> None:
self.depth = depth
def get_depth(self) -> int:
return self.depth
def set_wife_id(self, wife_id: str) -> None:
"""Sets the wife ID of this family."""
self.__dict["wife_id"] = wife_id
def get_wife_id(self) -> str:
"""Gets the wife ID of this family."""
return self.__dict["wife_id"]
def set_husb_id(self, husb_id: str) -> None:
"""Sets the husband ID of this family."""
self.__dict["husb_id"] = husb_id
def get_husb_id(self) -> str:
"""Gets the husband ID of this family."""
return self.__dict["husb_id"]
class GedcomImport:
"""Builds the graph from GEDCOM."""
def __init__(self) -> None:
self.individual: Optional[Individual] = None
self.family: Optional[Family] = None
self.graph: List[Node] = []
self.in_birt = False
self.in_deat = False
self.in_marr = False
def __reset_flags(self) -> None:
if self.in_birt:
self.in_birt = False
elif self.in_deat:
self.in_deat = False
elif self.in_marr:
self.in_marr = False
def __handle_level0(self, line: str) -> None:
if self.individual:
self.graph.append(self.individual)
self.individual = None
if self.family:
self.graph.append(self.family)
self.family = None
if line.startswith("@") and line.endswith("INDI"):
self.individual = Individual()
self.individual.set_identifier(line[1:-6])
elif line.startswith("@") and line.endswith("FAM"):
self.family = Family()
self.family.set_identifier(line[1:-5])
def __handle_level1(self, line: str) -> None:
self.__reset_flags()
line_lead_token = line.split(' ')[0]
if line_lead_token == "SEX" and self.individual:
tokens = line.split(' ')
if len(tokens) > 1:
self.individual.set_sex(tokens[1])
elif line_lead_token == "NAME" and self.individual:
line = line[5:]
tokens = line.split('/')
self.individual.set_forename(tokens[0].strip())
if len(tokens) > 1:
self.individual.set_surname(tokens[1].strip())
elif line_lead_token == "FAMC" and self.individual:
# At least <https://www.ancestry.com> sometimes writes multiple FAMC, which doesn't
# make sense. Import only the first one.
if not self.individual.get_famc_id():
self.individual.set_famc_id(line[6:-1])
elif line_lead_token == "FAMS" and self.individual:
self.individual.fams_ids.append(line[6:-1])
elif line_lead_token == "HUSB" and self.family:
self.family.set_husb_id(line[6:-1])
elif line_lead_token == "WIFE" and self.family:
self.family.set_wife_id(line[6:-1])
elif line_lead_token == "CHIL" and self.family:
self.family.child_ids.append(line[6:-1])
elif line_lead_token == "MARR" and self.family:
self.in_marr = True
else:
self.__handle_individual_config(line)
def __handle_individual_config(self, line: str) -> None:
"""Handles fields stored in individual.get_config()."""
line_lead_token = line.split(' ')[0]
if line_lead_token == "BIRT":
self.in_birt = True
elif line_lead_token == "DEAT":
self.in_deat = True
elif line_lead_token == "NOTE" and self.individual:
self.individual.get_config().set_note(line[5:])
def load(self, config: Dict[str, str]) -> List[Node]:
"""Tokenizes and resolves a gedcom file into a graph."""
graph = self.tokenize(config)
for node in graph:
node.resolve(graph)
return graph
def tokenize(self, config: Dict[str, str]) -> List[Node]:
"""Tokenizes a gedcom file into a graph."""
if config["input"] == "-":
return self.tokenize_from_stream(sys.stdin.buffer)
with open(config["input"], "rb") as stream:
return self.tokenize_from_stream(stream)
def tokenize_from_stream(self, stream: BinaryIO) -> List[Node]:
"""Tokenizes a gedcom stream into a graph."""
stream_buf = stream.read()
lines = stream_buf.split(b"\r\n")
if b"\r" not in stream_buf:
lines = stream_buf.split(b"\n")
for line_bytes in lines:
line = safe_utf8_decode(line_bytes.strip())
if not line:
continue
tokens = line.split(" ")
first_token = tokens[0]
# Ignore UTF-8 BOM, if there is one at the beginning of the line.
if first_token.startswith("\ufeff"):
first_token = first_token[1:]
level = safe_atoi(first_token)
rest = " ".join(tokens[1:])
if level == 0:
self.__handle_level0(rest)
elif level == 1:
self.__handle_level1(rest)
elif level == 2:
if rest.startswith("DATE"):
year = rest.rsplit(' ', maxsplit=1)[-1]
if self.individual:
if self.in_birt:
self.individual.get_config().set_birth(year)
elif self.in_deat:
self.individual.get_config().set_death(year)
elif self.family and self.in_marr:
self.family.set_marr(year)
return self.graph
def bfs(root: Node, config: Dict[str, str]) -> List[Node]:
"""
Does a breadth first search traversal of the graph, from root. Returns the traversed nodes.
"""
visited = [root]
queue = [root]
ret: List[Node] = []
direction = config.get("direction", "both")
while queue:
node = queue.pop(0)
# Every 2nd node is a family + the root is always a family.
family_depth = int(config["familydepth"])
if node.get_depth() > family_depth * 2 + 1:
return ret
ret.append(node)
for neighbour in node.get_neighbours(direction):
if neighbour not in visited:
neighbour.set_depth(node.get_depth() + 1)
visited.append(neighbour)
queue.append(neighbour)
return ret
def safe_atoi(string: str) -> int:
"""Converts str to an int, raising an own exception on error."""
try:
return int(string)
except ValueError as exc:
raise Ged2DotException() from exc
def safe_utf8_decode(source: bytes) -> str:
"""Decodes bytes to a string, raising an own exception on error."""
try:
return source.decode("utf-8")
except UnicodeDecodeError as exc:
raise Ged2DotException() from exc
class DotExport:
"""Serializes the graph to Graphviz / dot."""
def __init__(self) -> None:
self.subgraph: List[Node] = []
self.config: Dict[str, str] = {}
def __store_individual_nodes(self, stream: BinaryIO) -> None:
for node in self.subgraph:
if not isinstance(node, Individual):
continue
individual = node
stream.write(to_bytes(node.get_identifier() + " [shape=box, "))
image_dir = self.config.get("imagedir", "")
image_dir_abs = get_data_abspath(self.config.get("input", ""), image_dir)
name_order = self.config.get("nameorder", "little")
birth_format = self.config.get("birthformat", "{}-")
basepath = ""
if self.config.get("relpath", "false") == "true" and self.config["output"] != "-":
basepath = os.path.dirname(os.path.abspath(self.config["output"]))
label = individual.get_label(image_dir_abs, name_order, birth_format, basepath)
stream.write(to_bytes("label = <" + label + ">\n"))
stream.write(to_bytes("color = " + individual.get_color() + "];\n"))
def __store_family_nodes(self, stream: BinaryIO) -> None:
stream.write(to_bytes("\n"))
for node in self.subgraph:
if not isinstance(node, Family):
continue
image_path = get_abspath("marriage.svg")
if self.config.get("relpath", "false") == "true" and self.config["output"] != "-":
basepath = os.path.dirname(os.path.abspath(self.config["output"]))
image_path = os.path.relpath(image_path, basepath)
# Emit explicit size from marriage.svg, otherwise it won't be centered in the PNG
# output.
table_start = "<table border=\"0\" cellborder=\"0\" width=\"32px\" height=\"23px\">"
label = table_start + "<tr><td><img src=\"" + image_path + "\"/></td></tr></table>"
if node.get_marr():
label = node.get_marr()
# Make sure family -> children edges appear left-to-right in the same order in which
# they are defined in the input.
attrs = "shape=circle, margin=\"0,0\", label=<" + label + ">, ordering=out"
stream.write(to_bytes(node.get_identifier() + " [" + attrs + "];\n"))
stream.write(to_bytes("\n"))
def __store_edges(self, stream: BinaryIO) -> None:
for node in self.subgraph:
if not isinstance(node, Family):
continue
family = node
if family.wife:
from_wife = family.wife.get_identifier() + " -> " + family.get_identifier() + " [dir=none];\n"
stream.write(to_bytes(from_wife))
if family.husb:
from_husb = family.husb.get_identifier() + " -> " + family.get_identifier() + " [dir=none];\n"
stream.write(to_bytes(from_husb))
for child in family.child_list:
stream.write(to_bytes(family.get_identifier() + " -> " + child.get_identifier() + " [dir=none];\n"))
def store(self, subgraph: List[Node], config: Dict[str, str]) -> None:
"""Exports subgraph to a graphviz path."""
if config["output"] == "-":
self.store_to_stream(subgraph, sys.stdout.buffer, config)
return
with open(config["output"], "wb") as stream:
self.store_to_stream(subgraph, stream, config)
def store_to_stream(self, subgraph: List[Node], stream: BinaryIO, config: Dict[str, str]) -> None:
"""Exports subgraph to a graphviz stream."""
stream.write(to_bytes("// Generated by <https://github.com/vmiklos/ged2dot>.\n"))
stream.write(to_bytes("digraph\n"))
stream.write(to_bytes("{\n"))
stream.write(to_bytes("splines = ortho;\n"))
stream.write(to_bytes("\n"))
self.subgraph = subgraph
self.config = config
self.__store_individual_nodes(stream)
self.__store_family_nodes(stream)
self.__store_edges(stream)
stream.write(to_bytes("}\n"))
def convert(config: Dict[str, str]) -> None:
"""API interface."""
importer = GedcomImport()
graph = importer.load(config)
root_family = graph_find(graph, config["rootfamily"])
if not root_family:
family_id = ""
for node in graph:
if not isinstance(node, Family):
continue
family_id = node.get_identifier()
break
reason = f"Root family '{config['rootfamily']}' is not found."
if family_id:
reason += f" First valid family would be '{family_id}'."
raise Ged2DotException(reason)
subgraph = bfs(root_family, config)
exporter = DotExport()
exporter.store(subgraph, config)
def main() -> None:
"""Commandline interface."""
# Parse config from file and cmdline args.
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str,
help="configuration file")
parser.add_argument("--input", type=str,
help="input GEDCOM file")
parser.add_argument("--output", type=str,
help="output DOT file")
parser.add_argument("--rootfamily", type=str,
help="root family")
parser.add_argument("--familydepth", type=str,
help="family depth")
parser.add_argument("--imagedir", type=str,
help="image directory")
parser.add_argument("--nameorder", choices=["little", "big"],
help="name order")
parser.add_argument("--direction", choices=["both", "child"],
help="name order")
parser.add_argument("--birthformat", type=str,
help="birth format when death is missing (default: '{}-', e.g. '1942-')")
parser.add_argument("--relpath", dest="relpath", action="store_true",
help="try to use relative paths (default: false)")
args = parser.parse_args()
config = Config()
config.read_config(args.config)
config.read_args(args)
convert(config.get_dict())
if __name__ == '__main__':
main()
# vim:set shiftwidth=4 softtabstop=4 expandtab: