Changes

Jump to: navigation, search

Develop an Add-on Rule

1,706 bytes removed, 17:53, 23 September 2022
m
Converting a query to an add-on Filter Rule
== Converting a query to an add-on Filter Rule ==
=== Sample Gramps Plug-in Registration file ===
[https://raw.githubusercontentgithub.com/gramps-project/addons-source/blob/master/FilterRules/infamilyrulehasrolerule.gpr.py <code>infamilyrulehasrolerule.gpr.py</code>]
<pre>#
# for Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2020 2018 Paul Culley# Copyright (C) 2020 Matthias Kemmer
#
# This program is free software; you can redistribute it and/or modify
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""Filter rule that matches people who are matched by to match an Event Role with a family filterparticular value."""
register(RULE,
id='PersonsInFamilyFilterMatchHasPersonEventRole', name=_('"People who are part of families matching <filter>'with events with a selected role"), description=_('People who are part of families matching <filter>'"Matches people with an event with a selected role"), version='10.0.914', authors=["Matthias Kemmer", "Paul Culley"], authors_email=["[email protected]", "[email protected]"],
gramps_target_version='5.1',
status=STABLE,
fname="infamilyrulehasrolerule.py", ruleclass='PersonsInFamilyFilterMatchHasPersonEventRole', # must match the be rule class name
namespace='Person', # one of the primary object classes
)
 
register(RULE,
id='HasFamilyEventRole',
name=_("Families with events with a selected role"),
description=_("Matches families with an event with a selected role"),
version='0.0.14',
authors=["Paul Culley"],
authors_email=["[email protected]"],
gramps_target_version='5.1',
status=STABLE,
fname="hasrolerule.py",
ruleclass='HasFamilyEventRole', # must be rule class name
namespace='Family', # one of the primary object classes
)
</pre>
=== Sample Gramps Plug-in source file ===
[https://github.com/gramps-project/addons-source/blob/master/FilterRules/infamilyrulehasrolerule.py <code>infamilyrulehasrolerule.py</code>]
''Filenames'' or <code>Filenames</code>
<pre>##
# plug-in Python module
# for Gramps - a GTK+/GNOME based genealogy program
## Copyright (C) 2020 2018 Paul Culley# Copyright (C) 2020 Matthias Kemmer#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""Filter rule to match persons in with a matching familyparticular event."""# -------------------------------------------------------------------------# # Standard Python modules# # -------------------------------------------------------------------------
# -------------------------------------------------------------------------
#
# Gramps modules
#
# -------------------------------------------------------------------------
from gramps.gen.lib.eventroletype import EventRoleTypefrom gramps.gui.editors.filtereditor import MySelect, MyBoolean, MyFiltersfrom gramps.gen.filters.rules._matchesfilterbase import MatchesFilterBaseRule
from gramps.gen.const import GRAMPS_LOCALE as glocale
try:
_ = _trans.gettext
# These globals are the only easy way I could think of to communicate between
# checkboxes to make sure at least one was selected.
child_state = True # used to indicate state of checkbox
parent_state = True
 
 
class IncChildren(MyBoolean):
"""Include children."""
class Roletype(MySelect):
""" Provide a Role type selector """
def __init__(self, db):
MyBooleanMySelect.__init__(self, _("Include Children")) self.set_tooltip_text(_("Include the children in the matching" " families.")) self.connect("toggled", self.toggled) self.set_active(True)  def toggled(selfEventRoleType, widget): """Make sure user doesn't get to turn off both children and parents.""" if not parent_state: if not widget.get_active(): widget.set_active(True) global child_state child_state = widgetdb.get_active() # printget_event_roles("child:", child_state def set_text(self, val): """Set the selector state to display the passed value.""" is_active = bool(int(val)) self.set_active(is_active) global child_state child_state = is_active 
class IncParents(MyBoolean):
"""Provide a negation switch."""
class NoMatch(MyBoolean):
""" Provide a negation switch """
def __init__(self, db):
MyBoolean.__init__(self, _("Include ParentsDoes NOT match with selected Role")) self.set_tooltip_text(_("Include Finds the parents in the matchingitems that don't have event Roles " " familiesof the selected type.")) self.connect("toggled", self.toggled)# -------------------------------------------------------------------------# # HasEvent# self.set_active(True)# -------------------------------------------------------------------------
def toggled(self, widget):
"""Make sure user doesn't get to turn off both children and parents."""
if not child_state:
if not widget.get_active():
widget.set_active(True)
global parent_state
parent_state = widget.get_active()
# print("parent:", parent_state)
def set_textclass HasPersonEventRole(self, valRule): """Set the selector state to display the passed value.Rule that checks for a person with a selected event role""" is_active = bool(int(val)) self.set_active(is_active) global parent_state parent_state = is_active
labels = [(_('Role'), Roletype),
(_('Inverse'), NoMatch)]
name = _('People with events with the <role>')
description = _("Matches people with an event with a selected role")
category = _('Event filters')
class FamFilt def apply(MyFiltersself, dbase, person): """Add custom family filter selector if not self."""list[0]: return False # This is a horrible hack that is needed because the filtereditor doesn't # have support for a 'Family Filter name' selector. So we have to make our # ownevent_ref in person. Furthermore, we don't have the needed reference to the 'filterdb', # the list of custom filters. def __init__get_event_ref_list(self, db): import inspect if not event_ref: continue stack = inspect if self.stack() # our stack frame caller_locals = stacklist[1][0].f_locals # locals from caller # the caller has an attribute == 'filterdb1' which has what we need: MyFilters if event_ref.role.__init__xml_str() != self, caller_locals.list["filterdb"0].get_filters('Family')): return True else:# -------------------------------------------------------------------------## Person part of matching family## -------------------------------------------------------------------------class PersonsInFamilyFilterMatch if event_ref.role.xml_str(MatchesFilterBase)== self.list[0]: """Rule that checks for a person with a selected event role.""" return True return False
labels = [(_('Family Filter name:'), FamFilt),
(_('Include Children'), IncChildren),
(_('Include Parents'), IncParents)]
name = _('People who are part of families matching <filter>')
description = _("People who are part of families matching <filter>")
category = _('General filters')
# we want to have this filter show family filters
namespace = 'Family'
def prepareclass HasFamilyEventRole(self, db, userRule): """Prepare Rule that checks for a reference list for the filter.family with a selected event role""" self.persons = set() MatchesFilterBase.prepare(self, db, user) self.MFF_filt = self.find_filter() if self.MFF_filt: for family_handle in db.iter_family_handles(): if self.MFF_filt.check(db, family_handle): family = db.get_family_from_handle(family_handle) if bool(int(self.list[2])): father = family.get_father_handle() mother = family.get_mother_handle() self.persons.add(father) self.persons.add(mother) if bool(int(self.list[1])): for child_ref in family.get_child_ref_list(): self.persons.add(child_ref.ref)
def applylabels = [(self_('Role'), _dbRoletype), (_('Inverse'), objNoMatch)] name = _('Families with events with the <role>'): " description = _("Matches families with an event with a selected role") Return True if a person applies to the filter rule. category = _('Event filters')
def apply(self, dbase, family): if not self.list[0]:returns: True or return False """for event_ref in family.get_event_ref_list(): if not event_ref: continue if self.list[1] == '1': if objevent_ref.get_handlerole.xml_str() in != self.personslist[0]: return True else: if event_ref.role.xml_str() == self.list[0]: return True
return False
 
</pre>
4,529
edits

Navigation menu