Skip to content

Commit

Permalink
Some filter rule type related cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhorn committed Apr 24, 2024
1 parent 0e626b6 commit a0c5583
Show file tree
Hide file tree
Showing 16 changed files with 460 additions and 311 deletions.
49 changes: 29 additions & 20 deletions gramps/gen/filters/rules/_hasattributebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,25 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

# -------------------------------------------------------------------------
#
# Standard Python modules
#
# -------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale

_ = glocale.translation.gettext
"""
Rule that checks for an object with a particular attribute.
"""

# -------------------------------------------------------------------------
#
# Gramps modules
#
# -------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale
from ...lib.attrtype import AttributeType
from . import Rule

_ = glocale.translation.gettext


# -------------------------------------------------------------------------
#
# HasAttribute
# HasAttributeBase
#
# -------------------------------------------------------------------------
class HasAttributeBase(Rule):
Expand All @@ -49,19 +47,30 @@ class HasAttributeBase(Rule):

labels = ["Attribute:", "Value:"]
name = "Objects with the <attribute>"
description = "Matches objects with the given attribute " "of a particular value"
description = "Matches objects with the given attribute of a particular value"
category = _("General filters")
allow_regex = True

def apply(self, db, obj):
if not self.list[0]:
return False
for attr in obj.get_attribute_list():
specified_type = AttributeType()
specified_type.set_from_xml_str(self.list[0])
name_match = attr.get_type() == specified_type
def __init__(self, arg, use_regex=False, use_case=False):
super().__init__(arg, use_regex, use_case)
self.attribute_type = None

def prepare(self, db, user):
"""
Prepare the rule. Things that should only be done once.
"""
if self.list[0]:
self.attribute_type = AttributeType()
self.attribute_type.set_from_xml_str(self.list[0])

if name_match:
if self.match_substring(1, attr.get_value()):
return True
def apply(self, db, obj):
"""
Apply the rule. Return True if a match.
"""
if self.attribute_type:
for attribute in obj.get_attribute_list():
name_match = attribute.get_type() == self.attribute_type
if name_match:
if self.match_substring(1, attribute.get_value()):
return True
return False
46 changes: 27 additions & 19 deletions gramps/gen/filters/rules/_haseventbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,23 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

# -------------------------------------------------------------------------
#
# Standard Python modules
#
# -------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale

_ = glocale.translation.gettext
"""
Rule that checks for an event with a particular value.
"""

# -------------------------------------------------------------------------
#
# Gramps modules
#
# -------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale
from ...datehandler import parser
from ...display.place import displayer as place_displayer
from ...lib.eventtype import EventType
from . import Rule
from ...utils.db import get_participant_from_event
from ...display.place import displayer as place_displayer
from . import Rule

_ = glocale.translation.gettext


# -------------------------------------------------------------------------
Expand All @@ -45,33 +43,43 @@
#
# -------------------------------------------------------------------------
class HasEventBase(Rule):
"""Rule that checks for an event with a particular value."""
"""
Rule that checks for an event with a particular value.
"""

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

def prepare(self, db, user):
def __init__(self, arg, use_regex=False, use_case=False):
super().__init__(arg, use_regex, use_case)
self.date = None
self.event_type = None

def prepare(self, db, user):
"""
Prepare the rule. Things that should only be done once.
"""
if self.list[0]:
self.etype = EventType()
self.etype.set_from_xml_str(self.list[0])
else:
self.etype = None
self.event_type = EventType()
self.event_type.set_from_xml_str(self.list[0])
try:
if self.list[1]:
self.date = parser.parse(self.list[1])
except:
pass

def apply(self, db, event):
if self.etype:
if self.etype.is_custom() and self.use_regex:
"""
Apply the rule. Return True if a match.
"""
if self.event_type:
if self.event_type.is_custom() and self.use_regex:
if self.regex[0].search(str(event.type)) is None:
return False
elif event.type != self.etype:
elif event.type != self.event_type:
return False

if not self.match_substring(3, event.get_description()):
Expand Down
41 changes: 26 additions & 15 deletions gramps/gen/filters/rules/event/_hasdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,49 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

# -------------------------------------------------------------------------
#
# Standard Python modules
#
# -------------------------------------------------------------------------
from ....const import GRAMPS_LOCALE as glocale

_ = glocale.translation.gettext
"""
Rules that check for events containing particular values.
"""

# -------------------------------------------------------------------------
#
# Gramps modules
#
# -------------------------------------------------------------------------
from ....const import GRAMPS_LOCALE as glocale
from ....datehandler import parser
from ....display.place import displayer as place_displayer
from ....lib.eventtype import EventType
from .. import Rule

_ = glocale.translation.gettext


# -------------------------------------------------------------------------
#
# HasBirth
# HasData
#
# -------------------------------------------------------------------------
class HasData(Rule):
"""Rule that checks for an event containing particular values"""
"""
Rule that checks for an event containing particular values.
"""

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

def __init__(self, arg, use_regex=False, use_case=False):
super().__init__(arg, use_regex, use_case)
self.event_type = None
self.date = None

def prepare(self, db, user):
"""
Prepare the rule. Things we only want to do once.
"""
self.event_type = self.list[0]
self.date = self.list[1]

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

def apply(self, db, event):
if self.event_type and event.get_type() != self.event_type:
def apply(self, db, obj):
"""
Apply the rule. Return True on a match.
"""
if self.event_type and obj.get_type() != self.event_type:
# No match
return False

if self.date and not event.get_date_object().match(self.date):
if self.date and not obj.get_date_object().match(self.date):
# No match
return False

if self.list[2]:
place_id = event.get_place_handle()
place_id = obj.get_place_handle()
if place_id:
place = db.get_place_from_handle(place_id)
place_title = place_displayer.display(db, place)
Expand All @@ -84,7 +95,7 @@ def apply(self, db, event):
# No place attached to event
return False

if not self.match_substring(3, event.get_description()):
if not self.match_substring(3, obj.get_description()):
# No match
return False

Expand Down
44 changes: 28 additions & 16 deletions gramps/gen/filters/rules/event/_hastype.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,53 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

# -------------------------------------------------------------------------
#
# Standard Python modules
#
# -------------------------------------------------------------------------
from ....const import GRAMPS_LOCALE as glocale

_ = glocale.translation.gettext
"""
Rule that checks for an event of a particular type.
"""

# -------------------------------------------------------------------------
#
# Gramps modules
#
# -------------------------------------------------------------------------
from ....const import GRAMPS_LOCALE as glocale
from ....lib.eventtype import EventType
from .. import Rule

_ = glocale.translation.gettext


# -------------------------------------------------------------------------
#
# HasType
#
# -------------------------------------------------------------------------
class HasType(Rule):
"""Rule that checks for an event of a particular type."""
"""
Rule that checks for an event of a particular type.
"""

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

def apply(self, db, event):
if not self.list[0]:
return False
else:
specified_type = EventType()
specified_type.set_from_xml_str(self.list[0])
return event.get_type() == specified_type
def __init__(self, arg, use_regex=False, use_case=False):
super().__init__(arg, use_regex, use_case)
self.event_type = None

def prepare(self, db, user):
"""
Prepare the rule. Things we only want to do once.
"""
if self.list[0]:
self.event_type = EventType()
self.event_type.set_from_xml_str(self.list[0])

def apply(self, _db, obj):
"""
Apply the rule. Return True if a match.
"""
if self.event_type:
return obj.get_type() == self.event_type
return False
Loading

0 comments on commit a0c5583

Please sign in to comment.