comparison src/sat.tac @ 287:2720536b5a22

core: added plugin dependency management
author Goffi <goffi@goffi.org>
date Sun, 06 Feb 2011 22:41:32 +0100
parents 6a0c6d8e119d
children 76247af9917c
comparison
equal deleted inserted replaced
286:3b382fa0ac28 287:2720536b5a22
383 self._import_plugins() 383 self._import_plugins()
384 384
385 385
386 def _import_plugins(self): 386 def _import_plugins(self):
387 """Import all plugins found in plugins directory""" 387 """Import all plugins found in plugins directory"""
388 #TODO: manage dependencies
389 import sat.plugins 388 import sat.plugins
390 plugins_path = os.path.dirname(sat.plugins.__file__) 389 plugins_path = os.path.dirname(sat.plugins.__file__)
391 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename,glob (os.path.join(plugins_path,"plugin*.py")))] 390 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename,glob (os.path.join(plugins_path,"plugin*.py")))]
392 391 __plugins_to_import = {} #plugins will still have to import
393 for plug in plug_lst: 392 for plug in plug_lst:
394 plug_path = 'sat.plugins.'+plug 393 plugin_path = 'sat.plugins.'+plug
395 __import__(plug_path) 394 __import__(plugin_path)
396 mod = sys.modules[plug_path] 395 mod = sys.modules[plugin_path]
397 plug_info = mod.PLUGIN_INFO 396 plugin_info = mod.PLUGIN_INFO
398 info (_("importing plugin: %s"), plug_info['name']) 397 __plugins_to_import[plugin_info['import_name']] = (plugin_path, mod, plugin_info)
399 self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self) 398 while True:
400 if plug_info.has_key('handler') and plug_info['handler'] == 'yes': 399 self._import_plugins_from_dict(__plugins_to_import)
401 self.plugins[plug_info['import_name']].is_handler = True 400 if not __plugins_to_import:
402 else: 401 break
403 self.plugins[plug_info['import_name']].is_handler = False 402
404 #TODO: test xmppclient presence and register handler parent 403 def _import_plugins_from_dict(self, plugins_to_import, import_name=None):
404 """Recursively import and their dependencies in the right order
405 @param plugins_to_import: dict where key=import_name and values= (plugin_path, module, plugin_info)"""
406 if self.plugins.has_key(import_name):
407 debug('Plugin [%s] already imported, passing' % import_name)
408 return
409 if not import_name:
410 import_name,(plugin_path, mod, plugin_info) = plugins_to_import.popitem()
411 else:
412 plugin_path, mod, plugin_info = plugins_to_import.pop(import_name)
413 dependencies = plugin_info.setdefault("dependencies",[])
414 for dependency in dependencies:
415 if not self.plugins.has_key(dependency):
416 debug('Recursively import dependency of [%s]: [%s]' % (import_name, dependency))
417 self._import_plugins_from_dict(plugins_to_import, dependency)
418 info (_("importing plugin: %s"), plugin_info['name'])
419 self.plugins[import_name] = getattr(mod, plugin_info['main'])(self)
420 if plugin_info.has_key('handler') and plugin_info['handler'] == 'yes':
421 self.plugins[import_name].is_handler = True
422 else:
423 self.plugins[import_name].is_handler = False
424 #TODO: test xmppclient presence and register handler parent
405 425
406 def connect(self, profile_key = '@DEFAULT@'): 426 def connect(self, profile_key = '@DEFAULT@'):
407 """Connect to jabber server""" 427 """Connect to jabber server"""
408 428
409 profile = self.memory.getProfileName(profile_key) 429 profile = self.memory.getProfileName(profile_key)