Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor, fix, and optimizer filters/rules #1794

Open
wants to merge 4 commits into
base: dsb/depickle
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 239 additions & 75 deletions gramps/gen/filters/_genericfilter.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_changedsincebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def prepare(self, db, user):
if self.list[1]:
self.before = self.time_str_to_sec(self.list[1])

def apply(self, db, obj):
obj_time = obj.get_change_time()
def apply_to_one(self, db, data):
obj_time = data["change"]
if self.since:
if obj_time < self.since:
return False
Expand Down
2 changes: 1 addition & 1 deletion gramps/gen/filters/rules/_everything.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ class Everything(Rule):
def is_empty(self):
return True

def apply(self, db, obj):
def apply_to_one(self, db, data):
return True
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_hasattributebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def prepare(self, db, user):
self.attribute_type = AttributeType()
self.attribute_type.set_from_xml_str(self.list[0])

def apply(self, db, obj):
def apply_to_one(self, db, data):
"""
Apply the rule. Return True if a match.
"""
obj = self.get_object(data)
if self.attribute_type:
for attribute in obj.get_attribute_list():
name_match = attribute.get_type() == self.attribute_type
Expand Down
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_hascitationbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def prepare(self, db, user):
except:
pass

def apply(self, dbase, object):
def apply_to_one(self, dbase, data):
object = self.get_object(data)
for citation_handle in object.get_citation_list():
citation = dbase.get_citation_from_handle(citation_handle)
if self._apply(dbase, citation):
Expand Down
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_haseventbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def prepare(self, db, user):
except:
pass

def apply(self, db, event):
def apply_to_one(self, db, data):
"""
Apply the rule. Return True if a match.
"""
event = self.get_object(data)
if self.event_type:
if self.event_type.is_custom() and self.use_regex:
if self.regex[0].search(str(event.type)) is None:
Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_hasgallerybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def prepare(self, db, user):

self.userSelectedCount = int(self.list[0])

def apply(self, db, obj):
count = len(obj.get_media_list())
def apply_to_one(self, db, data):
count = len(data["media_list"])
if self.count_type == 0: # "less than"
return count < self.userSelectedCount
elif self.count_type == 2: # "greater than"
Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_hasgrampsid.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class HasGrampsId(Rule):
description = "Matches objects with a specified Gramps ID"
category = _("General filters")

def apply(self, db, obj):
def apply_to_one(self, db, data):
"""
apply the rule on the obj.
return true if the rule passes, false otherwise.
"""
return obj.gramps_id == self.list[0]
return data["gramps_id"] == self.list[0]
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_hasldsbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def prepare(self, db, user):

self.userSelectedCount = int(self.list[0])

def apply(self, db, obj):
def apply_to_one(self, db, data):
obj = self.get_object(data)
count = len(obj.get_lds_ord_list())
if self.count_type == 0: # "less than"
return count < self.userSelectedCount
Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_hasnotebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def prepare(self, db, user):

self.userSelectedCount = int(self.list[0])

def apply(self, db, obj):
count = len(obj.get_note_list())
def apply_to_one(self, db, data):
count = len(data["note_list"])
if self.count_type == 0: # "less than"
return count < self.userSelectedCount
elif self.count_type == 2: # "greater than"
Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_hasnoteregexbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class HasNoteRegexBase(Rule):
category = _("General filters")
allow_regex = True

def apply(self, db, person):
for handle in person.get_note_list():
def apply_to_one(self, db, data):
for handle in data["note_list"]:
note = db.get_note_from_handle(handle)
if self.match_substring(0, note.get()):
return True
Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_hasnotesubstrbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class HasNoteSubstrBase(Rule):
description = "Matches objects whose notes contain text matching a " "substring"
category = _("General filters")

def apply(self, db, person):
notelist = person.get_note_list()
def apply_to_one(self, db, data):
notelist = data["note_list"]
for notehandle in notelist:
note = db.get_note_from_handle(notehandle)
n = note.get()
Expand Down
8 changes: 3 additions & 5 deletions gramps/gen/filters/rules/_hasreferencecountbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ def prepare(self, db, user):

self.userSelectedCount = int(self.list[1])

def apply(self, db, obj):
handle = obj.get_handle()
count = 0
for item in db.find_backlink_handles(handle):
count += 1
def apply_to_one(self, db, data):
handle = data["handle"]
count = len(list(db.find_backlink_handles(handle)))

if self.count_type == 0: # "less than"
return count < self.userSelectedCount
Expand Down
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_hassourcebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class HasSourceBase(Rule):
category = _("Citation/source filters")
allow_regex = True

def apply(self, db, source):
def apply_to_one(self, db, data):
source = self.get_object(data)
if not self.match_substring(0, source.get_title()):
return False

Expand Down
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_hassourcecountbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def prepare(self, db, user):

self.userSelectedCount = int(self.list[0])

def apply(self, db, obj):
def apply_to_one(self, db, data):
obj = self.get_object(data)
count = len(obj.get_citation_list())
if self.count_type == 0: # "less than"
return count < self.userSelectedCount
Expand Down
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_hassourceofbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def prepare(self, db, user):
except:
self.source_handle = None

def apply(self, db, object):
def apply_to_one(self, db, data):
object = self.get_object(data)
if not self.source_handle:
if self.nosource:
# check whether the citation list is empty as a proxy for
Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_hastagbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def prepare(self, db, user):
if tag is not None:
self.tag_handle = tag.get_handle()

def apply(self, db, obj):
def apply_to_one(self, db, data):
"""
Apply the rule. Return True for a match.
"""
if self.tag_handle is None:
return False
return self.tag_handle in obj.get_tag_list()
return self.tag_handle in data["tag_list"]
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_isprivate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ class IsPrivate(Rule):
description = "Matches objects that are indicated as private"
category = _("General filters")

def apply(self, db, obj):
return obj.get_privacy()
def apply_to_one(self, db, data):
return data["private"]
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_ispublic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ class IsPublic(Rule):
description = "Matches objects that are not indicated as private"
category = _("General filters")

def apply(self, db, obj):
return not obj.get_privacy()
def apply_to_one(self, db, data):
return not data["private"]
5 changes: 3 additions & 2 deletions gramps/gen/filters/rules/_matcheseventfilterbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ def prepare(self, db, user):
MatchesFilterBase.prepare(self, db, user)
self.MEF_filt = self.find_filter()

def apply(self, db, object):
def apply_to_one(self, db, data):
object = self.get_object(data)
if self.MEF_filt is None:
return False

eventlist = [x.ref for x in object.get_event_ref_list()]
for eventhandle in eventlist:
# check if event in event filter
if self.MEF_filt.check(db, eventhandle):
if self.MEF_filt.apply_to_one(db, eventhandle):
return True
return False
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_matchesfilterbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def reset(self):
for rule in filt.flist:
rule.requestreset()

def apply(self, db, obj):
def apply_to_one(self, db, data):
if gramps.gen.filters.CustomFilters:
filters = gramps.gen.filters.CustomFilters.get_filters_dict(self.namespace)
if self.list[0] in filters:
filt = filters[self.list[0]]
return filt.check(db, obj.handle)
return filt.apply_to_one(db, data)
return False

def find_filter(self):
Expand Down
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/_matchessourceconfidencebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class MatchesSourceConfidenceBase(Rule):
)
category = _("Citation/source filters")

def apply(self, db, obj):
def apply_to_one(self, db, data):
obj = self.get_object(data)
required_conf = int(self.list[0])
for citation_handle in obj.get_citation_list():
citation = db.get_citation_from_handle(citation_handle)
Expand Down
6 changes: 4 additions & 2 deletions gramps/gen/filters/rules/_matchessourcefilterbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ def prepare(self, db, user):
MatchesFilterBase.prepare(self, db, user)
self.MSF_filt = self.find_filter()

def apply(self, db, object):
def apply_to_one(self, db, data):
if self.MSF_filt is None:
return False

object = self.get_object(data)
for citation_handle in object.get_citation_list():
citation = db.get_citation_from_handle(citation_handle)
sourcehandle = citation.get_reference_handle()
if self.MSF_filt.check(db, sourcehandle):
source_data = db.get_raw_source_data(sourcehandle)
if self.MSF_filt.apply_to_one(db, source_data):
return True
return False
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/_regexpidbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ class RegExpIdBase(Rule):
category = _("General filters")
allow_regex = True

def apply(self, db, obj):
return self.match_substring(0, obj.gramps_id)
def apply_to_one(self, db, data):
return self.match_substring(0, data["gramps_id"])
11 changes: 10 additions & 1 deletion gramps/gen/filters/rules/_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from ...errors import FilterError
from ...const import GRAMPS_LOCALE as glocale
from ...lib.serialize import from_dict

_ = glocale.translation.gettext

Expand Down Expand Up @@ -150,10 +151,18 @@ def check(self):
"""Verify the number of rule values versus the number of rule labels."""
return len(self.list) == len(self.labels)

def apply(self, dummy_db, dummy_person):
def apply_to_one(self, dummy_db, dummy_data):
"""Apply the rule to some database entry; must be overwritten."""
return True

def get_object(self, data):
"""
Create an object, but only do it once per data.
"""
if "_object" not in data:
data["_object"] = from_dict(data)
return data["_object"]

def display_values(self):
"""Return the labels and values of this rule."""
l_v = (
Expand Down
3 changes: 2 additions & 1 deletion gramps/gen/filters/rules/citation/_hascitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def prepare(self, db, user):
except:
pass

def apply(self, dbase, citation):
def apply_to_one(self, dbase, data):
citation = self.get_object(data)
if not self.match_substring(0, citation.get_page()):
return False

Expand Down
7 changes: 4 additions & 3 deletions gramps/gen/filters/rules/citation/_hassource.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class HasSource(HasSourceBase):
description = _("Matches citations with a source of a particular " "value")
category = _("Source filters")

def apply(self, dbase, citation):
source = dbase.get_source_from_handle(citation.get_reference_handle())
if HasSourceBase.apply(self, dbase, source):
def apply_to_one(self, dbase, data):
citation = self.get_object(data)
source_data = dbase.get_raw_source_data(citation.get_reference_handle())
if HasSourceBase.apply_to_one(self, dbase, source_data):
return True
return False
6 changes: 4 additions & 2 deletions gramps/gen/filters/rules/citation/_hassourceidof.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#
# -------------------------------------------------------------------------
from .._hasgrampsid import HasGrampsId
from gramps.gen.lib.serialize import to_dict


# -------------------------------------------------------------------------
Expand All @@ -49,8 +50,9 @@ class HasSourceIdOf(HasGrampsId):
description = _("Matches a citation with a source with a specified Gramps " "ID")
category = _("Source filters")

def apply(self, dbase, citation):
def apply_to_one(self, dbase, data):
citation = self.get_object(data)
source = dbase.get_source_from_handle(citation.get_reference_handle())
if HasGrampsId.apply(self, dbase, source):
if HasGrampsId.apply_to_one(self, dbase, to_dict(source)):
return True
return False
6 changes: 4 additions & 2 deletions gramps/gen/filters/rules/citation/_hassourcenoteregexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#
# -------------------------------------------------------------------------
from .._hasnoteregexbase import HasNoteRegexBase
from gramps.gen.lib.serialize import to_dict


# -------------------------------------------------------------------------
Expand All @@ -58,8 +59,9 @@ class HasSourceNoteRegexp(HasNoteRegexBase):
)
category = _("Source filters")

def apply(self, db, citation):
def apply_to_one(self, db, data):
citation = self.get_object(data)
source = db.get_source_from_handle(citation.get_reference_handle())
if HasNoteRegexBase.apply(self, db, source):
if HasNoteRegexBase.apply_to_one(self, db, to_dict(source)):
return True
return False
4 changes: 2 additions & 2 deletions gramps/gen/filters/rules/citation/_matchespagesubstringof.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ class MatchesPageSubstringOf(Rule):
category = _("General filters")
allow_regex = True

def apply(self, db, object):
def apply_to_one(self, db, data):
"""Apply the filter"""
return self.match_substring(0, object.get_page())
return self.match_substring(0, data["page"])
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def prepare(self, db, user):
MatchesFilterBase.prepare(self, db, user)
self.MRF_filt = self.find_filter()

def apply(self, db, object):
def apply_to_one(self, db, data):
object = self.get_object(data)
if self.MRF_filt is None:
return False

Expand Down
6 changes: 3 additions & 3 deletions gramps/gen/filters/rules/citation/_matchessourcefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def prepare(self, db, user):
MatchesFilterBase.prepare(self, db, user)
self.MRF_filt = self.find_filter()

def apply(self, db, object):
def apply_to_one(self, db, data):
if self.MRF_filt is None:
return False

source_handle = object.source_handle
if self.MRF_filt.check(db, source_handle):
source_handle = data["source_handle"]
if self.MRF_filt.apply_to_one(db, source_handle):
return True
return False
Loading