-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgedcomParser.py
1257 lines (1077 loc) · 45.4 KB
/
gedcomParser.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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Python GEDCOM Parser
#
# Copyright (C) 2018 Nicklas Reincke (contact at reynke.com)
# Copyright (C) 2016 Andreas Oberritter
# Copyright (C) 2012 Madeleine Price Ball
# Copyright (C) 2005 Daniel Zappala (zappala at cs.byu.edu)
# Copyright (C) 2005 Brigham Young University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Further information about the license: http://www.gnu.org/licenses/gpl-2.0.html
import re as regex
from sys import version_info
from datetime import datetime
__all__ = ["Gedcom", "Element", "GedcomParseError"]
# Relationship to a mother.
GEDCOM_PROGRAM_DEFINED_TAG_MREL = "_MREL"
# Relationship to a father.
GEDCOM_PROGRAM_DEFINED_TAG_FREL = "_FREL"
GEDCOM_TAG_NOTES = "NOTE"
# The event of entering into life.
GEDCOM_TAG_BIRTH = "BIRT"
# The event of the proper disposing of the mortal remains of a deceased person.
GEDCOM_TAG_BURIAL = "BURI"
# The event of the periodic count of the population for a designated locality, such as a national or state Census.
GEDCOM_TAG_CENSUS = "CENS"
# Indicates a change, correction, or modification. Typically used in connection
# with a DATE to specify when a change in information occurred.
GEDCOM_TAG_CHANGE = "CHAN"
# The natural, adopted, or sealed (LDS) child of a father and a mother.
GEDCOM_TAG_CHILD = "CHIL"
# An indicator that additional data belongs to the superior value. The information from the CONC value is to be
# connected to the value of the superior preceding line without a space and without a carriage return and/or
# new line character. Values that are split for a CONC tag must always be split at a non-space. If the value is
# split on a space the space will be lost when concatenation takes place. This is because of the treatment that
# spaces get as a GEDCOM delimiter, many GEDCOM values are trimmed of trailing spaces and some systems look for
# the first non-space starting after the tag to determine the beginning of the value.
GEDCOM_TAG_CONCATENATION = "CONC"
# An indicator that additional data belongs to the superior value. The information from the CONT value is to be
# connected to the value of the superior preceding line with a carriage return and/or new line character.
# Leading spaces could be important to the formatting of the resultant text. When importing values from CONT lines
# the reader should assume only one delimiter character following the CONT tag. Assume that the rest of the leading
# spaces are to be a part of the value.
GEDCOM_TAG_CONTINUED = "CONT"
# The time of an event in a calendar format.
GEDCOM_TAG_DATE = "DATE"
# The event when mortal life terminates.
GEDCOM_TAG_DEATH = "DEAT"
# Identifies a legal, common law, or other customary relationship of man and woman and their children,
# if any, or a family created by virtue of the birth of a child to its biological father and mother.
GEDCOM_TAG_FAMILY = "FAM"
# Identifies the family in which an individual appears as a child.
GEDCOM_TAG_FAMILY_CHILD = "FAMC"
# Identifies the family in which an individual appears as a spouse.
GEDCOM_TAG_FAMILY_SPOUSE = "FAMS"
# An information storage place that is ordered and arranged for preservation and reference.
GEDCOM_TAG_FILE = "FILE"
# A given or earned name used for official identification of a person.
GEDCOM_TAG_GIVEN_NAME = "GIVN"
# An individual in the family role of a married man or father.
GEDCOM_TAG_HUSBAND = "HUSB"
# A person.
GEDCOM_TAG_INDIVIDUAL = "INDI"
# A legal, common-law, or customary event of creating a family unit of a man and a woman as husband and wife.
GEDCOM_TAG_MARRIAGE = "MARR"
# A word or combination of words used to help identify an individual, title, or other item.
# More than one NAME line should be used for people who were known by multiple names.
GEDCOM_TAG_NAME = "NAME"
# Pertaining to a grouping of attributes used in describing something. Usually referring to the data required
# to represent a multimedia object, such an audio recording, a photograph of a person, or an image of a document.
GEDCOM_TAG_OBJECT = "OBJE"
# The type of work or profession of an individual.
GEDCOM_TAG_OCCUPATION = "OCCU"
# A jurisdictional name to identify the place or location of an event.
GEDCOM_TAG_PLACE = "PLAC"
# Flag for private address or event.
GEDCOM_TAG_PRIVATE = "PRIV"
# Indicates the sex of an individual--male or female.
GEDCOM_TAG_SEX = "SEX"
# The initial or original material from which information was obtained.
GEDCOM_TAG_SOURCE = "SOUR"
# A family name passed on or used by members of a family.
GEDCOM_TAG_SURNAME = "SURN"
GEDCOM_TAG_NICKNAME = "NICK"
# An individual in the role as a mother and/or married woman.
GEDCOM_TAG_WIFE = "WIFE"
GEDCOM_TAG_TITLE = "TITL"
GEDCOM_TAG_PUBLICATION = "PUBL"
class Gedcom:
"""Parses and manipulates GEDCOM 5.5 format data
For documentation of the GEDCOM 5.5 format, see:
http://homepages.rootsweb.ancestry.com/~pmcbride/gedcom/55gctoc.htm
This parser reads and parses a GEDCOM file.
Elements may be accessed via:
- a list (all elements, default order is same as in file)
- a dict (only elements with pointers, which are the keys)
"""
def __init__(self, file_path):
"""Initialize a GEDCOM data object. You must supply a GEDCOM file
:type file_path: str
"""
self.__element_list = []
self.__element_dictionary = {}
self.invalidate_cache()
self.__root_element = Element(-1, "", "ROOT", "")
self.__parse(file_path)
def invalidate_cache(self):
"""Cause get_element_list() and get_element_dictionary() to return updated data
The update gets deferred until each of the methods actually gets called.
"""
self.__element_list = []
self.__element_dictionary = {}
def get_element_list(self):
"""Return a list of all the elements in the GEDCOM file
By default elements are in the same order as they appeared in the file.
This list gets generated on-the-fly, but gets cached. If the database
was modified, you should call invalidate_cache() once to let this
method return updated data.
Consider using `get_root_element()` or `get_root_child_elements()` to access
the hierarchical GEDCOM tree, unless you rarely modify the database.
:rtype: list of Element
"""
if not self.__element_list:
for element in self.get_root_child_elements():
self.__build_list(element, self.__element_list)
return self.__element_list
def get_element_dictionary(self):
"""Return a dictionary of elements from the GEDCOM file
Only elements identified by a pointer are listed in the dictionary.
The keys for the dictionary are the pointers.
This dictionary gets generated on-the-fly, but gets cached. If the
database was modified, you should call invalidate_cache() once to let
this method return updated data.
:rtype: dict of Element
"""
if not self.__element_dictionary:
self.__element_dictionary = {
element.get_pointer(): element for element in self.get_root_child_elements() if element.get_pointer()
}
return self.__element_dictionary
def get_root_element(self):
"""Returns a virtual root element containing all logical records as children
When printed, this element converts to an empty string.
:rtype: Element
"""
return self.__root_element
def get_root_child_elements(self):
"""Return a list of logical records in the GEDCOM file
By default, elements are in the same order as they appeared in the file.
:rtype: list of Element
"""
return self.get_root_element().get_child_elements()
# Private methods
def __parse(self, file_path):
"""Open and parse file path as GEDCOM 5.5 formatted data
:type file_path: str
"""
gedcom_file = open(file_path, 'rb')
line_number = 1
last_element = self.__root_element
for line in gedcom_file:
try:
last_element = self.__parse_line(line_number, line.decode('utf-8'), last_element)
except:
print(line_number)
print(line)
line_number += 1
@staticmethod
def __parse_line(line_number, line, last_element):
"""Parse a line from a GEDCOM 5.5 formatted document
Each line should have the following (bracketed items optional):
level + ' ' + [pointer + ' ' +] tag + [' ' + line_value]
:type line_number: int
:type line: str
:type last_element: Element
:rtype: Element
"""
# Level must start with non-negative int, no leading zeros.
level_regex = '^(0|[1-9]+[0-9]*) '
# Pointer optional, if it exists it must be flanked by `@`
pointer_regex = '(@[^@]+@ |)'
# Tag must be alphanumeric string
tag_regex = '([A-Za-z0-9_]+)'
# Value optional, consists of anything after a space to end of line
value_regex = '( [^\n\r]*|)'
# End of line defined by `\n` or `\r`
end_of_line_regex = '([\r\n]{1,2})'
# Complete regex
gedcom_line_regex = level_regex + pointer_regex + tag_regex + value_regex + end_of_line_regex
regex_match = regex.match(gedcom_line_regex, line)
if regex_match is None:
error_message = ("Line `%d` of document violates GEDCOM format 5.5" % line_number +
"\nSee: https://chronoplexsoftware.com/gedcomvalidator/gedcom/gedcom-5.5.pdf")
raise SyntaxError(error_message)
line_parts = regex_match.groups()
level = int(line_parts[0])
pointer = line_parts[1].rstrip(' ')
tag = line_parts[2]
value = line_parts[3][1:]
crlf = line_parts[4]
# Check level: should never be more than one higher than previous line.
if level > last_element.get_level() + 1:
error_message = ("Line %d of document violates GEDCOM format 5.5" % line_number +
"\nLines must be no more than one level higher than previous line." +
"\nSee: https://chronoplexsoftware.com/gedcomvalidator/gedcom/gedcom-5.5.pdf")
raise SyntaxError(error_message)
# Create element. Store in list and dict, create children and parents.
element = Element(level, pointer, tag, value, crlf, multi_line=False)
# Start with last element as parent, back up if necessary.
parent_element = last_element
while parent_element.get_level() > level - 1:
parent_element = parent_element.get_parent_element()
# Add child to parent & parent to child.
parent_element.add_child_element(element)
return element
def __build_list(self, element, element_list):
"""Recursively add elements to a list containing elements
:type element: Element
:type element_list: list of Element
"""
element_list.append(element)
for child in element.get_child_elements():
self.__build_list(child, element_list)
# Methods for analyzing individuals and relationships between individuals
def get_marriages(self, individual):
"""Return list of marriage tuples (date, place) for an individual
:type individual: Element
:rtype: tuple
"""
marriages = []
if not individual.is_individual():
raise ValueError("Operation only valid for elements with %s tag" % GEDCOM_TAG_INDIVIDUAL)
# Get and analyze families where individual is spouse.
families = self.get_families(individual, GEDCOM_TAG_FAMILY_SPOUSE)
for family in families:
for family_data in family.get_child_elements():
if family_data.get_tag() == GEDCOM_TAG_MARRIAGE:
for marriage_data in family_data.get_child_elements():
date = ''
place = ''
if marriage_data.get_tag() == GEDCOM_TAG_DATE:
date = marriage_data.get_value()
if marriage_data.get_tag() == GEDCOM_TAG_PLACE:
place = marriage_data.get_value()
marriages.append((date, place))
return marriages
def get_marriage_years(self, individual):
"""Return list of marriage years (as int) for an individual
:type individual: Element
:rtype: list of int
"""
dates = []
if not individual.is_individual():
raise ValueError("Operation only valid for elements with %s tag" % GEDCOM_TAG_INDIVIDUAL)
# Get and analyze families where individual is spouse.
families = self.get_families(individual, GEDCOM_TAG_FAMILY_SPOUSE)
for family in families:
for child in family.get_child_elements():
if child.get_tag() == GEDCOM_TAG_MARRIAGE:
for childOfChild in child.get_child_elements():
if childOfChild.get_tag() == GEDCOM_TAG_DATE:
date = childOfChild.get_value().split()[-1]
try:
dates.append(int(date))
except ValueError:
pass
return dates
def marriage_year_match(self, individual, year):
"""Check if one of the marriage years of an individual matches the supplied year. Year is an integer.
:type individual: Element
:type year: int
:rtype: bool
"""
years = self.get_marriage_years(individual)
return year in years
def marriage_range_match(self, individual, from_year, to_year):
"""Check if one of the marriage years of an individual is in a given range. Years are integers.
:type individual: Element
:type from_year: int
:type to_year: int
:rtype: bool
"""
years = self.get_marriage_years(individual)
for year in years:
if from_year <= year <= to_year:
return True
return False
def get_families(self, individual, family_type=GEDCOM_TAG_FAMILY_SPOUSE):
"""Return family elements listed for an individual
family_type can be `GEDCOM_TAG_FAMILY_SPOUSE` (families where the individual is a spouse) or
`GEDCOM_TAG_FAMILY_CHILD` (families where the individual is a child). If a value is not
provided, `GEDCOM_TAG_FAMILY_SPOUSE` is default value.
:type individual: Element
:type family_type: str
:rtype: list of Element
"""
if not individual.is_individual():
raise ValueError("Operation only valid for elements with %s tag." % GEDCOM_TAG_INDIVIDUAL)
families = []
element_dictionary = self.get_element_dictionary()
for child_element in individual.get_child_elements():
is_family = (child_element.get_tag() == family_type and
child_element.get_value() in element_dictionary and
element_dictionary[child_element.get_value()].is_family())
if is_family:
families.append(element_dictionary[child_element.get_value()])
return families
def get_ancestors(self, individual, ancestor_type="ALL"):
"""Return elements corresponding to ancestors of an individual
Optional `ancestor_type`. Default "ALL" returns all ancestors, "NAT" can be
used to specify only natural (genetic) ancestors.
:type individual: Element
:type ancestor_type: str
:rtype: list of Element
"""
if not individual.is_individual():
raise ValueError("Operation only valid for elements with %s tag." % GEDCOM_TAG_INDIVIDUAL)
parents = self.get_parents(individual, ancestor_type)
ancestors = []
ancestors.extend(parents)
for parent in parents:
ancestors.extend(self.get_ancestors(parent))
return ancestors
def get_parents(self, individual, parent_type="ALL"):
"""Return elements corresponding to parents of an individual
Optional parent_type. Default "ALL" returns all parents. "NAT" can be
used to specify only natural (genetic) parents.
:type individual: Element
:type parent_type: str
:rtype: list of Element
"""
if not individual.is_individual():
raise ValueError("Operation only valid for elements with %s tag." % GEDCOM_TAG_INDIVIDUAL)
parents = []
families = self.get_families(individual, GEDCOM_TAG_FAMILY_CHILD)
for family in families:
if parent_type == "NAT":
for family_member in family.get_child_elements():
if family_member.get_tag() == GEDCOM_TAG_CHILD and family_member.get_value() == individual.get_pointer():
for child in family_member.get_child_elements():
if child.get_value() == "Natural":
if child.get_tag() == GEDCOM_PROGRAM_DEFINED_TAG_MREL:
parents += self.get_family_members(family, GEDCOM_TAG_WIFE)
elif child.get_tag() == GEDCOM_PROGRAM_DEFINED_TAG_FREL:
parents += self.get_family_members(family, GEDCOM_TAG_HUSBAND)
else:
parents += self.get_family_members(family, "PARENTS")
return parents
def find_path_to_ancestor(self, descendant, ancestor, path=None):
"""Return path from descendant to ancestor
:rtype: object
"""
if not descendant.is_individual() and ancestor.is_individual():
raise ValueError("Operation only valid for elements with %s tag." % GEDCOM_TAG_INDIVIDUAL)
if not path:
path = [descendant]
if path[-1].get_pointer() == ancestor.get_pointer():
return path
else:
parents = self.get_parents(descendant, "NAT")
for parent in parents:
potential_path = self.find_path_to_ancestor(parent, ancestor, path + [parent])
if potential_path is not None:
return potential_path
return None
def get_family_members(self, family, members_type="ALL"):
"""Return array of family members: individual, spouse, and children
Optional argument `members_type` can be used to return specific subsets.
"ALL": Default, return all members of the family
"PARENTS": Return individuals with "HUSB" and "WIFE" tags (parents)
"HUSB": Return individuals with "HUSB" tags (father)
"WIFE": Return individuals with "WIFE" tags (mother)
"CHIL": Return individuals with "CHIL" tags (children)
:type family: Element
:type members_type: str
:rtype: list of Element
"""
if not family.is_family():
raise ValueError("Operation only valid for element with %s tag." % GEDCOM_TAG_FAMILY)
family_members = []
element_dictionary = self.get_element_dictionary()
for child_element in family.get_child_elements():
# Default is ALL
is_family = (child_element.get_tag() == GEDCOM_TAG_HUSBAND or
child_element.get_tag() == GEDCOM_TAG_WIFE or
child_element.get_tag() == GEDCOM_TAG_CHILD)
if members_type == "PARENTS":
is_family = (child_element.get_tag() == GEDCOM_TAG_HUSBAND or
child_element.get_tag() == GEDCOM_TAG_WIFE)
elif members_type == "HUSB":
is_family = child_element.get_tag() == GEDCOM_TAG_HUSBAND
elif members_type == "WIFE":
is_family = child_element.get_tag() == GEDCOM_TAG_WIFE
elif members_type == "CHIL":
is_family = child_element.get_tag() == GEDCOM_TAG_CHILD
if is_family and child_element.get_value() in element_dictionary:
family_members.append(element_dictionary[child_element.get_value()])
return family_members
# Other methods
def print_gedcom(self):
"""Write GEDCOM data to stdout"""
from sys import stdout
self.save_gedcom(stdout)
def save_gedcom(self, open_file):
"""Save GEDCOM data to a file
:type open_file: file
"""
if version_info[0] >= 3:
open_file.write(self.get_root_element().get_individual())
else:
open_file.write(self.get_root_element().get_individual().encode('utf-8'))
class GedcomParseError(Exception):
"""Exception raised when a GEDCOM parsing error occurs"""
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Element:
"""GEDCOM element
Each line in a GEDCOM file is an element with the format
level [pointer] tag [value]
where level and tag are required, and pointer and value are
optional. Elements are arranged hierarchically according to their
level, and elements with a level of zero are at the top level.
Elements with a level greater than zero are children of their
parent.
A pointer has the format @pname@, where pname is any sequence of
characters and numbers. The pointer identifies the object being
pointed to, so that any pointer included as the value of any
element points back to the original object. For example, an
element may have a FAMS tag whose value is @F1@, meaning that this
element points to the family record in which the associated person
is a spouse. Likewise, an element with a tag of FAMC has a value
that points to a family record in which the associated person is a
child.
See a GEDCOM file for examples of tags and their values.
"""
def __init__(self, level, pointer, tag, value, crlf="\n", multi_line=True):
"""Initialize an element
You must include a level, a pointer, a tag, and a value.
Normally initialized by the GEDCOM parser, not by a user.
:type level: int
:type pointer: str
:type tag: str
:type value: str
:type crlf: str
:type multi_line: bool
"""
# basic element info
self.__level = level
self.__pointer = pointer
self.__tag = tag
self.__value = value
self.__crlf = crlf
# structuring
self.__children = []
self.__parent = None
if multi_line:
self.set_multi_line_value(value)
def get_level(self):
"""Return the level of this element
:rtype: int
"""
return self.__level
def get_pointer(self):
"""Return the pointer of this element
:rtype: str
"""
return self.__pointer
def get_tag(self):
"""Return the tag of this element
:rtype: str
"""
return self.__tag
def get_value(self):
"""Return the value of this element
:rtype: str
"""
return self.__value
def set_value(self, value):
"""Set the value of this element
:type value: str
"""
self.__value = value
def get_multi_line_value(self):
"""Return the value of this element including continuations
:rtype: str
"""
result = self.get_value()
last_crlf = self.__crlf
for element in self.get_child_elements():
tag = element.get_tag()
if tag == GEDCOM_TAG_CONCATENATION:
result += element.get_value()
last_crlf = element.__crlf
elif tag == GEDCOM_TAG_CONTINUED:
result += last_crlf + element.get_value()
last_crlf = element.__crlf
return result
def __available_characters(self):
"""Get the number of available characters of the elements original string
:rtype: int
"""
element_characters = len(self.__unicode__())
return 0 if element_characters > 255 else 255 - element_characters
def __line_length(self, line):
"""@TODO Write docs.
:type line: str
:rtype: int
"""
total_characters = len(line)
available_characters = self.__available_characters()
if total_characters <= available_characters:
return total_characters
spaces = 0
while spaces < available_characters and line[available_characters - spaces - 1] == ' ':
spaces += 1
if spaces == available_characters:
return available_characters
return available_characters - spaces
def __set_bounded_value(self, value):
"""@TODO Write docs.
:type value: str
:rtype: int
"""
line_length = self.__line_length(value)
self.set_value(value[:line_length])
return line_length
def __add_bounded_child(self, tag, value):
"""@TODO Write docs.
:type tag: str
:type value: str
:rtype: int
"""
child = self.new_child_element(tag)
return child.__set_bounded_value(value)
def __add_concatenation(self, string):
"""@TODO Write docs.
:rtype: str
"""
index = 0
size = len(string)
while index < size:
index += self.__add_bounded_child(GEDCOM_TAG_CONCATENATION, string[index:])
def set_multi_line_value(self, value):
"""Set the value of this element, adding continuation lines as necessary
:type value: str
"""
self.set_value('')
self.get_child_elements()[:] = [child for child in self.get_child_elements() if
child.get_tag() not in (GEDCOM_TAG_CONCATENATION, GEDCOM_TAG_CONTINUED)]
lines = value.splitlines()
if lines:
line = lines.pop(0)
n = self.__set_bounded_value(line)
self.__add_concatenation(line[n:])
for line in lines:
n = self.__add_bounded_child(GEDCOM_TAG_CONTINUED, line)
self.__add_concatenation(line[n:])
def get_child_elements(self):
"""Return the child elements of this element
:rtype: list of Element
"""
return self.__children
def get_parent_element(self):
"""Return the parent element of this element
:rtype: Element
"""
return self.__parent
def new_child_element(self, tag, pointer="", value=""):
"""Create and return a new child element of this element
:type tag: str
:type pointer: str
:type value: str
:rtype: Element
"""
child_element = Element(self.get_level() + 1, pointer, tag, value, self.__crlf)
self.add_child_element(child_element)
return child_element
def add_child_element(self, element):
"""Add a child element to this element
:type element: Element
"""
self.get_child_elements().append(element)
element.set_parent_element(self)
def set_parent_element(self, element):
"""Add a parent element to this element
There's usually no need to call this method manually,
add_child_element() calls it automatically.
:type element: Element
"""
self.__parent = element
def is_individual(self):
"""Check if this element is an individual
:rtype: bool
"""
return self.get_tag() == GEDCOM_TAG_INDIVIDUAL
def is_source(self):
"""Check if this element is a source
:rtype: bool
"""
return self.get_tag() == GEDCOM_TAG_SOURCE
def is_family(self):
"""Check if this element is a family
:rtype: bool
"""
return self.get_tag() == GEDCOM_TAG_FAMILY
def is_file(self):
"""Check if this element is a file
:rtype: bool
"""
return self.get_tag() == GEDCOM_TAG_FILE
def is_object(self):
"""Check if this element is an object
:rtype: bool
"""
return self.get_tag() == GEDCOM_TAG_OBJECT
# criteria matching
def criteria_match(self, criteria):
"""Check in this element matches all of the given criteria
`criteria` is a colon-separated list, where each item in the
list has the form [name]=[value]. The following criteria are supported:
surname=[name]
Match a person with [name] in any part of the surname.
name=[name]
Match a person with [name] in any part of the given name.
birth=[year]
Match a person whose birth year is a four-digit [year].
birth_range=[from_year-to_year]
Match a person whose birth year is in the range of years from
[from_year] to [to_year], including both [from_year] and [to_year].
death=[year]
death_range=[from_year-to_year]
:type criteria: str
:rtype: bool
"""
# error checking on the criteria
try:
for criterion in criteria.split(':'):
key, value = criterion.split('=')
except:
return False
match = True
for criterion in criteria.split(':'):
key, value = criterion.split('=')
if key == "surname" and not self.surname_match(value):
match = False
elif key == "name" and not self.given_match(value):
match = False
elif key == "birth":
try:
year = int(value)
if not self.birth_year_match(year):
match = False
except:
match = False
elif key == "birth_range":
try:
from_year, to_year = value.split('-')
from_year = int(from_year)
to_year = int(to_year)
if not self.birth_range_match(from_year, to_year):
match = False
except:
match = False
elif key == "death":
try:
year = int(value)
if not self.death_year_match(year):
match = False
except:
match = False
elif key == "death_range":
try:
from_year, to_year = value.split('-')
from_year = int(from_year)
to_year = int(to_year)
if not self.death_range_match(from_year, to_year):
match = False
except:
match = False
return match
def surname_match(self, name):
"""Match a string with the surname of an individual
:type name: str
:rtype: bool
"""
(first, last) = self.get_name()
return last.find(name) >= 0
def given_match(self, name):
"""Match a string with the given names of an individual
:type name: str
:rtype: bool
"""
(first, last) = self.get_name()
return first.find(name) >= 0
def birth_year_match(self, year):
"""Match the birth year of an individual
:type year: int
:rtype: bool
"""
return self.get_birth_year() == year
def birth_range_match(self, from_year, to_year):
"""Check if the birth year of an individual is in a given range
:type from_year: int
:type to_year: int
:rtype: bool
"""
birth_year = self.get_birth_year()
if from_year <= birth_year <= to_year:
return True
return False
def death_year_match(self, year):
"""Match the death year of an individual.
:type year: int
:rtype: bool
"""
return self.get_death_year() == year
def death_range_match(self, from_year, to_year):
"""Check if the death year of an individual is in a given range. Years are integers
:type from_year: int
:type to_year: int
:rtype: bool
"""
death_year = self.get_death_year()
if from_year <= death_year <= to_year:
return True
return False
def get_source_title(self):
title = ""
if not self.is_source():
return title
for child in self.get_child_elements():
if child.get_tag() == GEDCOM_TAG_TITLE:
title = child.get_value()
return title
def get_source_publication(self):
publication = ""
if not self.is_source():
return publication
for child in self.get_child_elements():
if child.get_tag() == GEDCOM_TAG_PUBLICATION:
publication = child.get_value()
return publication
def get_name(self):
"""Return a person's names as a tuple: (first, last, nick)
:rtype: tuple
"""
first = ""
last = ""
nick = ""
if not self.is_individual():
return first, last
for child in self.get_child_elements():
if child.get_tag() == GEDCOM_TAG_NAME:
# some older GEDCOM files don't use child tags but instead
# place the name in the value of the NAME tag
if child.get_value() != "":
name = child.get_value().split('/')
if len(name) > 0:
first = name[0].strip()
if len(name) > 1:
last = name[1].strip()
else:
for childOfChild in child.get_child_elements():
if childOfChild.get_tag() == GEDCOM_TAG_GIVEN_NAME:
first = childOfChild.get_value()
if childOfChild.get_tag() == GEDCOM_TAG_SURNAME:
last = childOfChild.get_value()
if childOfChild.get_tag() == GEDCOM_TAG_NICKNAME:
nick = childOfChild.get_value()
for childOfChild in child.get_child_elements():
if childOfChild.get_tag() == GEDCOM_TAG_NICKNAME:
nick = childOfChild.get_value()
return first, last, nick
def get_gender(self):
"""Return the gender of a person in string format
:rtype: str
"""
gender = ""
if not self.is_individual():
return gender
for child in self.get_child_elements():
if child.get_tag() == GEDCOM_TAG_SEX:
gender = child.get_value()
return gender
def is_private(self):
"""Return if the person is marked private in boolean format
:rtype: bool
"""
private = False
if not self.is_individual():
return private
for child in self.get_child_elements():
if child.get_tag() == GEDCOM_TAG_PRIVATE:
private = child.get_value()
if private == 'Y':
private = True
return private
def get_birth_data(self):
"""Return the birth tuple of a person as (date, place, sources)
:rtype: tuple
"""
date = ""
place = ""
sources = []
if not self.is_individual():
return date, place, sources
for child in self.get_child_elements():
if child.get_tag() == GEDCOM_TAG_BIRTH:
for childOfChild in child.get_child_elements():
if childOfChild.get_tag() == GEDCOM_TAG_DATE:
date = childOfChild.get_value()
if childOfChild.get_tag() == GEDCOM_TAG_PLACE:
place = childOfChild.get_value()
if childOfChild.get_tag() == GEDCOM_TAG_SOURCE:
sources.append(childOfChild.get_value())
return date, place, sources
def get_birth_year(self):
"""Return the birth year of a person in integer format
:rtype: int
"""
date = ""
if not self.is_individual():
return date
for child in self.get_child_elements():
if child.get_tag() == GEDCOM_TAG_BIRTH: