Enhanced GENERAL plugins for flexible API: added data and process to GENERAL plugindata, more details at http://www.gramps-project.org/wiki/index.php?title=Addons_development#General_Plugins

svn: r15701
This commit is contained in:
Doug Blank 2010-08-11 05:42:46 +00:00
parent abd7b09450
commit 0199cd7da5
4 changed files with 125 additions and 10 deletions

View File

@ -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):
"""

View File

@ -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')
<a list of raw data items>
"""
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')
<a list of processed data items>
"""
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.

View File

@ -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])]

View File

@ -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]