mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Reduce code duplication between job metrics and dependency resolution plugins.
Same code reused again downstream in workflow schedulers plugin framework.
This commit is contained in:
@@ -3,7 +3,7 @@ import os
|
||||
|
||||
from xml.etree import ElementTree
|
||||
|
||||
from galaxy.util.submodules import submodules
|
||||
from galaxy.util import plugin_config
|
||||
from galaxy import util
|
||||
|
||||
from ..metrics import formatting
|
||||
@@ -49,17 +49,8 @@ class JobMetrics( object ):
|
||||
return self.job_instrumenters[ destination_id ].collect_properties( job_id, job_directory )
|
||||
|
||||
def __plugins_dict( self ):
|
||||
plugin_dict = {}
|
||||
for plugin_module in self.__plugin_modules():
|
||||
for clazz in plugin_module.__all__:
|
||||
plugin_type = getattr( clazz, 'plugin_type', None )
|
||||
if plugin_type:
|
||||
plugin_dict[ plugin_type ] = clazz
|
||||
return plugin_dict
|
||||
|
||||
def __plugin_modules( self ):
|
||||
import galaxy.jobs.metrics.instrumenters
|
||||
return submodules( galaxy.jobs.metrics.instrumenters )
|
||||
return plugin_config.plugins_dict( galaxy.jobs.metrics.instrumenters, 'plugin_type' )
|
||||
|
||||
|
||||
class NullJobInstrumenter( object ):
|
||||
@@ -117,14 +108,7 @@ class JobInstrumenter( object ):
|
||||
return per_plugin_properites
|
||||
|
||||
def __plugins_for_element( self, plugins_element ):
|
||||
plugins = []
|
||||
for plugin_element in plugins_element.getchildren():
|
||||
plugin_type = plugin_element.tag
|
||||
plugin_kwds = dict( plugin_element.items() )
|
||||
plugin_kwds.update( self.extra_kwargs )
|
||||
plugin = self.plugin_classes[ plugin_type ]( **plugin_kwds )
|
||||
plugins.append( plugin )
|
||||
return plugins
|
||||
return plugin_config.load_plugins_from_element(self.plugin_classes, plugins_element, self.extra_kwargs)
|
||||
|
||||
@staticmethod
|
||||
def from_file( plugin_classes, conf_file, **kwargs ):
|
||||
|
||||
@@ -12,7 +12,7 @@ from xml.etree import ElementTree
|
||||
from .resolvers import INDETERMINATE_DEPENDENCY
|
||||
from .resolvers.galaxy_packages import GalaxyPackageDependencyResolver
|
||||
from .resolvers.tool_shed_packages import ToolShedPackageDependencyResolver
|
||||
from galaxy.util.submodules import submodules
|
||||
from galaxy.util import plugin_config
|
||||
|
||||
|
||||
def build_dependency_manager( config ):
|
||||
@@ -110,24 +110,9 @@ class DependencyManager( object ):
|
||||
:param root: Object representing the root ``<dependency_resolvers>`` object in the file.
|
||||
:type root: ``xml.etree.ElementTree.Element``
|
||||
"""
|
||||
resolvers = []
|
||||
resolvers_element = root
|
||||
for resolver_element in resolvers_element.getchildren():
|
||||
resolver_type = resolver_element.tag
|
||||
resolver_kwds = dict(resolver_element.items())
|
||||
resolver = self.resolver_classes[resolver_type](self, **resolver_kwds)
|
||||
resolvers.append(resolver)
|
||||
return resolvers
|
||||
extra_kwds = dict( dependency_manager=self )
|
||||
return plugin_config.load_plugins_from_element( self.resolver_classes, root, extra_kwds )
|
||||
|
||||
def __resolvers_dict( self ):
|
||||
resolver_dict = {}
|
||||
for resolver_module in self.__resolver_modules():
|
||||
for clazz in resolver_module.__all__:
|
||||
resolver_type = getattr(clazz, 'resolver_type', None)
|
||||
if resolver_type:
|
||||
resolver_dict[resolver_type] = clazz
|
||||
return resolver_dict
|
||||
|
||||
def __resolver_modules( self ):
|
||||
import galaxy.tools.deps.resolvers
|
||||
return submodules( galaxy.tools.deps.resolvers )
|
||||
return plugin_config.plugins_dict( galaxy.tools.deps.resolvers, 'resolver_type' )
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
from galaxy.util.submodules import submodules
|
||||
|
||||
|
||||
def plugins_dict(module, plugin_type_identifier):
|
||||
""" Walk through all classes in submodules of module and find ones labelled
|
||||
with specified plugin_type_identifier and throw in a dictionary to allow
|
||||
constructions from plugins by these types later on.
|
||||
"""
|
||||
plugin_dict = {}
|
||||
|
||||
for plugin_module in submodules( module ):
|
||||
# FIXME: this is not how one is suppose to use __all__ why did you do
|
||||
# this past John?
|
||||
for clazz in plugin_module.__all__:
|
||||
plugin_type = getattr( clazz, plugin_type_identifier, None )
|
||||
if plugin_type:
|
||||
plugin_dict[ plugin_type ] = clazz
|
||||
|
||||
return plugin_dict
|
||||
|
||||
|
||||
def load_plugins_from_element(plugins_dict, plugins_element, extra_kwds={}):
|
||||
plugins = []
|
||||
|
||||
for plugin_element in plugins_element.getchildren():
|
||||
plugin_type = plugin_element.tag
|
||||
plugin_kwds = dict( plugin_element.items() )
|
||||
plugin_kwds.update( extra_kwds )
|
||||
plugin = plugins_dict[ plugin_type ]( **plugin_kwds )
|
||||
plugins.append( plugin )
|
||||
|
||||
return plugins
|
||||
Reference in New Issue
Block a user