Skip to content

Commit 8d7b406

Browse files
committed
Some filter rule type related cleanups
1 parent 5adaef9 commit 8d7b406

16 files changed

+460
-311
lines changed

gramps/gen/filters/rules/_hasattributebase.py

+29-20
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,25 @@
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020
#
2121

22-
# -------------------------------------------------------------------------
23-
#
24-
# Standard Python modules
25-
#
26-
# -------------------------------------------------------------------------
27-
from ...const import GRAMPS_LOCALE as glocale
28-
29-
_ = glocale.translation.gettext
22+
"""
23+
Rule that checks for an object with a particular attribute.
24+
"""
3025

3126
# -------------------------------------------------------------------------
3227
#
3328
# Gramps modules
3429
#
3530
# -------------------------------------------------------------------------
31+
from ...const import GRAMPS_LOCALE as glocale
3632
from ...lib.attrtype import AttributeType
3733
from . import Rule
3834

35+
_ = glocale.translation.gettext
36+
3937

4038
# -------------------------------------------------------------------------
4139
#
42-
# HasAttribute
40+
# HasAttributeBase
4341
#
4442
# -------------------------------------------------------------------------
4543
class HasAttributeBase(Rule):
@@ -49,19 +47,30 @@ class HasAttributeBase(Rule):
4947

5048
labels = ["Attribute:", "Value:"]
5149
name = "Objects with the <attribute>"
52-
description = "Matches objects with the given attribute " "of a particular value"
50+
description = "Matches objects with the given attribute of a particular value"
5351
category = _("General filters")
5452
allow_regex = True
5553

56-
def apply(self, db, obj):
57-
if not self.list[0]:
58-
return False
59-
for attr in obj.get_attribute_list():
60-
specified_type = AttributeType()
61-
specified_type.set_from_xml_str(self.list[0])
62-
name_match = attr.get_type() == specified_type
54+
def __init__(self, arg, use_regex=False, use_case=False):
55+
super().__init__(arg, use_regex, use_case)
56+
self.attribute_type = None
57+
58+
def prepare(self, db, user):
59+
"""
60+
Prepare the rule. Things that should only be done once.
61+
"""
62+
if self.list[0]:
63+
self.attribute_type = AttributeType()
64+
self.attribute_type.set_from_xml_str(self.list[0])
6365

64-
if name_match:
65-
if self.match_substring(1, attr.get_value()):
66-
return True
66+
def apply(self, db, obj):
67+
"""
68+
Apply the rule. Return True if a match.
69+
"""
70+
if self.attribute_type:
71+
for attribute in obj.get_attribute_list():
72+
name_match = attribute.get_type() == self.attribute_type
73+
if name_match:
74+
if self.match_substring(1, attribute.get_value()):
75+
return True
6776
return False

gramps/gen/filters/rules/_haseventbase.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,23 @@
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919
#
2020

21-
# -------------------------------------------------------------------------
22-
#
23-
# Standard Python modules
24-
#
25-
# -------------------------------------------------------------------------
26-
from ...const import GRAMPS_LOCALE as glocale
27-
28-
_ = glocale.translation.gettext
21+
"""
22+
Rule that checks for an event with a particular value.
23+
"""
2924

3025
# -------------------------------------------------------------------------
3126
#
3227
# Gramps modules
3328
#
3429
# -------------------------------------------------------------------------
30+
from ...const import GRAMPS_LOCALE as glocale
3531
from ...datehandler import parser
32+
from ...display.place import displayer as place_displayer
3633
from ...lib.eventtype import EventType
37-
from . import Rule
3834
from ...utils.db import get_participant_from_event
39-
from ...display.place import displayer as place_displayer
35+
from . import Rule
36+
37+
_ = glocale.translation.gettext
4038

4139

4240
# -------------------------------------------------------------------------
@@ -45,33 +43,43 @@
4543
#
4644
# -------------------------------------------------------------------------
4745
class HasEventBase(Rule):
48-
"""Rule that checks for an event with a particular value."""
46+
"""
47+
Rule that checks for an event with a particular value.
48+
"""
4949

5050
labels = ["Event type:", "Date:", "Place:", "Description:", "Main Participants:"]
5151
name = "Events matching parameters"
5252
description = "Matches events with particular parameters"
5353
category = _("Event filters")
5454
allow_regex = True
5555

56-
def prepare(self, db, user):
56+
def __init__(self, arg, use_regex=False, use_case=False):
57+
super().__init__(arg, use_regex, use_case)
5758
self.date = None
59+
self.event_type = None
60+
61+
def prepare(self, db, user):
62+
"""
63+
Prepare the rule. Things that should only be done once.
64+
"""
5865
if self.list[0]:
59-
self.etype = EventType()
60-
self.etype.set_from_xml_str(self.list[0])
61-
else:
62-
self.etype = None
66+
self.event_type = EventType()
67+
self.event_type.set_from_xml_str(self.list[0])
6368
try:
6469
if self.list[1]:
6570
self.date = parser.parse(self.list[1])
6671
except:
6772
pass
6873

6974
def apply(self, db, event):
70-
if self.etype:
71-
if self.etype.is_custom() and self.use_regex:
75+
"""
76+
Apply the rule. Return True if a match.
77+
"""
78+
if self.event_type:
79+
if self.event_type.is_custom() and self.use_regex:
7280
if self.regex[0].search(str(event.type)) is None:
7381
return False
74-
elif event.type != self.etype:
82+
elif event.type != self.event_type:
7583
return False
7684

7785
if not self.match_substring(3, event.get_description()):

gramps/gen/filters/rules/event/_hasdata.py

+26-15
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,49 @@
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919
#
2020

21-
# -------------------------------------------------------------------------
22-
#
23-
# Standard Python modules
24-
#
25-
# -------------------------------------------------------------------------
26-
from ....const import GRAMPS_LOCALE as glocale
27-
28-
_ = glocale.translation.gettext
21+
"""
22+
Rules that check for events containing particular values.
23+
"""
2924

3025
# -------------------------------------------------------------------------
3126
#
3227
# Gramps modules
3328
#
3429
# -------------------------------------------------------------------------
30+
from ....const import GRAMPS_LOCALE as glocale
3531
from ....datehandler import parser
3632
from ....display.place import displayer as place_displayer
3733
from ....lib.eventtype import EventType
3834
from .. import Rule
3935

36+
_ = glocale.translation.gettext
37+
4038

4139
# -------------------------------------------------------------------------
4240
#
43-
# HasBirth
41+
# HasData
4442
#
4543
# -------------------------------------------------------------------------
4644
class HasData(Rule):
47-
"""Rule that checks for an event containing particular values"""
45+
"""
46+
Rule that checks for an event containing particular values.
47+
"""
4848

4949
labels = [_("Event type:"), _("Date:"), _("Place:"), _("Description:")]
5050
name = _("Events with <data>")
5151
description = _("Matches events with data of a particular value")
5252
category = _("General filters")
5353
allow_regex = True
5454

55+
def __init__(self, arg, use_regex=False, use_case=False):
56+
super().__init__(arg, use_regex, use_case)
57+
self.event_type = None
58+
self.date = None
59+
5560
def prepare(self, db, user):
61+
"""
62+
Prepare the rule. Things we only want to do once.
63+
"""
5664
self.event_type = self.list[0]
5765
self.date = self.list[1]
5866

@@ -63,17 +71,20 @@ def prepare(self, db, user):
6371
if self.date:
6472
self.date = parser.parse(self.date)
6573

66-
def apply(self, db, event):
67-
if self.event_type and event.get_type() != self.event_type:
74+
def apply(self, db, obj):
75+
"""
76+
Apply the rule. Return True on a match.
77+
"""
78+
if self.event_type and obj.get_type() != self.event_type:
6879
# No match
6980
return False
7081

71-
if self.date and not event.get_date_object().match(self.date):
82+
if self.date and not obj.get_date_object().match(self.date):
7283
# No match
7384
return False
7485

7586
if self.list[2]:
76-
place_id = event.get_place_handle()
87+
place_id = obj.get_place_handle()
7788
if place_id:
7889
place = db.get_place_from_handle(place_id)
7990
place_title = place_displayer.display(db, place)
@@ -84,7 +95,7 @@ def apply(self, db, event):
8495
# No place attached to event
8596
return False
8697

87-
if not self.match_substring(3, event.get_description()):
98+
if not self.match_substring(3, obj.get_description()):
8899
# No match
89100
return False
90101

gramps/gen/filters/rules/event/_hastype.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,53 @@
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919
#
2020

21-
# -------------------------------------------------------------------------
22-
#
23-
# Standard Python modules
24-
#
25-
# -------------------------------------------------------------------------
26-
from ....const import GRAMPS_LOCALE as glocale
27-
28-
_ = glocale.translation.gettext
21+
"""
22+
Rule that checks for an event of a particular type.
23+
"""
2924

3025
# -------------------------------------------------------------------------
3126
#
3227
# Gramps modules
3328
#
3429
# -------------------------------------------------------------------------
30+
from ....const import GRAMPS_LOCALE as glocale
3531
from ....lib.eventtype import EventType
3632
from .. import Rule
3733

34+
_ = glocale.translation.gettext
35+
3836

3937
# -------------------------------------------------------------------------
4038
#
4139
# HasType
4240
#
4341
# -------------------------------------------------------------------------
4442
class HasType(Rule):
45-
"""Rule that checks for an event of a particular type."""
43+
"""
44+
Rule that checks for an event of a particular type.
45+
"""
4646

4747
labels = [_("Event type:")]
4848
name = _("Events with the particular type")
4949
description = _("Matches events with the particular type ")
5050
category = _("General filters")
5151

52-
def apply(self, db, event):
53-
if not self.list[0]:
54-
return False
55-
else:
56-
specified_type = EventType()
57-
specified_type.set_from_xml_str(self.list[0])
58-
return event.get_type() == specified_type
52+
def __init__(self, arg, use_regex=False, use_case=False):
53+
super().__init__(arg, use_regex, use_case)
54+
self.event_type = None
55+
56+
def prepare(self, db, user):
57+
"""
58+
Prepare the rule. Things we only want to do once.
59+
"""
60+
if self.list[0]:
61+
self.event_type = EventType()
62+
self.event_type.set_from_xml_str(self.list[0])
63+
64+
def apply(self, _db, obj):
65+
"""
66+
Apply the rule. Return True if a match.
67+
"""
68+
if self.event_type:
69+
return obj.get_type() == self.event_type
70+
return False

0 commit comments

Comments
 (0)