__author__="bjschuma" __date__ ="$Dec 8, 2009 8:40:36 AM$" import sys from bt.message import * import loader class Manager: def __init__(self): write("Creating plugin manager", True) # Map the plugin name to a dictionary self.enabled = dict() self.disabled = dict() def addPlugins(self, plugins): # Add each plugin to the disabled list for plugin in plugins: write("Adding plugin: " + str(plugin), True) self.disabled[plugin.name] = plugin # If we are adding a core plugin, activate it right away try: if plugin.type=="core": self.enablePlugin(plugin.name) except: error("Error adding plugin: "+plugin.name) # Move plugin from old[name] to new[name] # Return the plugin def movePlugin(self, name, old, new): plugin = old.pop(name) new[name] = plugin return plugin # Disable a plugin # Returns true if there was a plugin to disable, false otherwise def disablePlugin(self,name): if (name in self.enabled) == True: write("Disabling plugin: "+name, True) plugin = self.movePlugin(name, self.enabled, self.disabled) plugin.close() return True return False # Move a plugin to the enabled list # Returns true if a plugin was enabled, false otherwise def enablePlugin(self,name): if (name in self.disabled) == True: try: write("Enabling plugin: "+name,True) plugin = self.movePlugin(name, self.disabled, self.enabled) plugin.open() return True except: error("Error enabling: "+name) return False # Disable the plugin, reload it, reenable it def reloadPlugin(self,name): # Check if plugin has been loaded if (name in sys.modules) == False: write("Plugin not loaded: "+name, True) return # Disable the plugin and pop from sys.modules reenable = self.disablePlugin(name) reload(self.disabled[name]) if reenable == True: self.enablePlugin(name) # Disable all plugins def shutdown(self): write("Shutting down manager, disabling all active plugins", True) keys = self.enabled.keys() for plugin in keys: self.disablePlugin(plugin) def run(self,name,args=None): name = name.strip() if (name in self.enabled)==True: self.enabled[name].run(args) global manager manager = Manager()