Created
May 6, 2011 22:22
-
-
Save rscarvalho/959907 to your computer and use it in GitHub Desktop.
Plugin Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from plugin_manager import PluginManager | |
app = App() | |
app.plugin_manager = PluginManager() | |
app.plugin_manager.initialize_plugins() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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