Source code for paws.core.plugins.PluginManager
from __future__ import print_function
import importlib
from collections import OrderedDict
from ..models.TreeModel import TreeModel
from .. import plugins as pgns
from .. import pawstools
from .PawsPlugin import PawsPlugin
[docs]class PluginManager(TreeModel):
"""Tree for storing, browsing, and managing PawsPlugins"""
def __init__(self):
flag_dict = OrderedDict()
flag_dict['select'] = False
super(PluginManager,self).__init__(flag_dict)
self.plugins = self._root_dict
self.message_callback = print
# dict of clones for plugins across threads:
self.plugin_clones = OrderedDict()
# dict of bools to keep track of who is at work:
self.plugin_running = OrderedDict()
[docs] def add_plugin(self,plugin_name,plugin_module):
"""Import, name, and add a plugin.
After a plugin is added to a plugin_manager,
it is available as plugin_manager.plugins[plugin_name].
Parameters
----------
plugin_name : str
Name that will be used to refer to this plugin after it is added.
plugin_module : str
Name of the plugin module.
Example: If class MyPlugin is in the CATEGORY.MyPlugin module,
retrieve it with `plugin_module` = 'CATEGORY.MyPlugin'.
"""
p = self.get_plugin(plugin_module)
p.message_callback = self.message_callback
if not self.is_tag_valid(plugin_name):
raise pawstools.PluginNameError(self.tag_error_message(plugin_name))
# NOTE: set_item() is used here, because subclasses reimplement set_item().
self.set_item(plugin_name,p)
self.plugin_running[plugin_name] = False
[docs] def start_plugin(self,plugin_name):
self.plugin_running[plugin_name] = True
self.plugins[plugin_name].start()
[docs] def stop_plugin(self,plugin_name):
self.plugins[plugin_name].stop()
self.plugin_running[plugin_name] = False
[docs] def get_plugin(self,plugin_module):
"""Import, instantiate, return a PawsPlugin from its module.
This can also be used to test the Python environment
for compatibility with a plugin.
Parameters
----------
plugin_module : str
Name of the plugin module.
See add_plugin().
Returns
-------
PawsPlugin
An instance of the PawsPlugin subclass defined in `plugin_module`.
"""
mod = importlib.import_module('.'+plugin_module,pgns.__name__)
return mod.__dict__[plugin_module]()
[docs] def load_plugin(self,plugin_name,plugin_setup_dict):
"""Load, set up, and start() a PawsPlugin, given its setup_dict().
Parameters
----------
plugin_name : str
Name that will be used to refer to this plugin after it is added.
plugin_setup_dict : dict
Dict specifying plugin setup,
probably generated by calling the plugin's own setup_dict() method.
"""
plugin_mod = plugin_setup_dict['plugin_module']
p = self.get_plugin(plugin_mod)
self.set_item(plugin_name,p)
for inp_name in p.inputs.keys():
if inp_name in plugin_setup_dict['inputs'].keys():
self.set_input(plugin_name,inp_name,plugin_setup_dict['inputs'][inp_name])
p.start()
[docs] def n_plugins(self):
"""Return number of plugins currently loaded."""
return len(self.plugins)
[docs] def build_tree(self,x):
"""Return a dict describing a tree-like structure of this object.
This is a reimplemention of TreeModel.build_tree()
to define this object's child tree structure.
For a PluginManager, a dict is provided for each PawsPlugin,
where the dict contains the results of calling
self.build_tree(plugin.inputs) and self.build_tree(plugin.content()).
"""
if isinstance(x,PawsPlugin):
d = OrderedDict()
d['inputs'] = self.build_tree(x.inputs)
d['content'] = self.build_tree(x.content())
else:
return super(PluginManager,self).build_tree(x)
return d