diff options
author | Jan Pokorný <jpokorny@redhat.com> | 2014-09-09 21:02:41 +0200 |
---|---|---|
committer | Jan Pokorný <jpokorny@redhat.com> | 2014-09-09 21:12:46 +0200 |
commit | 25268e5d0643a5d531804370c35b2777b1354f39 (patch) | |
tree | f40df3641a1f277ca88c923288e59fd14c7e11d6 /plugin_registry.py | |
parent | 9114f50df29eadeef92f690795c8ad784777e764 (diff) | |
download | clufter-25268e5d0643a5d531804370c35b2777b1354f39.tar.gz clufter-25268e5d0643a5d531804370c35b2777b1354f39.tar.xz clufter-25268e5d0643a5d531804370c35b2777b1354f39.zip |
plugin_registry: optimization + id enforcement (for tests)
When plugin in question is not tracked yet, but is found in the
dedicated namespace module (as per module:symbol convention),
it is sucked from here rather than applying whole "probe" cycle
resulting in duplicated classes (which should rather be treated
as singletons, anyway).
Possible thanks to 5ed999651ec89bfbc1ab1e8283b40ae2476a02da
that introduced that convention (oh well, which should rather
be enforced programatically).
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'plugin_registry.py')
-rw-r--r-- | plugin_registry.py | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/plugin_registry.py b/plugin_registry.py index 8a66e8f..1149a19 100644 --- a/plugin_registry.py +++ b/plugin_registry.py @@ -88,23 +88,28 @@ class PluginRegistry(type): def probe(registry, name, bases, attrs=None): """Meta-magic to register plugin""" assert '-' not in name, "name cannot contain a dash" - name = cli_decor(name) + dname = cli_decor(name) attrs = attrs or {} try: - ret = registry._plugins[name] + ret = registry._plugins[dname] log.info("Probe `{0}' plugin under `{1}' registry: already tracked" - .format(name, registry.registry)) + .format(dname, registry.registry)) except KeyError: log.debug("Probe `{0}' plugin under `{1}' registry: yet untracked" - .format(name, registry.registry)) - ret = bases if not tuplist(bases) else \ - super(PluginRegistry, registry).__new__(registry, name, - bases, attrs) + .format(dname, registry.registry)) + namespaced = registry.namespaced(dname.split('-', 1)[0]) + if namespaced in modules and hasattr(modules[namespaced], name): + # XXX can be used even for "preload" + ret = getattr(modules[namespaced], name) + else: # bases arg of type has to be tuplist, final plugin otherwise + ret = bases if not tuplist(bases) else \ + super(PluginRegistry, registry).__new__(registry, dname, + bases, attrs) # XXX init plugin here? - registry._plugins[name] = ret + registry._plugins[dname] = ret finally: if registry._path_context is not None: - registry._path_mapping[registry._path_context].add(name) + registry._path_mapping[registry._path_context].add(dname) return ret |