forked from hgboldt/consanguinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollconsformatter.py
450 lines (369 loc) · 14.9 KB
/
collconsformatter.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
# CosFormatter - Services for formatting data for collapse consanguinity gramplet
#
# Copyright (C) 2021 Hans Boldt, 2023 ChrisR
#
# 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.
"""
Module consformatter.py
Services for formatting data for consanguinity gramplet
Exports:
class ConsFormatter
class SimpleStringBuffer
"""
#-------------------#
# Python modules #
#-------------------#
from html import escape
from math import log
from itertools import chain
# import pdb
#-------------------#
# Gramps modules #
#-------------------#
from gramps.gen.lib import Person, EventType
from gramps.gen.display.name import displayer as name_displayer
from gramps.gen.datehandler import get_date
from gramps.gen.relationship import get_relationship_calculator
from gramps.gen.utils.db import (get_birth_or_fallback, get_death_or_fallback)
from gramps.gen.config import config
from gramps.gen.utils.symbols import Symbols
from gramps.gen.const import GRAMPS_LOCALE as glocale
#-----------------------------------#
# Other related gramplet modules #
#-----------------------------------#
from pedigree import Pedigree
#------------------#
# Translation #
#------------------#
try:
from gramps.gen.const import GRAMPS_LOCALE as glocale # ChrisR/Claude de.po try
_trans = glocale.get_addon_translator(__file__)
_ = _trans.gettext
except ValueError:
_ = glocale.translation.sgettext
ngettext = glocale.translation.ngettext # else "nearby" comments are ignored
#-------------#
# Messages #
#-------------#
MSG_UNKNOWN_NAME = _('(unknown)')
MSG_RELATIONSHIP = _('Relationship:')
MSG_RELATIONSHIPS = _('Relationships:')
MSG_COMMON_ANC = _('"Common ancestor:')
MSG_COMMON_ANCS = _('Common ancestors:')
MSG_MORE_SPOUSE_RELS = _('More spouse relationships not shown.')
MSG_MORE_RELS = _('More relationships not shown.')
MSG_PED_COLLAPSE_ACTIVE = _('Pedigree collapse for active person')
MSG_NO_PED_COLLAPSE = _('No pedigree collapse found.')
MSG_PED_COLLAPSE_AT = _('Pedigree collapse at %(relationship)s:')
MSG_MORE_PED_COLLAPSE = _('More instances of pedigree collapse not shown.')
MSG_NO_PARTNERS = _('No partners.')
MSG_NOT_MARRIED = _('Not married.')
MSG_PARTNER = _('Partner:')
MSG_RELS_BET_ACTIVE_AND_PARTNER = \
_('Relationships between active person and partners')
MSG_NO_COMMON_ANCS = _('No common ancestors found.')
MSG_MORE_ANCESTORS = _('More ancestors not shown.')
TITLE_FORMAT = '<span size="larger" weight="bold" underline="single">%s</span>'
PED_COLLAPSE_LIMIT = 10
RELATIONSHIP_LIMIT = 8
#------------------------------#
# #
# SimpleStringBuffer class #
# #
#------------------------------#
class SimpleStringBuffer:
"""
Simple string buffer
"""
__slots__ = ['buffer']
def __init__(self, string=None):
"""
init()
"""
self.buffer = list()
if string:
self.buffer.append(string)
def __add__(self, string):
"""
Append string to buffer
"""
self.buffer.append(string)
return self
def __str__(self):
"""
Convert buffer to string
"""
return ''.join(self.buffer)
#------------------------------#
# #
# CosFormatter class #
# #
#------------------------------#
class ConsFormatter:
"""
Methods for formatting the consanguinity data.
"""
# class variables
relcalc = get_relationship_calculator()
symbols = config.get('utf8.in-use')
if symbols:
syms = Symbols()
death_symbols = syms.get_death_symbols()
death_symbol = config.get('utf8.death-symbol')
birth_symbol = syms.get_symbol_for_string(Symbols.SYMBOL_BIRTH)
baptism_symbol = syms.get_symbol_for_string(Symbols.SYMBOL_BAPTISM)
death_symbol = death_symbols[death_symbol][1]
burial_symbol = syms.get_symbol_for_string(Symbols.SYMBOL_BURIED)
else:
birth_symbol = '*'
baptism_symbol = '~'
death_symbol = '+'
burial_symbol = '[]'
def __init__(self, db, person_handle, pedigree, spouse_pedigrees):
"""
__init__()
"""
self.db = db
self.person_handle = person_handle
self.pedigree = pedigree
self.spouse_pedigrees = spouse_pedigrees
self.person = self.db.get_person_from_handle(person_handle)
self.gender = self.person.get_gender()
def get_title(self):
"""
Format title
"""
return self.format_person(self.db, self.person_handle,
'<span size="larger" weight="ultrabold">',
'</span>', link=False)
def get_pedigree_collapse(self):
"""
Format the pedigree collapse section
"""
outstr = SimpleStringBuffer((TITLE_FORMAT % MSG_PED_COLLAPSE_ACTIVE)
+ "\n\n")
# Any pedigree collapse?
if not self.pedigree.has_pedigree_collapse():
outstr += "<i>" + MSG_NO_PED_COLLAPSE + "</i>\n\n"
return str(outstr)
# List out ancestors who were cousins
ped_collapse = self.pedigree.determine_pedigree_collapse()
count = 0
for descnum in sorted(ped_collapse.keys()):
count += 1
if count > PED_COLLAPSE_LIMIT:
outstr += "<b><i>" + MSG_MORE_PED_COLLAPSE + "</i></b>\n"
break
comm_anc = ped_collapse[descnum]
# Process one common descendant
father_handle = self.pedigree.get_ancestor_by_number(descnum*2) \
.get_person_handle()
mother_handle = self.pedigree.get_ancestor_by_number(descnum*2+1) \
.get_person_handle()
gens = int(log(descnum, 2)) + 1
rel = self.relcalc.get_plural_relationship_string(gens, 0)
outstr += "<b>" + MSG_PED_COLLAPSE_AT % {'relationship': rel} \
+ "</b>\n"
# Print names of ancestors where pedigree collapse occurs
outstr += self.format_person(self.db, father_handle, "\t", "\n")
outstr += self.format_person(self.db, mother_handle, "\t", "\n")
# Order the common ancestors list by primary ancestor numbers
ordered_anc = self.pedigree.order_ancestor_list(comm_anc)
for (primnums, ancs) in ordered_anc.items():
outstr += self.format_common_anc_rels \
(0, ancs, (True if len(primnums) == 1 else False),
gens, Person.MALE, Person.FEMALE)
outstr += self.format_common_ancestor_names(self.pedigree,
primnums)
outstr += "\n"
return str(outstr)
def get_consanguinity(self):
"""
Format the consanguinity section
"""
outstr = SimpleStringBuffer((TITLE_FORMAT
% MSG_RELS_BET_ACTIVE_AND_PARTNER) + "\n")
if not self.spouse_pedigrees:
return str(outstr + "\n<i>"
+ self.no_spouses_string(self.person) + "</i>\n")
# Go through spouses
spouse_num = 0
for (spouse_handle, spouse_pedigree) in self.spouse_pedigrees:
# Get and print info for spouse
spouse_num += 1
spouse = self.db.get_person_from_handle(spouse_handle)
spouse_gender = spouse.get_gender()
outstr += self.format_person(self.db, spouse_handle,
"\n<b>" + MSG_PARTNER + "</b> ", "\n")
if not spouse_pedigree.has_pedigree_collapse():
outstr += "\t<i>" + MSG_NO_COMMON_ANCS + "</i>\n"
continue
# Look for pedigree collapse where common descendant is #1
ped_collapse = spouse_pedigree.determine_pedigree_collapse(1)
if not ped_collapse:
outstr += "\t<i>" + MSG_NO_COMMON_ANCS + "</i>\n"
continue
# We have relationship between active person and spouse
ordered_anc = spouse_pedigree.order_ancestor_list(ped_collapse[1])
count = 0
for (primnums, ancs) in ordered_anc.items():
count += 1
if count > PED_COLLAPSE_LIMIT:
outstr += "\t<b><i>" + MSG_MORE_SPOUSE_RELS + "</i></b>\n"
break
outstr += self.format_common_anc_rels \
(spouse_num, ancs,
(True if len(primnums) == 1 else False),
1, self.gender, spouse_gender)
outstr += self.format_common_ancestor_names(spouse_pedigree,
primnums)
return str(outstr)
def format_common_ancestor_names(self, pedigree, common_ancestors):
"""
Print out the names of one set of common ancestors.
"""
outstr = SimpleStringBuffer()
if len(common_ancestors) == 1:
outstr += "\t<b>" + MSG_COMMON_ANC + "</b>\n"
else:
outstr += "\t<b>" + MSG_COMMON_ANCS + "</b>\n"
for anc_num in common_ancestors:
ancestor = pedigree.get_ancestor_by_number(anc_num)
outstr += self.format_person(self.db,
ancestor.get_person_handle(),
"\t\t", "\n")
return str(outstr)
def format_common_anc_rels(self, ped_index,
rel, half, generations,
active_gender, spouse_gender):
"""
Print out list of ancestor relationships.
"""
outstr = SimpleStringBuffer()
# Count number of relationships
relnums = rel.keys()
plural = (len(relnums) > 1)
if plural:
outstr += "\t<b>" + MSG_RELATIONSHIPS + '</b> '
tabs = "\n\t\t\t"
else:
outstr += "\t<b>" + MSG_RELATIONSHIP + '</b> '
tabs = ''
relfun = self.relcalc.get_plural_relationship_string
rel_type = ' ½' if half else ''
# Loop through all relationships
count = 0
for relnum in relnums:
count += 1
if count > RELATIONSHIP_LIMIT:
outstr += "\t\t\t<b><i>" + MSG_MORE_RELS + "</i></b>\n"
break
rellist = rel[relnum]
num_rels = len(rellist)
ways = (" x%d" % num_rels) if num_rels > 1 else ''
if active_gender == Person.MALE:
relstr = relfun(relnum[0] - generations, relnum[1] - generations)
else:
relstr = relfun(relnum[1] - generations, relnum[0] - generations)
relstr = "%(relationship)s%(separator)s%(half)s%(ways)s" % \
{'relationship': relstr,
'separator': (',' if half or ways else ''),
'half': rel_type,
'ways': ways}
# Format relationship as clickable link
rellist_str = ''
for item in rellist:
rellist_str += ','.join([str(x) for x in chain.from_iterable(item)]) + ' '
href = 'N %d %s' % (ped_index, rellist_str)
outstr += tabs + '<a href="%s">%s</a>\n' % (href, relstr)
tabs = "\t\t\t"
return str(outstr)
def no_spouses_string(self, person):
"""
Determine if there are no spouses because person never married.
"""
# Look for death event, and then look for "unmarried" attribute.
death_event = get_death_or_fallback(self.db, person)
if not death_event:
return MSG_NO_PARTNERS
event_type = death_event.get_type()
if event_type != EventType.DEATH:
return MSG_NO_PARTNERS
attr_list = death_event.get_attribute_list()
for attr in attr_list:
if attr.get_type().string.upper() == 'UNMARRIED':
return MSG_NOT_MARRIED
return MSG_NO_PARTNERS
@classmethod
def format_person(cls, db, person_handle, prestr='', poststr='',
link=True, dates=True, split=False):
"""
Print out person.
"""
if person_handle:
person = db.get_person_from_handle(person_handle)
name = name_displayer.display_name(person.get_primary_name())
else:
name = MSG_UNKNOWN_NAME
dates = False
if link:
outstr = '<a href="P %s">%s</a>' % (person_handle, name)
else:
outstr = name
if dates:
datestr = cls.info_string(db, person, split)
if datestr:
outstr += ("\n" if split else ' ') + datestr
return prestr + outstr + poststr
@classmethod
def info_string(cls, db, person, split=False):
"""
Information string for a person, including date of birth (or baptism)
and date of death (or burial).
"""
bdate = cls.fmt_date(get_birth_or_fallback(db, person),
EventType.BIRTH)
ddate = cls.fmt_date(get_death_or_fallback(db, person),
EventType.DEATH)
if split:
if bdate and ddate:
return "%s\n%s" % (bdate, ddate)
return bdate if bdate else ddate if ddate else ''
if bdate and ddate:
return "(%s, %s)" % (bdate, ddate)
if bdate:
return "(%s)" % (bdate)
if ddate:
return "(%s)" % (ddate)
return ''
@classmethod
def fmt_date(cls, date, preferred_event_type):
"""
Format the given date.
"""
if not date:
return ''
sdate = get_date(date)
if not sdate:
return ''
sdate = escape(sdate)
date_type = date.get_type()
if preferred_event_type == EventType.BIRTH:
if date_type != preferred_event_type:
return "%s<i>%s</i>" % (cls.baptism_symbol, sdate)
return "%s%s" % (cls.birth_symbol, sdate)
if date_type != preferred_event_type:
return "%s<i>%s</i>" % (cls.burial_symbol, sdate)
return "%s%s" % (cls.death_symbol, sdate)