~rosco2/ubuntu/wily/gramps/bug-1492304

« back to all changes in this revision

Viewing changes to src/Relationship.py

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2004-06-16 16:53:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040616165336-kjzczqef4gnxrn2b
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Gramps - a GTK+/GNOME based genealogy program
 
3
#
 
4
# Copyright (C) 2003-2004  Donald N. Allingham
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
 
 
21
# $Id: Relationship.py,v 1.12 2004/01/23 14:44:28 rshura Exp $
 
22
 
 
23
#-------------------------------------------------------------------------
 
24
#
 
25
# GRAMPS modules
 
26
#
 
27
#-------------------------------------------------------------------------
 
28
 
 
29
import RelLib
 
30
import GrampsCfg
 
31
from gettext import gettext as _
 
32
 
 
33
#-------------------------------------------------------------------------
 
34
#
 
35
#
 
36
#
 
37
#-------------------------------------------------------------------------
 
38
 
 
39
_level_name = [ "", "first", "second", "third", "fourth", "fifth", "sixth",
 
40
                "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth",
 
41
                "thirteenth", "fourteenth", "fifteenth", "sixteenth",
 
42
                "seventeenth", "eighteenth", "nineteenth", "twentieth" ]
 
43
 
 
44
_removed_level = [ "", " once removed", " twice removed", " three times removed",
 
45
                   " four times removed", " five times removed", " six times removed",
 
46
                   " sevent times removed", " eight times removed", " nine times removed",
 
47
                   " ten times removed", " eleven times removed", " twelve times removed",
 
48
                   " thirteen times removed", " fourteen times removed", " fifteen times removed",
 
49
                   " sixteen times removed", " seventeen times removed", " eighteen times removed",
 
50
                   " nineteen times removed", " twenty times removed" ]
 
51
 
 
52
_parents_level = [ "", "parents", "grandparents", "great grandparents", "second great grandparents",
 
53
                  "third great grandparents",  "fourth great grandparents",
 
54
                  "fifth great grandparents",  "sixth great grandparents",
 
55
                  "seventh great grandparents", "eighth great grandparents",
 
56
                  "ninth great grandparents", "tenth great grandparents",
 
57
                  "eleventh great grandparents",  "twelfth great grandparents",
 
58
                  "thirteenth great grandparents", "fourteenth great grandparents",
 
59
                  "fifteenth great grandparents", "sixteenth great grandparents",
 
60
                  "seventeenth great grandparents", "eighteenth great grandparents",
 
61
                  "nineteenth great grandparents", "twentieth great grandparents", ]
 
62
 
 
63
_father_level = [ "", "father", "grandfather", "great grandfather", "second great grandfather",
 
64
                  "third great grandfather",  "fourth great grandfather",
 
65
                  "fifth great grandfather",  "sixth great grandfather",
 
66
                  "seventh great grandfather", "eighth great grandfather",
 
67
                  "ninth great grandfather", "tenth great grandfather",
 
68
                  "eleventh great grandfather",  "twelfth great grandfather",
 
69
                  "thirteenth great grandfather", "fourteenth great grandfather",
 
70
                  "fifteenth great grandfather", "sixteenth great grandfather",
 
71
                  "seventeenth great grandfather", "eighteenth great grandfather",
 
72
                  "nineteenth great grandfather", "twentieth great grandfather", ]
 
73
 
 
74
_mother_level = [ "", "mother", "grandmother", "great grandmother", "second great grandmother",
 
75
                  "third great grandmother",  "fourth great grandmother",
 
76
                  "fifth great grandmother",  "sixth great grandmother",
 
77
                  "seventh great grandmother", "eighth great grandmother",
 
78
                  "ninth great grandmother", "tenth great grandmother",
 
79
                  "eleventh great grandmother",  "twelfth great grandmother",
 
80
                  "thirteenth great grandmother", "fourteenth great grandmother",
 
81
                  "fifteenth great grandmother", "sixteenth great grandmother",
 
82
                  "seventeenth great grandmother", "eighteenth great grandmother",
 
83
                  "nineteenth great grandmother", "twentieth great grandmother", ]
 
84
 
 
85
_son_level = [ "", "son", "grandson", "great grandson", "second great grandson",
 
86
               "third great grandson",  "fourth great grandson",
 
87
               "fifth great grandson",  "sixth great grandson",
 
88
               "seventh great grandson", "eighth great grandson",
 
89
               "ninth great grandson", "tenth great grandson",
 
90
               "eleventh great grandson",  "twelfth great grandson",
 
91
               "thirteenth great grandson", "fourteenth great grandson",
 
92
               "fifteenth great grandson", "sixteenth great grandson",
 
93
               "seventeenth great grandson", "eighteenth great grandson",
 
94
               "nineteenth great grandson", "twentieth great grandson", ]
 
95
 
 
96
_daughter_level = [ "", "daughter", "granddaughter", "great granddaughter",
 
97
                    "second great granddaughter",
 
98
                    "third great granddaughter",  "fourth great granddaughter",
 
99
                    "fifth great granddaughter",  "sixth great granddaughter",
 
100
                    "seventh great granddaughter", "eighth great granddaughter",
 
101
                    "ninth great granddaughter", "tenth great granddaughter",
 
102
                    "eleventh great granddaughter",  "twelfth great granddaughter",
 
103
                    "thirteenth great granddaughter", "fourteenth great granddaughter",
 
104
                    "fifteenth great granddaughter", "sixteenth great granddaughter",
 
105
                    "seventeenth great granddaughter", "eighteenth great granddaughter",
 
106
                    "nineteenth great granddaughter", "twentieth great granddaughter", ]
 
107
 
 
108
_sister_level = [ "", "sister", "aunt", "grandaunt", "great grandaunt", "second great grandaunt",
 
109
                  "third great grandaunt",  "fourth great grandaunt",
 
110
                  "fifth great grandaunt",  "sixth great grandaunt",
 
111
                  "seventh great grandaunt", "eighth great grandaunt",
 
112
                  "ninth great grandaunt", "tenth great grandaunt",
 
113
                  "eleventh great grandaunt",  "twelfth great grandaunt",
 
114
                  "thirteenth great grandaunt", "fourteenth great grandaunt",
 
115
                  "fifteenth great grandaunt", "sixteenth great grandaunt",
 
116
                  "seventeenth great grandaunt", "eighteenth great grandaunt",
 
117
                  "nineteenth great grandaunt", "twentieth great grandaunt", ]
 
118
 
 
119
_brother_level = [ "", "brother", "uncle", "granduncle", "great granduncle", "second great granduncle",
 
120
                   "third great granduncle",  "fourth great granduncle",
 
121
                   "fifth great granduncle",  "sixth great granduncle",
 
122
                   "seventh great granduncle", "eighth great granduncle",
 
123
                   "ninth great granduncle", "tenth great granduncle",
 
124
                   "eleventh great granduncle",  "twelfth great granduncle",
 
125
                   "thirteenth great granduncle", "fourteenth great granduncle",
 
126
                   "fifteenth great granduncle", "sixteenth great granduncle",
 
127
                   "seventeenth great granduncle", "eighteenth great granduncle",
 
128
                   "nineteenth great granduncle", "twentieth great granduncle", ]
 
129
 
 
130
_nephew_level = [ "", "nephew", "grandnephew", "great grandnephew", "second great grandnephew",
 
131
                  "third great grandnephew",  "fourth great grandnephew",
 
132
                  "fifth great grandnephew",  "sixth great grandnephew",
 
133
                  "seventh great grandnephew", "eighth great grandnephew",
 
134
                  "ninth great grandnephew", "tenth great grandnephew",
 
135
                  "eleventh great grandnephew",  "twelfth great grandnephew",
 
136
                  "thirteenth great grandnephew", "fourteenth great grandnephew",
 
137
                  "fifteenth great grandnephew", "sixteenth great grandnephew",
 
138
                  "seventeenth great grandnephew", "eighteenth great grandnephew",
 
139
                  "nineteenth great grandnephew", "twentieth great grandnephew", ]
 
140
 
 
141
_niece_level = [ "", "niece", "grandniece", "great grandniece", "second great grandniece",
 
142
                 "third great grandniece",  "fourth great grandniece",
 
143
                 "fifth great grandniece",  "sixth great grandniece",
 
144
                 "seventh great grandniece", "eighth great grandniece",
 
145
                 "ninth great grandniece", "tenth great grandniece",
 
146
                 "eleventh great grandniece",  "twelfth great grandniece",
 
147
                 "thirteenth great grandniece", "fourteenth great grandniece",
 
148
                 "fifteenth great grandniece", "sixteenth great grandniece",
 
149
                 "seventeenth great grandniece", "eighteenth great grandniece",
 
150
                 "nineteenth great grandniece", "twentieth great grandniece", ]
 
151
 
 
152
#-------------------------------------------------------------------------
 
153
#
 
154
#
 
155
#
 
156
#-------------------------------------------------------------------------
 
157
def apply_filter(person,index,plist,pmap):
 
158
    if person == None:
 
159
        return
 
160
    plist.append(person)
 
161
    pmap[person.getId()] = index
 
162
    
 
163
    family = person.getMainParents()
 
164
    if family != None:
 
165
        apply_filter(family.getFather(),index+1,plist,pmap)
 
166
        apply_filter(family.getMother(),index+1,plist,pmap)
 
167
 
 
168
def get_cousin(level,removed):
 
169
    if removed > len(_removed_level)-1 or level>len(_level_name)-1:
 
170
        return "distant relative"
 
171
    else:
 
172
        return "%s cousin%s" % (_level_name[level],_removed_level[removed])
 
173
 
 
174
def get_parents(level):
 
175
    if level>len(_parents_level)-1:
 
176
        return "distant ancestors"
 
177
    else:
 
178
        return _parents_level[level]
 
179
 
 
180
def get_father(level):
 
181
    if level>len(_father_level)-1:
 
182
        return "distant ancestor"
 
183
    else:
 
184
        return _father_level[level]
 
185
 
 
186
def get_son(level):
 
187
    if level>len(_son_level)-1:
 
188
        return "distant descendant"
 
189
    else:
 
190
        return _son_level[level]
 
191
 
 
192
def get_mother(level):
 
193
    if level>len(_mother_level)-1:
 
194
        return "distant ancestor"
 
195
    else:
 
196
        return _mother_level[level]
 
197
 
 
198
def get_daughter(level):
 
199
    if level>len(_daughter_level)-1:
 
200
        return "distant descendant"
 
201
    else:
 
202
        return _daughter_level[level]
 
203
 
 
204
def get_aunt(level):
 
205
    if level>len(_sister_level)-1:
 
206
        return "distant ancestor"
 
207
    else:
 
208
        return _sister_level[level]
 
209
 
 
210
def get_uncle(level):
 
211
    if level>len(_brother_level)-1:
 
212
        return "distant ancestor"
 
213
    else:
 
214
        return _brother_level[level]
 
215
 
 
216
def get_nephew(level):
 
217
    if level>len(_nephew_level)-1:
 
218
        return "distant descendant"
 
219
    else:
 
220
        return _nephew_level[level]
 
221
 
 
222
def get_niece(level):
 
223
    if level>len(_niece_level)-1:
 
224
        return "distant descendant"
 
225
    else:
 
226
        return _niece_level[level]
 
227
 
 
228
def is_spouse(orig,other):
 
229
    for f in orig.getFamilyList():
 
230
        if other == f.getFather() or other == f.getMother():
 
231
            return 1
 
232
    return 0
 
233
 
 
234
def get_relationship(orig_person,other_person):
 
235
    """
 
236
    returns a string representing the relationshp between the two people,
 
237
    along with a list of common ancestors (typically father,mother) 
 
238
    """
 
239
    firstMap = {}
 
240
    firstList = []
 
241
    secondMap = {}
 
242
    secondList = []
 
243
    common = []
 
244
    rank = 9999999
 
245
 
 
246
    if orig_person == None:
 
247
        return ("undefined",[])
 
248
 
 
249
    if orig_person == other_person:
 
250
        return ('', [])
 
251
    if is_spouse(orig_person,other_person):
 
252
        return ("spouse",[])
 
253
 
 
254
    try:
 
255
        apply_filter(orig_person,0,firstList,firstMap)
 
256
        apply_filter(other_person,0,secondList,secondMap)
 
257
    except RuntimeError,msg:
 
258
        return (_("Relationship loop detected"),None)
 
259
    
 
260
    for person in firstList:
 
261
        if person in secondList:
 
262
            new_rank = firstMap[person.getId()]
 
263
            if new_rank < rank:
 
264
                rank = new_rank
 
265
                common = [ person ]
 
266
            elif new_rank == rank:
 
267
                common.append(person)
 
268
 
 
269
    firstRel = -1
 
270
    secondRel = -1
 
271
 
 
272
    length = len(common)
 
273
    
 
274
    if length == 1:
 
275
        person = common[0]
 
276
        secondRel = firstMap[person.getId()]
 
277
        firstRel = secondMap[person.getId()]
 
278
    elif length == 2:
 
279
        p1 = common[0]
 
280
        secondRel = firstMap[p1.getId()]
 
281
        firstRel = secondMap[p1.getId()]
 
282
    elif length > 2:
 
283
        person = common[0]
 
284
        secondRel = firstMap[person.getId()]
 
285
        firstRel = secondMap[person.getId()]
 
286
 
 
287
    if firstRel == -1:
 
288
        return ("",[])
 
289
    elif firstRel == 0:
 
290
        if secondRel == 0:
 
291
            return ('',common)
 
292
        elif other_person.getGender() == RelLib.Person.male:
 
293
            return (get_father(secondRel),common)
 
294
        else:
 
295
            return (get_mother(secondRel),common)
 
296
    elif secondRel == 0:
 
297
        if other_person.getGender() == RelLib.Person.male:
 
298
            return (get_son(firstRel),common)
 
299
        else:
 
300
            return (get_daughter(firstRel),common)
 
301
    elif firstRel == 1:
 
302
        if other_person.getGender() == RelLib.Person.male:
 
303
            return (get_uncle(secondRel),common)
 
304
        else:
 
305
            return (get_aunt(secondRel),common)
 
306
    elif secondRel == 1:
 
307
        if other_person.getGender() == RelLib.Person.male:
 
308
            return (get_nephew(firstRel-1),common)
 
309
        else:
 
310
            return (get_niece(firstRel-1),common)
 
311
    else:
 
312
        if secondRel > firstRel:
 
313
            return (get_cousin(firstRel-1,secondRel-firstRel),common)
 
314
        else:
 
315
            return (get_cousin(secondRel-1,firstRel-secondRel),common)
 
316
 
 
317
def get_grandparents_string(orig_person,other_person):
 
318
    """
 
319
    returns a string representing the relationshp between the two people,
 
320
    along with a list of common ancestors (typically father,mother) 
 
321
    """
 
322
    firstMap = {}
 
323
    firstList = []
 
324
    secondMap = {}
 
325
    secondList = []
 
326
    common = []
 
327
    rank = 9999999
 
328
 
 
329
    if orig_person == None:
 
330
        return ("undefined",[])
 
331
 
 
332
    if orig_person == other_person:
 
333
        return ('', [])
 
334
 
 
335
    apply_filter(orig_person,0,firstList,firstMap)
 
336
    apply_filter(other_person,0,secondList,secondMap)
 
337
    
 
338
    for person in firstList:
 
339
        if person in secondList:
 
340
            new_rank = firstMap[person.getId()]
 
341
            if new_rank < rank:
 
342
                rank = new_rank
 
343
                common = [ person ]
 
344
            elif new_rank == rank:
 
345
                common.append(person)
 
346
 
 
347
    firstRel = -1
 
348
    secondRel = -1
 
349
 
 
350
    length = len(common)
 
351
    
 
352
    if length == 1:
 
353
        person = common[0]
 
354
        secondRel = firstMap[person.getId()]
 
355
        firstRel = secondMap[person.getId()]
 
356
    elif length == 2:
 
357
        p1 = common[0]
 
358
        secondRel = firstMap[p1.getId()]
 
359
        firstRel = secondMap[p1.getId()]
 
360
    elif length > 2:
 
361
        person = common[0]
 
362
        secondRel = firstMap[person.getId()]
 
363
        firstRel = secondMap[person.getId()]
 
364
 
 
365
    if firstRel == 0:
 
366
        if secondRel == 0:
 
367
            return ('',common)
 
368
        else:
 
369
            return (get_parents(secondRel),common)
 
370
    else:
 
371
        return None