# $Id$ # # Copyright 2006 sense.lab e.V. # # This file is part of the CryptoBox. # # The CryptoBox 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. # # The CryptoBox 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 the CryptoBox; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # import imp import os import logging class PluginManager: """manage available plugins""" def __init__(self, cbox, plugin_dirs=".", siteClass=None): self.cbox = cbox self.log = logging.getLogger("CryptoBox") self.site = siteClass if hasattr(plugin_dirs, "__iter__"): self.plugin_dirs = [os.path.abspath(dir) for dir in plugin_dirs] else: self.plugin_dirs = [os.path.abspath(plugin_dirs)] self.pluginList = self.__getAllPlugins() def getPlugins(self): return self.pluginList[:] def getPlugin(self, name): for p in self.pluginList[:]: if p.getName() == name: return p return None def __getAllPlugins(self): list = [] for plfile in self.__getPluginFiles(): list.append(self.__getPluginClass(os.path.basename(plfile)[:-3])) return list def __getPluginClass(self, name): for plfile in self.__getPluginFiles(): if name == os.path.basename(plfile)[:-3]: try: pl_class = getattr(imp.load_source(name, plfile), name) except AttributeError: return None return pl_class(self.cbox, os.path.dirname(plfile), self.site) else: return None def __getPluginFiles(self): result = [] if self.cbox and self.cbox.prefs["Main"]["DisabledPlugins"]: disabled = self.cbox.prefs["Main"]["DisabledPlugins"] else: disabled = [] for dir in [os.path.abspath(e) for e in self.plugin_dirs if os.access(e, os.R_OK) and os.path.isdir(e)]: for plname in [f for f in os.listdir(dir)]: if plname in disabled: if self.cbox: self.cbox.log.info("skipped plugin '%s' (disabled via config)" % plname) continue pldir = os.path.join(dir, plname) plfile = os.path.join(pldir, plname + ".py") if os.path.isfile(plfile) and os.access(plfile, os.R_OK): result.append(plfile) return result if __name__ == "__main__": x = PluginManager(None, "../plugins") for a in x.getPlugins(): if not a is None: print "Plugin: %s" % a.getName()