From 1c0721f179c9b2a0c0d8d15dbb940edbc7b5e521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 12 Sep 2014 11:21:39 +0200 Subject: [PATCH] Introduced __plugin_init__ method called after all plugins have been discovered Plugins may be able to use this method for final setups before the implementations and hooks are evaluated, e.g. for adjusting their offered functionality based on what other plugins are available. --- src/octoprint/plugin/core.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/octoprint/plugin/core.py b/src/octoprint/plugin/core.py index 1a4eae0e..627e9eee 100644 --- a/src/octoprint/plugin/core.py +++ b/src/octoprint/plugin/core.py @@ -28,6 +28,8 @@ class PluginInfo(object): attr_check = '__plugin_check__' + attr_init = '__plugin_init__' + def __init__(self, key, location, instance, version=None): self.key = key self.location = location @@ -82,6 +84,10 @@ class PluginInfo(object): def check(self): return self._get_instance_attribute(self.__class__.attr_check, default=lambda: True) + @property + def init(self): + return self._get_instance_attribute(self.__class__.attr_init, default=lambda: True) + def _get_instance_attribute(self, attr, default=None): if not hasattr(self.instance, attr): return default @@ -205,8 +211,14 @@ class PluginManager(object): self.plugins = self._find_plugins() for name, plugin in self.plugins.items(): + # initialize the plugin + plugin.init() + + # evaluate registered hooks for hook, callback in plugin.hooks.items(): self.plugin_hooks[hook].append((name, callback)) + + # evaluate registered implementations for plugin_type in self.plugin_types: implementations = plugin.get_implementations(plugin_type) self.plugin_implementations[plugin_type] += ( (name, implementation) for implementation in implementations ) @@ -240,13 +252,13 @@ class PluginManager(object): return set() return {impl[0]: impl[1] for impl in result} - def get_helpers(self, name, helpers=None): + def get_helpers(self, name, *helpers): if not name in self.plugins: return None plugin = self.plugins[name] all_helpers = plugin.helpers - if helpers: + if len(helpers): return dict((k, v) for (k, v) in all_helpers.items() if k in helpers) else: return all_helpers