4196: Error by using special filter in person view

svn: r15855
This commit is contained in:
Benny Malengier 2010-09-03 22:54:40 +00:00
parent 3da3a57960
commit 38e87b074e
12 changed files with 74 additions and 29 deletions

View File

@ -47,7 +47,7 @@ def filter_database(db, progress, filter_name):
filt = MatchesFilter([filter_name]) filt = MatchesFilter([filter_name])
progress.set_header('Preparing sub-filter') progress.set_header('Preparing sub-filter')
filt.prepare(db) filt.requestprepare(db)
progress.set_header('Retrieving all sub-filter matches') progress.set_header('Retrieving all sub-filter matches')
matches = [] matches = []
@ -57,7 +57,7 @@ def filter_database(db, progress, filter_name):
matches.append(handle) matches.append(handle)
progress.step() progress.step()
filt.reset() filt.requestreset()
return matches return matches
@ -133,7 +133,7 @@ class DeepRelationshipPathBetween(Rule):
root_person = db.get_person_from_gramps_id(root_person_id) root_person = db.get_person_from_gramps_id(root_person_id)
progress = ProgressMeter(_('Finding relationship paths')) progress = ProgressMeter(_('Finding relationship paths'))
progress.set_pass(header=_('Evaluating people'), mode=ProgressMeter.MODE_ACTIVITY) progress.set_pass(header=_('Evaluating people'), mode=ProgressMeter.MODE_ACTIVITY)
filter_name = self.list[1] filter_name = self.list[1]
target_people = filter_database(db, progress, filter_name) target_people = filter_database(db, progress, filter_name)

View File

@ -41,7 +41,7 @@ from _MatchesFilter import MatchesFilter
# HasCommonAncestorWithFilterMatch # HasCommonAncestorWithFilterMatch
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class HasCommonAncestorWithFilterMatch(HasCommonAncestorWith,MatchesFilter): class HasCommonAncestorWithFilterMatch(HasCommonAncestorWith):
"""Rule that checks for a person that has a common ancestor with """Rule that checks for a person that has a common ancestor with
someone matching a filter""" someone matching a filter"""
@ -64,7 +64,7 @@ class HasCommonAncestorWithFilterMatch(HasCommonAncestorWith,MatchesFilter):
self.ancestor_cache = {} self.ancestor_cache = {}
self.with_people = [] self.with_people = []
filt = MatchesFilter(self.list) filt = MatchesFilter(self.list)
filt.prepare(db) filt.requestprepare(db)
for handle in db.iter_person_handles(): for handle in db.iter_person_handles():
person = db.get_person_from_handle(handle) person = db.get_person_from_handle(handle)
if person and filt.apply(db, person): if person and filt.apply(db, person):
@ -73,4 +73,4 @@ class HasCommonAncestorWithFilterMatch(HasCommonAncestorWith,MatchesFilter):
#fill list of ancestor of person if not present yet #fill list of ancestor of person if not present yet
if handle not in self.ancestor_cache: if handle not in self.ancestor_cache:
self.add_ancs(db, person) self.add_ancs(db, person)
filt.reset() filt.requestreset()

View File

@ -40,7 +40,7 @@ from _MatchesFilter import MatchesFilter
# IsAncestorOfFilterMatch # IsAncestorOfFilterMatch
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class IsAncestorOfFilterMatch(IsAncestorOf,MatchesFilter): class IsAncestorOfFilterMatch(IsAncestorOf):
"""Rule that checks for a person that is an ancestor of """Rule that checks for a person that is an ancestor of
someone matched by a filter""" someone matched by a filter"""
@ -66,11 +66,11 @@ class IsAncestorOfFilterMatch(IsAncestorOf,MatchesFilter):
first = 1 first = 1
filt = MatchesFilter(self.list[0:1]) filt = MatchesFilter(self.list[0:1])
filt.prepare(db) filt.requestprepare(db)
for person in db.iter_people(): for person in db.iter_people():
if filt.apply(db, person): if filt.apply(db, person):
self.init_ancestor_list(db, person, first) self.init_ancestor_list(db, person, first)
filt.reset() filt.requestreset()
def reset(self): def reset(self):
self.map.clear() self.map.clear()

View File

@ -32,6 +32,7 @@ from gen.ggettext import gettext as _
# GRAMPS modules # GRAMPS modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from Filters.Rules import Rule
from _MatchesFilter import MatchesFilter from _MatchesFilter import MatchesFilter
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -39,7 +40,7 @@ from _MatchesFilter import MatchesFilter
# IsChildOfFilterMatch # IsChildOfFilterMatch
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class IsChildOfFilterMatch(MatchesFilter): class IsChildOfFilterMatch(Rule):
"""Rule that checks for a person that is a child """Rule that checks for a person that is a child
of someone matched by a filter""" of someone matched by a filter"""
@ -52,11 +53,11 @@ class IsChildOfFilterMatch(MatchesFilter):
self.db = db self.db = db
self.map = set() self.map = set()
filt = MatchesFilter(self.list) filt = MatchesFilter(self.list)
filt.prepare(db) filt.requestprepare(db)
for person in db.iter_people(): for person in db.iter_people():
if filt.apply(db, person): if filt.apply(db, person):
self.init_list(person) self.init_list(person)
filt.reset() filt.requestreset()
def reset(self): def reset(self):
self.map.clear() self.map.clear()

View File

@ -40,7 +40,7 @@ from _MatchesFilter import MatchesFilter
# IsDescendantOfFilterMatch # IsDescendantOfFilterMatch
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class IsDescendantOfFilterMatch(IsDescendantOf,MatchesFilter): class IsDescendantOfFilterMatch(IsDescendantOf):
"""Rule that checks for a person that is a descendant """Rule that checks for a person that is a descendant
of someone matched by a filter""" of someone matched by a filter"""
@ -66,11 +66,11 @@ class IsDescendantOfFilterMatch(IsDescendantOf,MatchesFilter):
first = 1 first = 1
filt = MatchesFilter(self.list[0:1]) filt = MatchesFilter(self.list[0:1])
filt.prepare(db) filt.requestprepare(db)
for person in db.iter_people(): for person in db.iter_people():
if filt.apply(db, person): if filt.apply(db, person):
self.init_list(person, first) self.init_list(person, first)
filt.reset() filt.requestreset()
def reset(self): def reset(self):
self.map.clear() self.map.clear()

View File

@ -32,6 +32,7 @@ from gen.ggettext import gettext as _
# GRAMPS modules # GRAMPS modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from Filters.Rules import Rule
from _MatchesFilter import MatchesFilter from _MatchesFilter import MatchesFilter
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -39,7 +40,7 @@ from _MatchesFilter import MatchesFilter
# IsParentOfFilterMatch # IsParentOfFilterMatch
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class IsParentOfFilterMatch(MatchesFilter): class IsParentOfFilterMatch(Rule):
"""Rule that checks for a person that is a parent """Rule that checks for a person that is a parent
of someone matched by a filter""" of someone matched by a filter"""
@ -52,11 +53,11 @@ class IsParentOfFilterMatch(MatchesFilter):
self.db = db self.db = db
self.map = set() self.map = set()
filt = MatchesFilter(self.list) filt = MatchesFilter(self.list)
filt.prepare(db) filt.requestprepare(db)
for person in db.iter_people(): for person in db.iter_people():
if filt.apply(db, person): if filt.apply(db, person):
self.init_list(person) self.init_list(person)
filt.reset() filt.requestreset()
def reset(self): def reset(self):
self.map.clear() self.map.clear()

View File

@ -32,6 +32,7 @@ from gen.ggettext import gettext as _
# GRAMPS modules # GRAMPS modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from Filters.Rules import Rule
from _MatchesFilter import MatchesFilter from _MatchesFilter import MatchesFilter
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -39,7 +40,7 @@ from _MatchesFilter import MatchesFilter
# IsSiblingOfFilterMatch # IsSiblingOfFilterMatch
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class IsSiblingOfFilterMatch(MatchesFilter): class IsSiblingOfFilterMatch(Rule):
"""Rule that checks for siblings of someone matched by a filter""" """Rule that checks for siblings of someone matched by a filter"""
labels = [ _('Filter name:') ] labels = [ _('Filter name:') ]
@ -51,11 +52,11 @@ class IsSiblingOfFilterMatch(MatchesFilter):
self.db = db self.db = db
self.map = set() self.map = set()
filt = MatchesFilter(self.list) filt = MatchesFilter(self.list)
filt.prepare(db) filt.requestprepare(db)
for person in db.iter_people(): for person in db.iter_people():
if filt.apply (db, person): if filt.apply (db, person):
self.init_list (person) self.init_list (person)
filt.reset() filt.requestreset()
def reset(self): def reset(self):
self.map.clear() self.map.clear()

View File

@ -32,6 +32,7 @@ from gen.ggettext import gettext as _
# GRAMPS modules # GRAMPS modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from Filters.Rules import Rule
from _MatchesFilter import MatchesFilter from _MatchesFilter import MatchesFilter
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -39,7 +40,7 @@ from _MatchesFilter import MatchesFilter
# IsSpouseOfFilterMatch # IsSpouseOfFilterMatch
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class IsSpouseOfFilterMatch(MatchesFilter): class IsSpouseOfFilterMatch(Rule):
"""Rule that checks for a person married to someone matching """Rule that checks for a person married to someone matching
a filter""" a filter"""
@ -50,7 +51,7 @@ class IsSpouseOfFilterMatch(MatchesFilter):
def prepare(self,db): def prepare(self,db):
self.filt = MatchesFilter (self.list) self.filt = MatchesFilter (self.list)
self.filt.prepare(db) self.filt.requestprepare(db)
def apply(self,db,person): def apply(self,db,person):
for family_handle in person.get_family_handle_list (): for family_handle in person.get_family_handle_list ():
@ -65,3 +66,6 @@ class IsSpouseOfFilterMatch(MatchesFilter):
if self.filt.apply (db, db.get_person_from_handle( spouse_id)): if self.filt.apply (db, db.get_person_from_handle( spouse_id)):
return True return True
return False return False
def reset(self):
self.filt.requestreset()

View File

@ -60,7 +60,8 @@ class MatchesFilterBase(Rule):
if self.list[0] in filters: if self.list[0] in filters:
filt = filters[self.list[0]] filt = filters[self.list[0]]
for rule in filt.flist: for rule in filt.flist:
rule.prepare(db) rule.requestprepare(db)
def reset(self): def reset(self):
if Filters.CustomFilters: if Filters.CustomFilters:
@ -68,7 +69,7 @@ class MatchesFilterBase(Rule):
if self.list[0] in filters: if self.list[0] in filters:
filt = filters[self.list[0]] filt = filters[self.list[0]]
for rule in filt.flist: for rule in filt.flist:
rule.reset() rule.requestreset()
def apply(self, db, obj): def apply(self, db, obj):
if Filters.CustomFilters: if Filters.CustomFilters:

View File

@ -43,14 +43,47 @@ class Rule(object):
def __init__(self, arg): def __init__(self, arg):
self.set_list(arg) self.set_list(arg)
self.nrprepare = 0
def is_empty(self): def is_empty(self):
return False return False
def requestprepare(self, db):
"""
Request that the prepare method of the rule is executed if needed
Special: Custom Filters have fixed values, so only one instance needs to
exists during a search. It is stored in a FilterStore, and initialized
once.
As filters are can be grouped in a group
filter, we request a prepare. Only the first time prepare will be
called
"""
if self.nrprepare == 0:
self.prepare(db)
self.nrprepare += 1
def prepare(self, db): def prepare(self, db):
"""prepare so the rule can be executed efficiently"""
pass pass
def requestreset(self):
"""
Request that the reset method of the rule is executed if possible
Special: Custom Filters have fixed values, so only one instance needs to
exists during a search. It is stored in a FilterStore, and initialized
once.
As filters are can be grouped in a group
filter, we request a reset. Only the last time reset will be
called
"""
self.nrprepare -= 1
if self.nrprepare == 0:
self.reset()
def reset(self): def reset(self):
"""remove no longer needed memory"""
pass pass
def set_list(self, arg): def set_list(self, arg):

View File

@ -227,10 +227,10 @@ class GenericFilter(object):
""" """
m = self.get_check_func() m = self.get_check_func()
for rule in self.flist: for rule in self.flist:
rule.prepare(db) rule.requestprepare(db)
res = m(db, id_list, progress, tupleind) res = m(db, id_list, progress, tupleind)
for rule in self.flist: for rule in self.flist:
rule.reset() rule.requestreset()
return res return res
class GenericFamilyFilter(GenericFilter): class GenericFamilyFilter(GenericFilter):

View File

@ -30,6 +30,7 @@ Package providing filtering framework for GRAMPS.
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from Filters._GenericFilter import GenericFilter from Filters._GenericFilter import GenericFilter
from Errors import FilterError
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -58,8 +59,11 @@ class ParamFilter(GenericFilter):
new_list[ix] = self.param_list[ix] new_list[ix] = self.param_list[ix]
rule.set_list(new_list) rule.set_list(new_list)
for rule in self.flist: for rule in self.flist:
rule.prepare(db) if rule.nrprepare > 0:
raise FilterError, 'Custom filters can not twice be used' \
' in a parameter filter'
rule.requestprepare(db)
result = GenericFilter.apply(self, db, id_list) result = GenericFilter.apply(self, db, id_list)
for rule in self.flist: for rule in self.flist:
rule.reset() rule.requestreset()
return result return result