Skip to content

Commit

Permalink
Remove unused hook plugin manager
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Sep 14, 2017
1 parent acd9642 commit 88d8e4b
Showing 1 changed file with 0 additions and 150 deletions.
150 changes: 0 additions & 150 deletions lib/galaxy/visualization/plugins/pluginframework.py
Expand Up @@ -159,156 +159,6 @@ def _load_plugin(self, plugin_path):
return plugin


# ============================================================================= plugin managers using hooks
class HookPluginManager(PluginManager):
"""
A hook plugin is a directory containing python modules or packages that:
* allow creating, including, and running custom code at specific 'hook'
points/events
* are not tracked in the Galaxy repository and allow adding custom code
to a Galaxy installation
A HookPluginManager imports the plugin code needed and calls the plugin's
hook functions at the specified time.
"""
#: the python file that will be imported - hook functions should be contained here
loading_point_filename = 'plugin.py'
hook_fn_prefix = 'hook_'

def is_plugin(self, plugin_path):
"""
Determines whether the given filesystem path contains a hookable plugin.
All sub-directories that contain ``loading_point_filename`` are considered
plugins.
:type plugin_path: string
:param plugin_path: relative or absolute filesystem path to the
potential plugin
:rtype: bool
:returns: True if the path contains a plugin
"""
if not super(HookPluginManager, self).is_plugin(plugin_path):
return False
# TODO: possibly switch to <plugin.name>.py or __init__.py
if self.loading_point_filename not in os.listdir(plugin_path):
return False
return True

def load_plugin(self, plugin_path):
"""
Import the plugin ``loading_point_filename`` and attach to the plugin bunch.
Plugin bunches are decorated with:
* name : the plugin name
* path : the plugin path
* module : the plugin code
:type plugin_path: string
:param plugin_path: relative or absolute filesystem path to the plugin
:rtype: ``util.bunch.Bunch``
:returns: the loaded plugin object
"""
plugin = super(HookPluginManager, self).load_plugin(plugin_path)

loading_point_name = self.loading_point_filename[:-3]
plugin['module'] = self.import_plugin_module(loading_point_name, plugin)
return plugin

def import_plugin_module(self, loading_point_name, plugin, import_as=None):
"""
Import the plugin code and cache the module in the plugin object.
:type loading_point_name: string
:param loading_point_name: name of the python file to import (w/o extension)
:type plugin: ``util.bunch.Bunch``
:param plugin: the plugin containing the template to render
:type import_as: string
:param import_as: namespace to use for imported module
This will be prepended with the ``__name__`` of this file.
Defaults to ``plugin.name``
:rtype: ``util.bunch.Bunch``
:returns: the loaded plugin object
"""
# add this name to import_as (w/ default to plugin.name) to prevent namespace pollution in sys.modules
import_as = '%s.%s' % (__name__, (import_as or plugin.name))
module_file, pathname, description = imp.find_module(loading_point_name, [plugin.path])
try:
# TODO: hate this hack but only way to get package imports inside the plugin to work?
sys.path.append(plugin.path)
# sys.modules will now have import_as in its list
module = imp.load_module(import_as, module_file, pathname, description)
finally:
module_file.close()
if plugin.path in sys.path:
sys.path.remove(plugin.path)
return module

def run_hook(self, hook_name, *args, **kwargs):
"""
Search all plugins for a function named ``hook_fn_prefix`` + ``hook_name``
and run it passing in args and kwargs.
Return values from each hook are returned in a dictionary keyed with the
plugin names.
:type hook_name: string
:param hook_name: name (suffix) of the hook to run
:rtype: dictionary
:returns: where keys are plugin.names and
values return values from the hooks
"""
# TODO: is hook prefix necessary?
# TODO: could be made more efficient if cached by hook_name in the manager on load_plugin
# (low maint. overhead since no dynamic loading/unloading of plugins)
hook_fn_name = ''.join([self.hook_fn_prefix, hook_name])
returned = {}
for plugin_name, plugin in self.plugins.items():
hook_fn = getattr(plugin.module, hook_fn_name, None)

if hook_fn and hasattr(hook_fn, '__call__'):
try:
fn_returned = hook_fn(*args, **kwargs)
returned[plugin.name] = fn_returned
except Exception:
# fail gracefully and continue with other plugins
log.exception('Hook function "%s" failed for plugin "%s"', hook_name, plugin.name)

# not sure of utility of this - seems better to be fire-and-forget pub-sub
return returned

def filter_hook(self, hook_name, hook_arg, *args, **kwargs):
"""
Search all plugins for a function named ``hook_fn_prefix`` + ``hook_name``
and run the first with ``hook_arg`` and every function after with the
return value of the previous.
..note:
This makes plugin load order very important.
:type hook_name: string
:param hook_name: name (suffix) of the hook to run
:type hook_arg: any
:param hook_arg: the arg to be passed between hook functions
:rtype: any
:returns: the modified hook_arg
"""
hook_fn_name = ''.join([self.hook_fn_prefix, hook_name])
for plugin_name, plugin in self.plugins.items():
hook_fn = getattr(plugin.module, hook_fn_name, None)

if hook_fn and hasattr(hook_fn, '__call__'):
try:
hook_arg = hook_fn(hook_arg, *args, **kwargs)

except Exception:
# fail gracefully and continue with other plugins
log.exception('Filter hook function "%s" failed for plugin "%s"', hook_name, plugin.name)

# may have been altered by hook fns, return
return hook_arg


class PluginManagerStaticException(PluginManagerException):
"""Exception for plugin framework static directory set up errors.
"""
Expand Down

0 comments on commit 88d8e4b

Please sign in to comment.