changeset 925:5c78cefd233f

core: plugins now accepts recommendations: - "recommendations" key can be used in PLUGIN_INFO - if a plugin can't be found an ImportError is raised if it's a dependency, if it's a recommendation only a warning message is shown, and plugins importation continue
author Goffi <goffi@goffi.org>
date Sun, 23 Mar 2014 10:02:50 +0100
parents 861593a5652b
children d609581bf74a
files src/core/sat_main.py
diffstat 1 files changed, 15 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Sat Mar 22 18:47:17 2014 +0100
+++ b/src/core/sat_main.py	Sun Mar 23 10:02:50 2014 +0100
@@ -172,9 +172,13 @@
             if not __plugins_to_import:
                 break
 
-    def _import_plugins_from_dict(self, plugins_to_import, import_name=None):
+    def _import_plugins_from_dict(self, plugins_to_import, import_name=None, optional=False):
         """Recursively import and their dependencies in the right order
-        @param plugins_to_import: dict where key=import_name and values= (plugin_path, module, plugin_info)"""
+        @param plugins_to_import: dict where key=import_name and values= (plugin_path, module, plugin_info)
+        @param import_name: name of the plugin to import as found in PLUGIN_INFO['import_name']
+        @param optional: if False and plugin is not found, an ImportError exception is raised
+
+        """
         if import_name in self.plugins:
             debug('Plugin [%s] already imported, passing' % import_name)
             return
@@ -182,13 +186,18 @@
             import_name, (plugin_path, mod, plugin_info) = plugins_to_import.popitem()
         else:
             if not import_name in plugins_to_import:
+                if optional:
+                    warning(_("Recommended plugin not found: %s") % import_name)
+                    return
+                error(_("Dependency not found: %s") % import_name)
                 raise ImportError(_('Dependency plugin not found: [%s]') % import_name)
             plugin_path, mod, plugin_info = plugins_to_import.pop(import_name)
         dependencies = plugin_info.setdefault("dependencies", [])
-        for dependency in dependencies:
-            if dependency not in self.plugins:
-                debug('Recursively import dependency of [%s]: [%s]' % (import_name, dependency))
-                self._import_plugins_from_dict(plugins_to_import, dependency)
+        recommendations = plugin_info.setdefault("recommendations", [])
+        for to_import in dependencies + recommendations:
+            if to_import not in self.plugins:
+                debug('Recursively import dependency of [%s]: [%s]' % (import_name, to_import))
+                self._import_plugins_from_dict(plugins_to_import, to_import, to_import not in dependencies)
         info(_("importing plugin: %s"), plugin_info['name'])
         self.plugins[import_name] = getattr(mod, plugin_info['main'])(self)
         if 'handler' in plugin_info and plugin_info['handler'] == 'yes':