1#
2# Gramps - a GTK+/GNOME based genealogy program
3#
4# Copyright (C) 2002-2006  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20
21#-------------------------------------------------------------------------
22#
23# Standard Python modules
24#
25#-------------------------------------------------------------------------
26from ....const import GRAMPS_LOCALE as glocale
27_ = glocale.translation.gettext
28
29#-------------------------------------------------------------------------
30#
31# Gramps modules
32#
33#-------------------------------------------------------------------------
34from .. import Rule
35
36#-------------------------------------------------------------------------
37#
38# IsMoreThanNthGenerationAncestorOf
39#
40#-------------------------------------------------------------------------
41class IsMoreThanNthGenerationAncestorOf(Rule):
42    """Rule that checks for a person that is an ancestor of a specified person
43    at least N generations away"""
44
45    labels = [ _('ID:'), _('Number of generations:') ]
46    name = _('Ancestors of <person> at least <N> generations away')
47    category = _("Ancestral filters")
48    description = _("Matches people that are ancestors "
49                    "of a specified person at least N generations away")
50
51    def prepare(self, db, user):
52        self.db = db
53        self.map = set()
54        person = db.get_person_from_gramps_id(self.list[0])
55        if person:
56            root_handle = person.get_handle()
57            if root_handle:
58                self.init_ancestor_list(root_handle)
59
60    def init_ancestor_list(self, root_handle):
61        queue = [(root_handle, 1)] # generation 1 is root
62        while queue:
63            handle, gen = queue.pop(0) # pop off front of queue
64            if gen > int(self.list[1]):
65                self.map.add(handle)
66            gen += 1
67            p = self.db.get_person_from_handle(handle)
68            fam_id = p.get_main_parents_family_handle()
69            if fam_id:
70                fam = self.db.get_family_from_handle(fam_id)
71                if fam:
72                    f_id = fam.get_father_handle()
73                    m_id = fam.get_mother_handle()
74                    # append to back of queue:
75                    if f_id:
76                        queue.append((f_id, gen))
77                    if m_id:
78                        queue.append((m_id, gen))
79
80    def reset(self):
81        self.map.clear()
82
83    def apply(self,db,person):
84        return person.handle in self.map
85