Skip to content

Commit

Permalink
First rough pass at all filters
Browse files Browse the repository at this point in the history
  • Loading branch information
dsblank committed Nov 16, 2024
1 parent f04ac4e commit 40322c9
Show file tree
Hide file tree
Showing 141 changed files with 347 additions and 258 deletions.
6 changes: 4 additions & 2 deletions gramps/gen/filters/_genericfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def walk_filters(self, filter, result):
if rules:
result.append((filter.invert, filter.logical_op, rules))

def apply_logical_op_to_all(
def apply_to_one_logical_op_to_all(
self, db, id_list, apply_logical_op, user=None, tupleind=None, tree=False
):
final_list = []
Expand Down Expand Up @@ -260,7 +260,9 @@ def apply_to_one(self, db, data):
raise Exception("invalid operator: %r" % self.logical_op)
return res != self.invert

def apply_to_all(self, db, id_list=None, tupleind=None, user=None, tree=False):
def apply_to_one_to_all(
self, db, id_list=None, tupleind=None, user=None, tree=False
):
"""
Apply the filter using db.
If id_list given, the handles in id_list are used. If not given
Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/filters/_paramfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, source=None):
def set_parameter(self, param):
self.param_list = [param]

def apply(self, db, id_list=None, user=None):
def apply_to_all(self, db, id_list=None, user=None):
for rule in self.flist:
# rule.set_list(self.param_list)
#
Expand All @@ -62,7 +62,7 @@ def apply(self, db, id_list=None, user=None):
"Custom filters can not twice be used" " in a parameter filter"
)
rule.requestprepare(db, user)
result = GenericFilter.apply(self, db, id_list)
result = GenericFilter.apply_to_all(self, db, id_list)
for rule in self.flist:
rule.requestreset()
return result
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_obj(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
3 changes: 0 additions & 3 deletions gramps/gen/filters/rules/_hastagbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ def prepare(self, db, user):
else:
self.map = set()

def get_rules_with_maps(self):
return [self]

def apply_to_one(self, db, data):
"""
Apply the rule. Return True for a match.
Expand Down
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
1 change: 0 additions & 1 deletion gramps/gen/filters/rules/_matchesfilterbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def apply_to_one(self, db, data):
filters = gramps.gen.filters.CustomFilters.get_filters_dict(self.namespace)
if self.list[0] in filters:
filt = filters[self.list[0]]
# FIXME: Is this correct?
return filt.apply_to_one(db, data)
return False

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"])
9 changes: 9 additions & 0 deletions 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 @@ -154,6 +155,14 @@ 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
5 changes: 3 additions & 2 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):
def apply_to_one(self, dbase, data):
citation = self.get_object(data)
source = dbase.get_source_from_handle(citation.get_reference_handle())
if HasSourceBase.apply(self, dbase, source):
if HasSourceBase.apply_to_one(self, dbase, source.handle):
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
1 change: 1 addition & 0 deletions gramps/gen/filters/rules/citation/_hastag.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ class HasTag(HasTagBase):
labels = [_("Tag:")]
name = _("Citations with the <tag>")
description = _("Matches citations with the particular tag")
table = "citation"
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
Loading

0 comments on commit 40322c9

Please sign in to comment.