# Plugin Manager - Originally used in Nyana # # Copyright (C) 2006-2007 Frank Hale # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import inspect import imp import os.path class PluginManager(object): def __init__(self, app): # This object would be a global object used to pass to each plugin so they # have access to the main application. self.app = app # This is a tuple of key=value pairs which represent the plugin name and # it's associated object created at plugin load time self.managed_plugins = {} def intialize_plugins(self, plugin_dir): if(os.path.exists(plugin_dir)): if(os.path.exists(os.path.join(plugin_dir, "__init__.py"))): plugin_dir = os.path.abspath(plugin_dir) filenames = os.listdir(plugin_dir) for filename in filenames: if(filename.endswith(".py") and not filename.startswith("__init__")): obj_name = filename.strip(".py") plugin_module = imp.load_source(obj_name, os.path.join(plugin_dir, filename)) obj = getattr(plugin_module, obj_name) # Our plugin should be a class object if(inspect.isclass(obj)): if(hasattr(obj, "__init__") and hasattr(obj, "metadata") and hasattr(obj, "load") and hasattr(obj, "unload") ): members = inspect.getargspec(obj.__init__) if("editor" in members[0]): if(obj.metadata.has_key("enabled")): if(obj.metadata["enabled"] is True): #print "Plugin Manager: loading %s" % (obj.metadata["name"]) plugin = obj(self.app) plugin.load() self.managed_plugins[obj_name] = plugin else: self.print_error(filename, "missing 'app' argument in plugin __init__() method") else: self.print_error(filename, "can't find class object") def print_error(self, filename, msg): print "Plugin Manager: (%s) does not conform to plugin protocol, %s" % (filename, msg) def tear_down_plugin(self, obj_name): if self.managed_plugins.has_key[obj_name]: self.managed_plugins[obj_name].unload() del self.managed_plugins[obj_name] def tear_down_all_plugins(self): for obj_name, plugin in self.managed_plugins.items(): #print "Plugin Manager: unloading %s" % (plugin.metadata["name"]) plugin.unload() del obj_name