diff --git a/src/cli/grampscli.py b/src/cli/grampscli.py index 2589605f8..edb7b8d99 100644 --- a/src/cli/grampscli.py +++ b/src/cli/grampscli.py @@ -290,7 +290,7 @@ class CLIManager(object): """ self._pmgr.reg_plugins(const.PLUGINS_DIR, dbstate, uistate) self._pmgr.reg_plugins(const.USER_PLUGINS, dbstate, uistate, - append=False) + append=False, load_on_reg=True) def startcli(errors, argparser): """ diff --git a/src/gen/plug/_manager.py b/src/gen/plug/_manager.py index cbe4c625e..438be1543 100644 --- a/src/gen/plug/_manager.py +++ b/src/gen/plug/_manager.py @@ -96,7 +96,8 @@ class BasePluginManager(object): self.__registereddir_set = set() self.__loaded_plugins = {} - def reg_plugins(self, direct, dbstate=None, uistate=None, append=True): + def reg_plugins(self, direct, dbstate=None, uistate=None, + append=True, load_on_reg=False): """ Searches the specified directory, and registers python plugin that are being defined in gpr.py files. @@ -125,11 +126,24 @@ class BasePluginManager(object): self.__registereddir_set.add(dirpath) self.__pgr.scan_dir(dirpath) - # load plugins that request to be loaded on startup - for plugin in self.__pgr.filter_load_on_reg(): - mod = self.load_plugin(plugin) - if hasattr(mod, "load_on_reg"): - mod.load_on_reg(dbstate, uistate) + if load_on_reg: + # Run plugins that request to be loaded on startup and + # have a load_on_reg callable. + for plugin in self.__pgr.filter_load_on_reg(): + mod = self.load_plugin(plugin) + if hasattr(mod, "load_on_reg"): + try: + results = mod.load_on_reg(dbstate, uistate, plugin) + except: + import traceback + traceback.print_exc() + print "Plugin '%s' did not run; continuing..." % plugin.name + continue + try: + iter(results) + plugin.data += results + except: + plugin.data = results def is_loaded(self, pdata_id): """ @@ -354,6 +368,72 @@ class BasePluginManager(object): """ return self.__pgr.docgen_plugins() + def get_reg_general(self, category=None): + """ Return list of registered general libs + """ + return self.__pgr.general_plugins(category) + + def get_plugin_data(self, category): + """ + Gets all of the data from general plugins of type category. + plugin.data maybe a single item, an iterable, or a callable. + + >>> PLUGMAN.get_plugin_data('CSS') + + """ + retval = [] + data = None + for plugin in self.__pgr.general_plugins(category): + if callable(plugin.data): + try: + data = plugin.data() + except: + import traceback + traceback.print_exc() + else: + data = plugin.data + try: + iter(data) + retval.extend(data) + except: + retval.append(data) + return retval + + def process_plugin_data(self, category): + """ + Gathers all of the data from general plugins of type category, + and pass it to a single process function from one of those + plugins. + + >>> PLUGMAN.process_plugin_data('CSS') + + """ + retval = [] + data = None + process = None + for plugin in self.__pgr.general_plugins(category): + if plugin.process is not None: + mod = self.load_plugin(plugin) + if hasattr(mod, plugin.process): + process = getattr(mod, plugin.process) + if callable(plugin.data): + try: + data = plugin.data() + except: + import traceback + traceback.print_exc() + else: + data = plugin.data + if data: + try: + iter(data) + retval.extend(data) + except: + retval.append(data) + if process: + return process(retval) + return retval + def get_import_plugins(self): """ Get the list of import plugins. diff --git a/src/gen/plug/_pluginreg.py b/src/gen/plug/_pluginreg.py index 196666d84..af77f0c29 100644 --- a/src/gen/plug/_pluginreg.py +++ b/src/gen/plug/_pluginreg.py @@ -398,6 +398,9 @@ class PluginData(object): self._menu_label = '' #VIEW and SIDEBAR attr self._order = END + #GENERAL attr + self._data = [] + self._process = None def _set_id(self, id): self._id = id @@ -878,6 +881,26 @@ class PluginData(object): order = property(_get_order, _set_order) + #GENERAL attr + def _set_data(self, data): + if not self._ptype in (GENERAL,): + raise ValueError, 'data may only be set for GENERAL plugins' + self._data = data + + def _get_data(self): + return self._data + + def _set_process(self, process): + if not self._ptype in (GENERAL,): + raise ValueError, 'process may only be set for GENERAL plugins' + self._process = process + + def _get_process(self): + return self._process + + data = property(_get_data, _set_data) + process = property(_get_process, _set_process) + def newplugin(): """ Function to create a new plugindata object, add it to list of @@ -1043,6 +1066,8 @@ class PluginRegister(object): rmlist = [] ind = lenpd-1 for plugin in self.__plugindata[lenpd:]: + if plugin.category == 'TEST': + import pdb; pdb.set_trace() ind += 1 plugin.directory = dir if not valid_plugin_version(plugin.gramps_target_version): @@ -1150,10 +1175,14 @@ class PluginRegister(object): """ return self.type_plugins(DOCGEN) - def general_plugins(self): + def general_plugins(self, category=None): """Return a list of PluginData that are of type GENERAL """ - return self.type_plugins(GENERAL) + plugins = self.type_plugins(GENERAL) + if category: + return [plugin for plugin in plugins + if plugin.category == category] + return plugins def mapservice_plugins(self): """Return a list of PluginData that are of type MAPSERVICE @@ -1184,4 +1213,5 @@ class PluginRegister(object): """Return a list of PluginData that have load_on_reg == True """ return [self.get_plugin(id) for id in - set([x.id for x in self.__plugindata if x.load_on_reg == True])] + set([x.id for x in self.__plugindata + if x.load_on_reg == True])] diff --git a/src/gui/pluginmanager.py b/src/gui/pluginmanager.py index abfdefcb7..7fb9624fe 100644 --- a/src/gui/pluginmanager.py +++ b/src/gui/pluginmanager.py @@ -312,3 +312,8 @@ class GuiPluginManager(gen.utils.Callback): """ return [plg for plg in self.basemgr.get_reg_docgens() if plg.id not in self.__hidden_plugins] + + def get_reg_general(self, category=None): + return [plg for plg in self.basemgr.get_reg_general(category) + if plg.id not in self.__hidden_plugins] +