Skip to content

Instantly share code, notes, and snippets.

@rscarvalho
Created May 6, 2011 22:22
Show Gist options
  • Save rscarvalho/959907 to your computer and use it in GitHub Desktop.
Save rscarvalho/959907 to your computer and use it in GitHub Desktop.
Plugin Manager
from plugin_manager import PluginManager
app = App()
app.plugin_manager = PluginManager()
app.plugin_manager.initialize_plugins()
from os import path
from glob import iglob
import logging
class PluginManager(object):
PLUGINS_DIR = path.abspath(path.dirname(__file__))
PLUGINS = []
def initialize_plugins(self):
sys.path += PLUGINS_DIR
for fpath in iglob(path.join(PLUGINS_DIR, "*")):
if path.isfile(fpath):
if !path.endswith(".py"): continue
module_name = path.basename(fpath)[:-3]
else:
module_name = path.basename(fpath)
mod = __import__(module_name, {}, [], fromlist=[])
# Assume that the module has a class called 'Plugin'
# And it has a initializer that takes the plugin_manager as a parameter
# *convention over configuration*
#
try:
PLUGINS.append(mod.Plugin(manager))
except AttributeError:
logging.error("Plugin not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment