svn: r13975
This commit is contained in:
Jérôme Rapinat 2010-01-05 18:07:09 +00:00
parent 57ac58b67e
commit 9ae34b4ba8
2 changed files with 82 additions and 75 deletions

View File

@ -19,7 +19,9 @@
# #
# $Id:Relationship.py 9912 2008-01-22 09:17:46Z acraphae $ # $Id:Relationship.py 9912 2008-01-22 09:17:46Z acraphae $
"""
Classes for relationships.
"""
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# python modules # python modules
@ -791,7 +793,7 @@ class RelationshipCalculator(object):
self.__apply_filter(db, other_person, '', [], secondMap, self.__apply_filter(db, other_person, '', [], secondMap,
stoprecursemap = firstMap) stoprecursemap = firstMap)
except RuntimeError: except RuntimeError:
return (-1,None,-1,[],-1,[] ) , \ return (-1, None, -1, [], -1, []) , \
[_("Relationship loop detected")] + self.__msg [_("Relationship loop detected")] + self.__msg
if self.storemap: if self.storemap:
@ -815,8 +817,8 @@ class RelationshipCalculator(object):
#insert common ancestor in correct position, #insert common ancestor in correct position,
# if shorter links, check if not subset # if shorter links, check if not subset
# if longer links, check if not superset # if longer links, check if not superset
pos=0 pos = 0
for ranknew, handlenew,rel1new,fam1new,rel2new,fam2new in com : for ranknew, handlenew, rel1new, fam1new, rel2new, fam2new in com :
insert = True insert = True
for rank, handle, rel1, fam1, rel2, fam2 in common : for rank, handle, rel1, fam1, rel2, fam2 in common :
if ranknew < rank : if ranknew < rank :
@ -832,14 +834,14 @@ class RelationshipCalculator(object):
if insert : if insert :
if common : if common :
common.insert(pos, (ranknew, handlenew, common.insert(pos, (ranknew, handlenew,
rel1new, fam1new,rel2new,fam2new)) rel1new, fam1new, rel2new, fam2new))
else: else:
common = [(ranknew, handlenew, common = [(ranknew, handlenew,
rel1new, fam1new, rel2new, fam2new)] rel1new, fam1new, rel2new, fam2new)]
#now check if superset must be deleted from common #now check if superset must be deleted from common
deletelist=[] deletelist = []
index = pos+1 index = pos+1
for rank, handle,rel1,fam1,rel2,fam2 in common[pos+1:]: for rank, handle, rel1, fam1, rel2, fam2 in common[pos+1:]:
if rel1new == rel1[:len(rel1new)] and \ if rel1new == rel1[:len(rel1new)] and \
rel2new == rel2[:len(rel2new)] : rel2new == rel2[:len(rel2new)] :
deletelist.append(index) deletelist.append(index)
@ -860,16 +862,16 @@ class RelationshipCalculator(object):
firstFam = common[0][3] firstFam = common[0][3]
secondRel = common[0][4] secondRel = common[0][4]
secondFam = common[0][5] secondFam = common[0][5]
return (rank,person_handle,firstRel,firstFam,secondRel,secondFam),\ return (rank, person_handle, firstRel, firstFam, secondRel, secondFam),\
self.__msg self.__msg
if common : if common :
#list with tuples (rank, handle person,rel_str_orig,rel_fam_orig, #list with tuples (rank, handle person,rel_str_orig,rel_fam_orig,
# rel_str_other,rel_fam_str) and messages # rel_str_other,rel_fam_str) and messages
return common, self.__msg return common, self.__msg
if not self.__all_dist : if not self.__all_dist :
return (-1,None,'',[],'',[]), self.__msg return (-1, None, '', [], '', []), self.__msg
else : else :
return [(-1,None,'',[],'',[])], self.__msg return [(-1, None, '', [], '', [])], self.__msg
def __apply_filter(self, db, person, rel_str, rel_fam, pmap, def __apply_filter(self, db, person, rel_str, rel_fam, pmap,
depth=1, stoprecursemap=None): depth=1, stoprecursemap=None):
@ -927,7 +929,7 @@ class RelationshipCalculator(object):
rel2[len(rel1):])] rel2[len(rel1):])]
return return
elif store: elif store:
pmap[person.handle] = [[rel_str],[rel_fam]] pmap[person.handle] = [[rel_str], [rel_fam]]
#having added person to the pmap, we only look up recursively to #having added person to the pmap, we only look up recursively to
# parents if this person is not common relative # parents if this person is not common relative
@ -999,7 +1001,7 @@ class RelationshipCalculator(object):
pmap[chandle][1] += [rel_fam_new] pmap[chandle][1] += [rel_fam_new]
#person is already a grandparent in another branch #person is already a grandparent in another branch
else: else:
pmap[chandle] = [[rel_str+addstr],[rel_fam_new]] pmap[chandle] = [[rel_str+addstr], [rel_fam_new]]
fam += 1 fam += 1
for handle, data in parentstodo.iteritems(): for handle, data in parentstodo.iteritems():
@ -1899,7 +1901,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting nieces\n' print '\n\ntesting nieces\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstr = _rand_relstr(i,'m') relstr = _rand_relstr(i,'m')
rel = FMT % rc.get_single_relationship_string(1, i, rel = FMT % rc.get_single_relationship_string(1, i,
gen.lib.Person.FEMALE, gen.lib.Person.FEMALE,
@ -1913,7 +1915,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting nephews\n' print '\n\ntesting nephews\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstr = _rand_relstr(i,'f') relstr = _rand_relstr(i,'f')
rel = FMT % rc.get_single_relationship_string(1, i, rel = FMT % rc.get_single_relationship_string(1, i,
gen.lib.Person.FEMALE, gen.lib.Person.FEMALE,
@ -1927,7 +1929,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting unknown nephews/nieces\n' print '\n\ntesting unknown nephews/nieces\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstr = _rand_relstr(i,'f') relstr = _rand_relstr(i,'f')
rel = FMT % rc.get_single_relationship_string(1, i, rel = FMT % rc.get_single_relationship_string(1, i,
gen.lib.Person.FEMALE, gen.lib.Person.FEMALE,
@ -1941,7 +1943,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting uncles\n' print '\n\ntesting uncles\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstr = _rand_relstr(i,'f') relstr = _rand_relstr(i,'f')
rel = FMT % rc.get_single_relationship_string(i, 1, rel = FMT % rc.get_single_relationship_string(i, 1,
gen.lib.Person.FEMALE, gen.lib.Person.FEMALE,
@ -1955,7 +1957,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting aunts\n' print '\n\ntesting aunts\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstr = _rand_relstr(i,'f') relstr = _rand_relstr(i,'f')
rel = FMT % rc.get_single_relationship_string(i, 1, rel = FMT % rc.get_single_relationship_string(i, 1,
gen.lib.Person.MALE, gen.lib.Person.MALE,
@ -1969,7 +1971,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting unknown uncles/aunts\n' print '\n\ntesting unknown uncles/aunts\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstr = _rand_relstr(i,'m') relstr = _rand_relstr(i,'m')
rel = FMT % rc.get_single_relationship_string(i, 1, rel = FMT % rc.get_single_relationship_string(i, 1,
gen.lib.Person.MALE, gen.lib.Person.MALE,
@ -1983,7 +1985,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting male cousins same generation\n' print '\n\ntesting male cousins same generation\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstra = _rand_relstr(i,'f') relstra = _rand_relstr(i,'f')
relstrb = _rand_relstr(i,'f') relstrb = _rand_relstr(i,'f')
rel = FMT % rc.get_single_relationship_string(i, i, rel = FMT % rc.get_single_relationship_string(i, i,
@ -1999,7 +2001,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting female cousins same generation\n' print '\n\ntesting female cousins same generation\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstra = _rand_relstr(i,'m') relstra = _rand_relstr(i,'m')
relstrb = _rand_relstr(i,'m') relstrb = _rand_relstr(i,'m')
rel = FMT % rc.get_single_relationship_string(i, i, rel = FMT % rc.get_single_relationship_string(i, i,
@ -2015,7 +2017,7 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting unknown cousins same generation\n' print '\n\ntesting unknown cousins same generation\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
relstra = _rand_relstr(i,'m') relstra = _rand_relstr(i,'m')
relstrb = _rand_relstr(i,'m') relstrb = _rand_relstr(i,'m')
rel = FMT % rc.get_single_relationship_string(i, i, rel = FMT % rc.get_single_relationship_string(i, i,
@ -2033,8 +2035,8 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
sys.stdin.readline() sys.stdin.readline()
import random import random
random.seed() random.seed()
for i in range(1,MAX) : for i in range(1, MAX) :
for j in range (i,MAX) : for j in range (i, MAX) :
rnd = random.randint(0, 100) rnd = random.randint(0, 100)
if rnd < 10 : if rnd < 10 :
relstra = _rand_relstr(j,'f') relstra = _rand_relstr(j,'f')
@ -2065,8 +2067,8 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr):
print rel print rel
print '\n\ntesting some cousins down\n' print '\n\ntesting some cousins down\n'
sys.stdin.readline() sys.stdin.readline()
for i in range(1,MAX) : for i in range(1, MAX) :
for j in range (i,MAX) : for j in range (i, MAX) :
rnd = random.randint(0, 100) rnd = random.randint(0, 100)
if rnd < 10 : if rnd < 10 :
relstra = _rand_relstr(i,'f') relstra = _rand_relstr(i,'f')

View File

@ -23,7 +23,9 @@
# $Id:rel_cs.py 9912 2008-01-22 09:17:46Z acraphae $ # $Id:rel_cs.py 9912 2008-01-22 09:17:46Z acraphae $
# Czech terms added by Zdeněk Hataš. Based on rel_sk.py by Lubo Vasko # Czech terms added by Zdeněk Hataš. Based on rel_sk.py by Lubo Vasko
"""
Czech-specific classes for relationships.
"""
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# GRAMPS modules # GRAMPS modules
@ -71,77 +73,80 @@ _niece_level = [ "", "neteř", "praneteř", "prapraneteř", ]
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class RelationshipCalculator(Relationship.RelationshipCalculator): class RelationshipCalculator(Relationship.RelationshipCalculator):
"""
RelationshipCalculator Class
"""
def __init__(self): def __init__(self):
Relationship.RelationshipCalculator.__init__(self) Relationship.RelationshipCalculator.__init__(self)
def get_male_cousin(self,level): def get_male_cousin(self, level):
if level>len(_level_name)-1: if level > len(_level_name)-1:
return "vzdálený příbuzný" return "vzdálený příbuzný"
else: else:
return "bratranec %s stupně" % (_level_name[level]) return "bratranec %s stupně" % (_level_name[level])
def get_female_cousin(self,level): def get_female_cousin(self, level):
if level>len(_level_name)-1: if level > len(_level_name)-1:
return "vzdálená příbuzná" return "vzdálená příbuzná"
else: else:
return "sestřenice %s stupně" % (_level_name[level]) return "sestřenice %s stupně" % (_level_name[level])
def get_parents(self,level): def get_parents(self, level):
if level>len(_parents_level)-1: if level > len(_parents_level)-1:
return "vzdáleení příbuzní" return "vzdáleení příbuzní"
else: else:
return _parents_level[level] return _parents_level[level]
def get_father(self,level): def get_father(self, level):
if level>len(_father_level)-1: if level > len(_father_level)-1:
return "vzdálený příbuzný" return "vzdálený příbuzný"
else: else:
return _father_level[level] return _father_level[level]
def get_son(self,level): def get_son(self, level):
if level>len(_son_level)-1: if level > len(_son_level)-1:
return "vzdálený potomek" return "vzdálený potomek"
else: else:
return _son_level[level] return _son_level[level]
def get_mother(self,level): def get_mother(self, level):
if level>len(_mother_level)-1: if level > len(_mother_level)-1:
return "vzdálený předek" return "vzdálený předek"
else: else:
return _mother_level[level] return _mother_level[level]
def get_daughter(self,level): def get_daughter(self, level):
if level>len(_daughter_level)-1: if level > len(_daughter_level)-1:
return "vzdálený potomek" return "vzdálený potomek"
else: else:
return _daughter_level[level] return _daughter_level[level]
def get_aunt(self,level): def get_aunt(self, level):
if level>len(_sister_level)-1: if level > len(_sister_level)-1:
return "vzdálený předek" return "vzdálený předek"
else: else:
return _sister_level[level] return _sister_level[level]
def get_uncle(self,level): def get_uncle(self, level):
if level>len(_brother_level)-1: if level > len(_brother_level)-1:
return "vzdálený předek" return "vzdálený předek"
else: else:
return _brother_level[level] return _brother_level[level]
def get_nephew(self,level): def get_nephew(self, level):
if level>len(_nephew_level)-1: if level > len(_nephew_level)-1:
return "vzdálený potomek" return "vzdálený potomek"
else: else:
return _nephew_level[level] return _nephew_level[level]
def get_niece(self,level): def get_niece(self, level):
if level>len(_niece_level)-1: if level > len(_niece_level)-1:
return "vzdálený potomek" return "vzdálený potomek"
else: else:
return _niece_level[level] return _niece_level[level]
def get_relationship(self,db, orig_person, other_person): def get_relationship(self, db, orig_person, other_person):
""" """
Return a string representing the relationshp between the two people, Return a string representing the relationshp between the two people,
along with a list of common ancestors (typically father,mother) along with a list of common ancestors (typically father,mother)
@ -150,75 +155,75 @@ class RelationshipCalculator(Relationship.RelationshipCalculator):
""" """
if orig_person is None: if orig_person is None:
return ("undefined",[]) return ("undefined", [])
if orig_person.get_handle() == other_person.get_handle(): if orig_person.get_handle() == other_person.get_handle():
return ('', []) return ('', [])
is_spouse = self.is_spouse(db, orig_person, other_person) is_spouse = self.is_spouse(db, orig_person, other_person)
if is_spouse: if is_spouse:
return (is_spouse,[]) return (is_spouse, [])
#get_relationship_distance changed, first data is relation to #get_relationship_distance changed, first data is relation to
#orig person, apperently secondRel in this function #orig person, apperently secondRel in this function
(secondRel,firstRel,common) = \ (secondRel, firstRel, common) = \
self.get_relationship_distance(db, orig_person, other_person) self.get_relationship_distance(db, orig_person, other_person)
if isinstance(common, basestring): if isinstance(common, basestring):
return (common,[]) return (common, [])
elif common: elif common:
person_handle = common[0] person_handle = common[0]
else: else:
return ("",[]) return ("", [])
firstRel = len(firstRel) firstRel = len(firstRel)
secondRel = len(secondRel) secondRel = len(secondRel)
if firstRel == 0: if firstRel == 0:
if secondRel == 0: if secondRel == 0:
return ('',common) return ('', common)
elif other_person.get_gender() == gen.lib.Person.MALE: elif other_person.get_gender() == gen.lib.Person.MALE:
return (self.get_father(secondRel),common) return (self.get_father(secondRel), common)
else: else:
return (self.get_mother(secondRel),common) return (self.get_mother(secondRel), common)
elif secondRel == 0: elif secondRel == 0:
if other_person.get_gender() == gen.lib.Person.MALE: if other_person.get_gender() == gen.lib.Person.MALE:
return (self.get_son(firstRel),common) return (self.get_son(firstRel), common)
else: else:
return (self.get_daughter(firstRel),common) return (self.get_daughter(firstRel), common)
elif firstRel == 1: elif firstRel == 1:
if other_person.get_gender() == gen.lib.Person.MALE: if other_person.get_gender() == gen.lib.Person.MALE:
return (self.get_uncle(secondRel),common) return (self.get_uncle(secondRel), common)
else: else:
return (self.get_aunt(secondRel),common) return (self.get_aunt(secondRel), common)
elif secondRel == 1: elif secondRel == 1:
if other_person.get_gender() == gen.lib.Person.MALE: if other_person.get_gender() == gen.lib.Person.MALE:
return (self.get_nephew(firstRel-1),common) return (self.get_nephew(firstRel-1), common)
else: else:
return (self.get_niece(firstRel-1),common) return (self.get_niece(firstRel-1), common)
elif firstRel == secondRel == 2: elif firstRel == secondRel == 2:
if other_person.get_gender() == gen.lib.Person.MALE: if other_person.get_gender() == gen.lib.Person.MALE:
return ('vlastní bratranec',common) return ('vlastní bratranec', common)
else: else:
return ('vlastní sestřenice',common) return ('vlastní sestřenice', common)
elif firstRel == 3 and secondRel == 2: elif firstRel == 3 and secondRel == 2:
if other_person.get_gender() == gen.lib.Person.MALE: if other_person.get_gender() == gen.lib.Person.MALE:
return ('bratranec druhého stupně',common) return ('bratranec druhého stupně', common)
else: else:
return ('sestřenice druhého stupně',common) return ('sestřenice druhého stupně', common)
elif firstRel == 2 and secondRel == 3: elif firstRel == 2 and secondRel == 3:
if other_person.get_gender() == gen.lib.Person.MALE: if other_person.get_gender() == gen.lib.Person.MALE:
return ('bratranec druhého stupně',common) return ('bratranec druhého stupně', common)
else: else:
return ('sestřenice druhého stupně',common) return ('sestřenice druhého stupně', common)
else: else:
if other_person.get_gender() == gen.lib.Person.MALE: if other_person.get_gender() == gen.lib.Person.MALE:
if firstRel+secondRel>len(_level_name)-1: if firstRel+secondRel > len(_level_name)-1:
return (self.get_male_cousin(firstRel+secondRel),common) return (self.get_male_cousin(firstRel+secondRel), common)
else: else:
return ('vzdálený bratranec',common) return ('vzdálený bratranec', common)
else: else:
if firstRel+secondRel>len(_level_name)-1: if firstRel+secondRel > len(_level_name)-1:
return (self.get_female_cousin(firstRel+secondRel),common) return (self.get_female_cousin(firstRel+secondRel), common)
else: else:
return ('vzdálená sestřenice',common) return ('vzdálená sestřenice', common)