Generic filtering improvements

svn: r894
This commit is contained in:
Don Allingham 2002-04-05 04:12:10 +00:00
parent 27ca14bbb1
commit 258397ff8c
3 changed files with 104 additions and 25 deletions

View File

@ -47,7 +47,24 @@ from string import find,join
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from RelLib import * from RelLib import *
from Date import Date import Date
#-------------------------------------------------------------------------
#
# date_cmp
#
#-------------------------------------------------------------------------
def date_cmp(rule,value):
sd = rule.get_start_date()
s = sd.mode
if s == Date.SingleDate.before:
return Date.compare_dates(rule,value) == 1
elif s == Date.SingleDate.after:
return Date.compare_dates(rule,value) == -1
elif sd.month == Date.UNDEF and sd.year != Date.UNDEF:
return sd.year == value.get_start_date().year
else:
return Date.compare_dates(rule,value) == 0
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -79,7 +96,7 @@ class Rule:
v = [] v = []
for i in range(0,len(self.list)): for i in range(0,len(self.list)):
if self.list[i]: if self.list[i]:
v.append('%s="%s"' % (self.labels[i],self.list[i])) v.append('%s="%s"' % (_(self.labels[i]),self.list[i]))
return join(v,'; ') return join(v,'; ')
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -136,7 +153,8 @@ class IsFemale(Rule):
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class IsDescendantOf(Rule): class IsDescendantOf(Rule):
"""Rule that checks for a person that is a descendant of a specified person""" """Rule that checks for a person that is a descendant
of a specified person"""
labels = ['ID'] labels = ['ID']
@ -149,7 +167,6 @@ class IsDescendantOf(Rule):
def search(self,p): def search(self,p):
if p.getId() == self.list[0]: if p.getId() == self.list[0]:
return 1 return 1
for (f,r1,r2) in p.getParentList(): for (f,r1,r2) in p.getParentList():
for p1 in [f.getMother(),f.getFather()]: for p1 in [f.getMother(),f.getFather()]:
if p1: if p1:
@ -210,26 +227,91 @@ class HasEvent(Rule):
labels = [ 'Event', 'Date', 'Place', 'Description' ] labels = [ 'Event', 'Date', 'Place', 'Description' ]
def __init__(self,list):
Rule.__init__(self,list)
if self.list[0]:
self.date = Date.Date()
self.date.set(self.list[0])
else:
self.date = None
def name(self): def name(self):
return 'Has the event' return 'Has the event'
def apply(self,p): def apply(self,p):
for event in [p.getBirth(),p.getDeath()] + p.getEventList(): for event in p.getEventList():
if self.list[0] and event.getName() != self.list[0]: if self.list[0] and event.getName() != self.list[0]:
return 0 return 0
if self.list[3] and find(event.getDescription(),self.list[3])==-1: if self.list[3] and find(event.getDescription(),self.list[3])==-1:
return 0 return 0
if self.list[1]: if self.date:
try: return date_cmp(self.date,event.getDateObj())
d = Date.Date(self.list[1])
except:
return 0
if Date.compare_dates(d,event.getDateObj()):
return 0
if self.list[2] and find(p.getPlaceName(),self.list[2]) == -1: if self.list[2] and find(p.getPlaceName(),self.list[2]) == -1:
return 0 return 0
return 1 return 1
#-------------------------------------------------------------------------
#
# HasBirth
#
#-------------------------------------------------------------------------
class HasBirth(Rule):
"""Rule that checks for a person with a birth of a particular value"""
labels = [ 'Date', 'Place', 'Description' ]
def __init__(self,list):
Rule.__init__(self,list)
if self.list[0]:
self.date = Date.Date()
self.date.set(self.list[0])
else:
self.date = None
def name(self):
return 'Has the birth'
def apply(self,p):
event = p.getBirth()
if self.list[2] and find(event.getDescription(),self.list[2])==-1:
return 0
if self.date:
return date_cmp(self.date,event.getDateObj())
if self.list[1] and find(p.getPlaceName(),self.list[1]) == -1:
return 0
return 1
#-------------------------------------------------------------------------
#
# HasDeath
#
#-------------------------------------------------------------------------
class HasDeath(Rule):
"""Rule that checks for a person with a death of a particular value"""
labels = [ 'Date', 'Place', 'Description' ]
def __init__(self,list):
Rule.__init__(self,list)
if self.list[0]:
self.date = Date.Date()
self.date.set(self.list[0])
else:
self.date = None
def name(self):
return 'Has the death'
def apply(self,p):
event = p.getDeath()
if self.list[2] and find(event.getDescription(),self.list[2])==-1:
return 0
if self.date:
date_cmp(self.date,event.getDateObj())
if self.list[1] and find(p.getPlaceName(),self.list[1]) == -1:
return 0
return 1
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# HasAttribute # HasAttribute
@ -338,6 +420,8 @@ tasks = {
"Everyone" : Everyone, "Everyone" : Everyone,
"Has the Id" : HasIdOf, "Has the Id" : HasIdOf,
"Has a name" : HasNameOf, "Has a name" : HasNameOf,
"Has the death" : HasDeath,
"Has the birth" : HasBirth,
"Is the descendant of" : IsDescendantOf, "Is the descendant of" : IsDescendantOf,
"Is an ancestor of" : IsAncestorOf, "Is an ancestor of" : IsAncestorOf,
"Is a female" : IsFemale, "Is a female" : IsFemale,
@ -374,7 +458,11 @@ class GenericFilterList:
pass pass
def save(self): def save(self):
try:
f = open(self.file,'w') f = open(self.file,'w')
except:
return
f.write("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n") f.write("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n")
f.write('<filters>\n') f.write('<filters>\n')
for i in self.filter_list: for i in self.filter_list:
@ -433,15 +521,4 @@ class FilterParser(handler.ContentHandler):
def characters(self, data): def characters(self, data):
pass pass
if __name__ == '__main__':
g = GenericFilter()
g.set_name("Everyone")
rule = Everyone([])
g.add_rule(rule)
l = GenericFilterList('/home/dona/.gramps/custom_filters.xml')
l.load()
l.add(g)
l.save()

View File

@ -504,6 +504,8 @@
<widget> <widget>
<class>GtkFrame</class> <class>GtkFrame</class>
<name>values</name> <name>values</name>
<width>300</width>
<height>200</height>
<label>Values</label> <label>Values</label>
<label_xalign>0</label_xalign> <label_xalign>0</label_xalign>
<shadow_type>GTK_SHADOW_ETCHED_IN</shadow_type> <shadow_type>GTK_SHADOW_ETCHED_IN</shadow_type>