From eb36980715ee5bea7d7bc96556d74e389d2c2b13 Mon Sep 17 00:00:00 2001 From: prculley Date: Sun, 2 Dec 2018 13:52:24 -0600 Subject: [PATCH] New feature to allow new rules in their own catagory in addons --- gramps/gen/plug/_manager.py | 13 ++++++++++ gramps/gen/plug/_pluginreg.py | 45 ++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/gramps/gen/plug/_manager.py b/gramps/gen/plug/_manager.py index 73166b622..8fc4348f7 100644 --- a/gramps/gen/plug/_manager.py +++ b/gramps/gen/plug/_manager.py @@ -39,6 +39,7 @@ import os import sys import re import logging +import importlib LOG = logging.getLogger('._manager') LOG.progagate = True from ..const import GRAMPS_LOCALE as glocale @@ -197,6 +198,18 @@ class BasePluginManager: plugin.data += results except: plugin.data = results + # Get the addon rules and import them and make them findable + for plugin in self.__pgr.rule_plugins(): + mod = self.load_plugin(plugin) # load the addon rule + # get place in rule heirarchy to put the new rule + obj_rules = importlib.import_module( + 'gramps.gen.filters.rules.' + plugin.namespace.lower()) + # get the new rule class object + r_class = getattr(mod, plugin.ruleclass) + # make the new rule findable via import statements + setattr(obj_rules, plugin.ruleclass, r_class) + # and add it to the correct fiter editor list + obj_rules.editor_rule_list.append(r_class) def is_loaded(self, pdata_id): """ diff --git a/gramps/gen/plug/_pluginreg.py b/gramps/gen/plug/_pluginreg.py index fdf855c15..3da23c35d 100644 --- a/gramps/gen/plug/_pluginreg.py +++ b/gramps/gen/plug/_pluginreg.py @@ -72,8 +72,9 @@ RELCALC = 9 GRAMPLET = 10 SIDEBAR = 11 DATABASE = 12 -PTYPE = [REPORT , QUICKREPORT, TOOL, IMPORT, EXPORT, DOCGEN, GENERAL, - MAPSERVICE, VIEW, RELCALC, GRAMPLET, SIDEBAR, DATABASE] +RULE = 13 +PTYPE = [REPORT, QUICKREPORT, TOOL, IMPORT, EXPORT, DOCGEN, GENERAL, + MAPSERVICE, VIEW, RELCALC, GRAMPLET, SIDEBAR, DATABASE, RULE] PTYPE_STR = { REPORT: _('Report') , QUICKREPORT: _('Quickreport'), @@ -88,6 +89,7 @@ PTYPE_STR = { GRAMPLET: _('Gramplet'), SIDEBAR: _('Sidebar'), DATABASE: _('Database'), + RULE: _('Rule') } #possible report categories @@ -211,7 +213,7 @@ class PluginData: The python path where the plugin implementation can be found .. attribute:: ptype The plugin type. One of REPORT , QUICKREPORT, TOOL, IMPORT, - EXPORT, DOCGEN, GENERAL, MAPSERVICE, VIEW, GRAMPLET, DATABASE + EXPORT, DOCGEN, GENERAL, MAPSERVICE, VIEW, GRAMPLET, DATABASE, RULE .. attribute:: authors List of authors of the plugin, default=[] .. attribute:: authors_email @@ -362,6 +364,13 @@ class PluginData: .. attribute:: reset_system Boolean to indicate that the system (sys.modules) should be reset. + + Attributes for RULE plugins + + .. attribute:: namespace + The class (Person, Event, Media, etc.) the rule applies to. + .. attribute:: ruleclass + The exact class name of the rule; ex: HasSourceParameter """ def __init__(self): @@ -440,6 +449,9 @@ class PluginData: #GENERAL attr self._data = [] self._process = None + #RULE attr + self._ruleclass = None + self._namespace = None def _set_id(self, id): self._id = id @@ -987,6 +999,26 @@ class PluginData: data = property(_get_data, _set_data) process = property(_get_process, _set_process) + #RULE attr + def _set_ruleclass(self, data): + if self._ptype != RULE: + raise ValueError('ruleclass may only be set for RULE plugins') + self._ruleclass = data + + def _get_ruleclass(self): + return self._ruleclass + + def _set_namespace(self, data): + if self._ptype != RULE: + raise ValueError('namespace may only be set for RULE plugins') + self._namespace = data + + def _get_namespace(self): + return self._namespace + + ruleclass = property(_get_ruleclass, _set_ruleclass) + namespace = property(_get_namespace, _set_namespace) + def newplugin(): """ Function to create a new plugindata object, add it to list of @@ -1034,6 +1066,7 @@ def make_environment(**kwargs): 'EXPORT': EXPORT, 'DOCGEN': DOCGEN, 'GENERAL': GENERAL, + 'RULE': RULE, 'MAPSERVICE': MAPSERVICE, 'VIEW': VIEW, 'RELCALC': RELCALC, @@ -1350,6 +1383,12 @@ class PluginRegister: """ return self.type_plugins(DATABASE) + def rule_plugins(self): + """ + Return a list of :class:`PluginData` that are of type RULE + """ + return self.type_plugins(RULE) + def filter_load_on_reg(self): """ Return a list of :class:`PluginData` that have load_on_reg == True