#!/usr/bin/env python # # File: plugins.py # # Copyright (C) 2008 Christopher R. Gabriel # # 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. # import os import os.path import imp import sys def load_plugins(pluginpath=None, nick="octofuss"): """return a list of all available plugins in the given directory""" if not pluginpath: pluginpath = os.environ.get(nick.upper() + "_PLUGINS", "plugins") # if the plugin path need to be auto-discovered # starting from a specified module #pluginpath = os.path.join(os.path.dirname(imp.find_module("octofussd")[1]), "extensions/") #pluginpath = "." pluginfiles = [fname[:-3] for fname in os.listdir(pluginpath) if fname.endswith(".py") and not fname.startswith(".") and not fname.endswith("~")] for fname in sorted(pluginfiles): oldpath = sys.path try: sys.path.append(os.path.abspath(pluginpath)) res = imp.load_source(fname, os.path.join(pluginpath, fname) + ".py") finally: sys.path = oldpath yield res