comparison src/core/sat_main.py @ 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 e77948faaef3
children cbf4122baae7
comparison
equal deleted inserted replaced
924:861593a5652b 925:5c78cefd233f
170 while True: 170 while True:
171 self._import_plugins_from_dict(__plugins_to_import) 171 self._import_plugins_from_dict(__plugins_to_import)
172 if not __plugins_to_import: 172 if not __plugins_to_import:
173 break 173 break
174 174
175 def _import_plugins_from_dict(self, plugins_to_import, import_name=None): 175 def _import_plugins_from_dict(self, plugins_to_import, import_name=None, optional=False):
176 """Recursively import and their dependencies in the right order 176 """Recursively import and their dependencies in the right order
177 @param plugins_to_import: dict where key=import_name and values= (plugin_path, module, plugin_info)""" 177 @param plugins_to_import: dict where key=import_name and values= (plugin_path, module, plugin_info)
178 @param import_name: name of the plugin to import as found in PLUGIN_INFO['import_name']
179 @param optional: if False and plugin is not found, an ImportError exception is raised
180
181 """
178 if import_name in self.plugins: 182 if import_name in self.plugins:
179 debug('Plugin [%s] already imported, passing' % import_name) 183 debug('Plugin [%s] already imported, passing' % import_name)
180 return 184 return
181 if not import_name: 185 if not import_name:
182 import_name, (plugin_path, mod, plugin_info) = plugins_to_import.popitem() 186 import_name, (plugin_path, mod, plugin_info) = plugins_to_import.popitem()
183 else: 187 else:
184 if not import_name in plugins_to_import: 188 if not import_name in plugins_to_import:
189 if optional:
190 warning(_("Recommended plugin not found: %s") % import_name)
191 return
192 error(_("Dependency not found: %s") % import_name)
185 raise ImportError(_('Dependency plugin not found: [%s]') % import_name) 193 raise ImportError(_('Dependency plugin not found: [%s]') % import_name)
186 plugin_path, mod, plugin_info = plugins_to_import.pop(import_name) 194 plugin_path, mod, plugin_info = plugins_to_import.pop(import_name)
187 dependencies = plugin_info.setdefault("dependencies", []) 195 dependencies = plugin_info.setdefault("dependencies", [])
188 for dependency in dependencies: 196 recommendations = plugin_info.setdefault("recommendations", [])
189 if dependency not in self.plugins: 197 for to_import in dependencies + recommendations:
190 debug('Recursively import dependency of [%s]: [%s]' % (import_name, dependency)) 198 if to_import not in self.plugins:
191 self._import_plugins_from_dict(plugins_to_import, dependency) 199 debug('Recursively import dependency of [%s]: [%s]' % (import_name, to_import))
200 self._import_plugins_from_dict(plugins_to_import, to_import, to_import not in dependencies)
192 info(_("importing plugin: %s"), plugin_info['name']) 201 info(_("importing plugin: %s"), plugin_info['name'])
193 self.plugins[import_name] = getattr(mod, plugin_info['main'])(self) 202 self.plugins[import_name] = getattr(mod, plugin_info['main'])(self)
194 if 'handler' in plugin_info and plugin_info['handler'] == 'yes': 203 if 'handler' in plugin_info and plugin_info['handler'] == 'yes':
195 self.plugins[import_name].is_handler = True 204 self.plugins[import_name].is_handler = True
196 else: 205 else: