comparison planemo/lib/python3.7/site-packages/galaxy/util/plugin_config.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 import collections
2
3 try:
4 import yaml
5 except ImportError:
6 yaml = None
7
8 from galaxy.util import parse_xml
9 from galaxy.util.submodules import import_submodules
10
11
12 PluginConfigSource = collections.namedtuple('PluginConfigSource', ['type', 'source'])
13
14
15 def plugins_dict(module, plugin_type_identifier):
16 """ Walk through all classes in submodules of module and find ones labelled
17 with specified plugin_type_identifier and throw in a dictionary to allow
18 constructions from plugins by these types later on.
19 """
20 plugin_dict = {}
21
22 for plugin_module in import_submodules(module, ordered=True):
23 # FIXME: this is not how one is suppose to use __all__ why did you do
24 # this past John?
25 for clazz in getattr(plugin_module, "__all__", []):
26 try:
27 clazz = getattr(plugin_module, clazz)
28 except TypeError:
29 clazz = clazz
30 plugin_type = getattr(clazz, plugin_type_identifier, None)
31 if plugin_type:
32 plugin_dict[plugin_type] = clazz
33
34 return plugin_dict
35
36
37 def load_plugins(plugins_dict, plugin_source, extra_kwds=None, plugin_type_keys=('type',)):
38 if extra_kwds is None:
39 extra_kwds = {}
40 if plugin_source.type == "xml":
41 return __load_plugins_from_element(plugins_dict, plugin_source.source, extra_kwds)
42 else:
43 return __load_plugins_from_dicts(plugins_dict, plugin_source.source, extra_kwds, plugin_type_keys=plugin_type_keys)
44
45
46 def __load_plugins_from_element(plugins_dict, plugins_element, extra_kwds):
47 plugins = []
48
49 for plugin_element in plugins_element:
50 plugin_type = plugin_element.tag
51 plugin_kwds = dict(plugin_element.items())
52 plugin_kwds.update(extra_kwds)
53 try:
54 plugin_klazz = plugins_dict[plugin_type]
55 except KeyError:
56 template = "Failed to find plugin of type [%s] in available plugin types %s"
57 message = template % (plugin_type, str(plugins_dict.keys()))
58 raise Exception(message)
59
60 plugin = plugin_klazz(**plugin_kwds)
61 plugins.append(plugin)
62
63 return plugins
64
65
66 def __load_plugins_from_dicts(plugins_dict, configs, extra_kwds, plugin_type_keys):
67 plugins = []
68
69 for config in configs:
70 plugin_type = None
71 for plugin_type_key in plugin_type_keys:
72 if plugin_type_key in config:
73 plugin_type = config[plugin_type_key]
74 break
75 assert plugin_type is not None, "Could not determine plugin type for [%s]" % config
76 plugin_kwds = config
77 plugin_kwds.update(extra_kwds)
78 plugin = plugins_dict[plugin_type](**plugin_kwds)
79 plugins.append(plugin)
80
81 return plugins
82
83
84 def plugin_source_from_path(path):
85 if path.endswith(".yaml") or path.endswith(".yml") or path.endswith(".yaml.sample") or path.endswith(".yml.sample"):
86 return PluginConfigSource('dict', __read_yaml(path))
87 else:
88 return PluginConfigSource('xml', parse_xml(path, remove_comments=True).getroot())
89
90
91 def __read_yaml(path):
92 if yaml is None:
93 raise ImportError("Attempting to read YAML configuration file - but PyYAML dependency unavailable.")
94
95 with open(path, "rb") as f:
96 return yaml.safe_load(f)