GEPS008: Create new module for tree utilities

svn: r19908
This commit is contained in:
Nick Hall 2012-06-23 23:37:30 +00:00
parent 5e1535e125
commit e484d54bd8
9 changed files with 137 additions and 107 deletions

View File

@ -214,6 +214,7 @@ src/gen/utils/callman.py
src/gen/utils/file.py
src/gen/utils/image.py
src/gen/utils/referent.py
src/gen/utils/tree.py
# gui - GUI code
src/gui/__init__.py

View File

@ -281,40 +281,6 @@ else:
conv_utf8_tosrtkey_ongtk = lambda x: locale.strxfrm(x)
conv_unicode_tosrtkey_ongtk = lambda x: locale.strxfrm(x)
#-------------------------------------------------------------------------
#
# Iterate over ancestors.
#
#-------------------------------------------------------------------------
def for_each_ancestor(db, start, func, data):
"""
Recursively iterate (breadth-first) over ancestors of
people listed in start.
Call func(data, pid) for the Id of each person encountered.
Exit and return 1, as soon as func returns true.
Return 0 otherwise.
"""
todo = start
done_ids = set()
while len(todo):
p_handle = todo.pop()
p = db.get_person_from_handle(p_handle)
# Don't process the same handle twice. This can happen
# if there is a cycle in the database, or if the
# initial list contains X and some of X's ancestors.
if p_handle in done_ids:
continue
done_ids.add(p_handle)
if func(data, p_handle):
return 1
for fam_handle in p.get_parent_family_handle_list():
fam = db.get_family_from_handle(fam_handle)
if fam:
f_handle = fam.get_father_handle()
m_handle = fam.get_mother_handle()
if f_handle: todo.append(f_handle)
if m_handle: todo.append(m_handle)
return 0
def title(n):
return '<span weight="bold" size="larger">%s</span>' % n
@ -517,74 +483,6 @@ def get_participant_from_event(db, event_handle, all_=False):
else:
return participant
#-------------------------------------------------------------------------
#
# Function to return children's list of a person
#
#-------------------------------------------------------------------------
def find_children(db,p):
"""
Return the list of all children's IDs for a person.
"""
childlist = []
for family_handle in p.get_family_handle_list():
family = db.get_family_from_handle(family_handle)
for child_ref in family.get_child_ref_list():
childlist.append(child_ref.ref)
return childlist
#-------------------------------------------------------------------------
#
# Function to return parent's list of a person
#
#-------------------------------------------------------------------------
def find_parents(db,p):
"""
Return the unique list of all parents' IDs for a person.
"""
parentlist = []
for f in p.get_parent_family_handle_list():
family = db.get_family_from_handle(f)
father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle()
if father_handle not in parentlist:
parentlist.append(father_handle)
if mother_handle not in parentlist:
parentlist.append(mother_handle)
return parentlist
#-------------------------------------------------------------------------
#
# Function to return persons, that share the same event.
# This for example links witnesses to the tree
#
#-------------------------------------------------------------------------
def find_witnessed_people(db,p):
people = []
for event_ref in p.get_event_ref_list():
for l in db.find_backlink_handles( event_ref.ref):
if l[0] == 'Person' and l[1] != p.get_handle() and l[1] not in people:
people.append(l[1])
if l[0] == 'Family':
fam = db.get_family_from_handle(l[1])
if fam:
father_handle = fam.get_father_handle()
if father_handle and father_handle != p.get_handle() and father_handle not in people:
people.append(father_handle)
mother_handle = fam.get_mother_handle()
if mother_handle and mother_handle != p.get_handle() and mother_handle not in people:
people.append(mother_handle)
for f in p.get_family_handle_list():
family = db.get_family_from_handle(f)
for event_ref in family.get_event_ref_list():
for l in db.find_backlink_handles( event_ref.ref):
if l[0] == 'Person' and l[1] != p.get_handle() and l[1] not in people:
people.append(l[1])
for pref in p.get_person_ref_list():
if pref.ref != p.get_handle and pref.ref not in people:
people.append(pref.ref)
return people
#-------------------------------------------------------------------------
#
# Function to return a label to display the active object in the status bar

View File

@ -32,7 +32,7 @@ from gen.ggettext import gettext as _
# GRAMPS modules
#
#-------------------------------------------------------------------------
from Utils import for_each_ancestor
from gen.utils.tree import for_each_ancestor
from gen.filters.rules import Rule
#-------------------------------------------------------------------------

View File

@ -32,7 +32,7 @@ from gen.ggettext import gettext as _
# GRAMPS modules
#
#-------------------------------------------------------------------------
from Utils import for_each_ancestor
from gen.utils.tree import for_each_ancestor
from _hascommonancestorwith import HasCommonAncestorWith
from _matchesfilter import MatchesFilter

View File

@ -21,6 +21,7 @@ pkgpython_PYTHON = \
place.py \
referent.py \
trans.py \
tree.py \
unknown.py
pkgpyexecdir = @pkgpyexecdir@/gen/utils

130
src/gen/utils/tree.py Normal file
View File

@ -0,0 +1,130 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2007 Donald N. Allingham
# Copyright (C) 2009 Gary Burton
# Copyright (C) 2011 Tim G L Lyons
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id$
"""
Tree related utility functions
"""
#-------------------------------------------------------------------------
#
# Function to return children's list of a person
#
#-------------------------------------------------------------------------
def find_children(db,p):
"""
Return the list of all children's IDs for a person.
"""
childlist = []
for family_handle in p.get_family_handle_list():
family = db.get_family_from_handle(family_handle)
for child_ref in family.get_child_ref_list():
childlist.append(child_ref.ref)
return childlist
#-------------------------------------------------------------------------
#
# Function to return parent's list of a person
#
#-------------------------------------------------------------------------
def find_parents(db,p):
"""
Return the unique list of all parents' IDs for a person.
"""
parentlist = []
for f in p.get_parent_family_handle_list():
family = db.get_family_from_handle(f)
father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle()
if father_handle not in parentlist:
parentlist.append(father_handle)
if mother_handle not in parentlist:
parentlist.append(mother_handle)
return parentlist
#-------------------------------------------------------------------------
#
# Function to return persons, that share the same event.
# This for example links witnesses to the tree
#
#-------------------------------------------------------------------------
def find_witnessed_people(db,p):
people = []
for event_ref in p.get_event_ref_list():
for l in db.find_backlink_handles( event_ref.ref):
if l[0] == 'Person' and l[1] != p.get_handle() and l[1] not in people:
people.append(l[1])
if l[0] == 'Family':
fam = db.get_family_from_handle(l[1])
if fam:
father_handle = fam.get_father_handle()
if father_handle and father_handle != p.get_handle() and father_handle not in people:
people.append(father_handle)
mother_handle = fam.get_mother_handle()
if mother_handle and mother_handle != p.get_handle() and mother_handle not in people:
people.append(mother_handle)
for f in p.get_family_handle_list():
family = db.get_family_from_handle(f)
for event_ref in family.get_event_ref_list():
for l in db.find_backlink_handles( event_ref.ref):
if l[0] == 'Person' and l[1] != p.get_handle() and l[1] not in people:
people.append(l[1])
for pref in p.get_person_ref_list():
if pref.ref != p.get_handle and pref.ref not in people:
people.append(pref.ref)
return people
#-------------------------------------------------------------------------
#
# Iterate over ancestors.
#
#-------------------------------------------------------------------------
def for_each_ancestor(db, start, func, data):
"""
Recursively iterate (breadth-first) over ancestors of
people listed in start.
Call func(data, pid) for the Id of each person encountered.
Exit and return 1, as soon as func returns true.
Return 0 otherwise.
"""
todo = start
done_ids = set()
while len(todo):
p_handle = todo.pop()
p = db.get_person_from_handle(p_handle)
# Don't process the same handle twice. This can happen
# if there is a cycle in the database, or if the
# initial list contains X and some of X's ancestors.
if p_handle in done_ids:
continue
done_ids.add(p_handle)
if func(data, p_handle):
return 1
for fam_handle in p.get_parent_family_handle_list():
fam = db.get_family_from_handle(fam_handle)
if fam:
f_handle = fam.get_father_handle()
m_handle = fam.get_mother_handle()
if f_handle: todo.append(f_handle)
if m_handle: todo.append(m_handle)
return 0

View File

@ -55,7 +55,7 @@ if gtk.pygtk_version < (2,3,93):
from gen.display.name import displayer as name_displayer
from gen.ggettext import gettext as _
from gen.plug import Gramplet
from Utils import (find_children, find_parents, find_witnessed_people)
from gen.utils.tree import (find_children, find_parents, find_witnessed_people)
from libformatting import FormattingHelper
import gen.lib
from gen.errors import WindowActiveError

View File

@ -54,7 +54,7 @@ if gtk.pygtk_version < (2,3,93):
#
#-------------------------------------------------------------------------
from gen.display.name import displayer as name_displayer
from Utils import (find_children, find_parents, find_witnessed_people)
from gen.utils.tree import (find_children, find_parents, find_witnessed_people)
from libformatting import FormattingHelper
import gen.lib
from gui.views.navigationview import NavigationView

View File

@ -53,7 +53,7 @@ from gui.editors import FilterEditor
from gen.display.name import displayer as name_displayer
from gen.utils.alive import probably_alive
from gen.utils.file import media_path_full
from Utils import find_children, find_parents, find_witnessed_people
from gen.utils.tree import find_children, find_parents, find_witnessed_people
from libformatting import FormattingHelper
from gui.thumbnails import get_thumbnail_path
from gen.errors import WindowActiveError