diff --git a/po/POTFILES.skip b/po/POTFILES.skip index 5094f57a8..5e462466e 100644 --- a/po/POTFILES.skip +++ b/po/POTFILES.skip @@ -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 diff --git a/src/Utils.py b/src/Utils.py index ee11a1090..779d83cd8 100644 --- a/src/Utils.py +++ b/src/Utils.py @@ -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 '%s' % 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 diff --git a/src/gen/filters/rules/person/_hascommonancestorwith.py b/src/gen/filters/rules/person/_hascommonancestorwith.py index ebecd124b..2f07c1680 100644 --- a/src/gen/filters/rules/person/_hascommonancestorwith.py +++ b/src/gen/filters/rules/person/_hascommonancestorwith.py @@ -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 #------------------------------------------------------------------------- diff --git a/src/gen/filters/rules/person/_hascommonancestorwithfiltermatch.py b/src/gen/filters/rules/person/_hascommonancestorwithfiltermatch.py index bb2e9488d..c20cb89dd 100644 --- a/src/gen/filters/rules/person/_hascommonancestorwithfiltermatch.py +++ b/src/gen/filters/rules/person/_hascommonancestorwithfiltermatch.py @@ -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 diff --git a/src/gen/utils/Makefile.am b/src/gen/utils/Makefile.am index b4ceea376..866d036e3 100644 --- a/src/gen/utils/Makefile.am +++ b/src/gen/utils/Makefile.am @@ -21,6 +21,7 @@ pkgpython_PYTHON = \ place.py \ referent.py \ trans.py \ + tree.py \ unknown.py pkgpyexecdir = @pkgpyexecdir@/gen/utils diff --git a/src/gen/utils/tree.py b/src/gen/utils/tree.py new file mode 100644 index 000000000..d674d530a --- /dev/null +++ b/src/gen/utils/tree.py @@ -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 diff --git a/src/plugins/gramplet/fanchartgramplet.py b/src/plugins/gramplet/fanchartgramplet.py index f550d07a6..7eb2cf23b 100644 --- a/src/plugins/gramplet/fanchartgramplet.py +++ b/src/plugins/gramplet/fanchartgramplet.py @@ -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 diff --git a/src/plugins/view/fanchartview.py b/src/plugins/view/fanchartview.py index d2151a5be..c9e41ad21 100644 --- a/src/plugins/view/fanchartview.py +++ b/src/plugins/view/fanchartview.py @@ -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 diff --git a/src/plugins/view/pedigreeview.py b/src/plugins/view/pedigreeview.py index 6b4786814..a86a481b1 100644 --- a/src/plugins/view/pedigreeview.py +++ b/src/plugins/view/pedigreeview.py @@ -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