Created
June 7, 2011 21:25
-
-
Save FurryHead/1013206 to your computer and use it in GitHub Desktop.
New plugin loader
This file contains hidden or 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
import os, inspect | |
pList = { } | |
mList = defaultdict(list) | |
def plugin(cls): | |
#Using inspect, figure out what file is calling this function (using @plugin) | |
clsFile = inspect.stack()[1][1] | |
#Split the path, because it will be in the form "plugins" + os.sep + "*.py" | |
modFile = clsFile.split(os.sep)[1] | |
#Append the class name to the list indexed by module file name | |
mList[modFile].append(cls.__name__) | |
#Set class (plugin) name to the class (plugin) reference | |
pList[cls.__name__] = cls | |
#return cls, since decorators must return the "new" type | |
return cls | |
def refresh(pluginName = None): | |
if pluginName is None: | |
_files = [os.sep.join("plugins", f) for f in os.listdir("plugins") if f != "__init__.py" and not f.endswith(".pyc") and not os.path.isdir(os.path.join("plugins",f))] | |
for f in _files: | |
env = {"plugin":plugin} | |
execfile(f, {}, env) | |
else: | |
for f,classes in mList: | |
if pluginName in classes: | |
env = {"plugin":plugin} | |
execfile(os.sep.join("plugins", f), {}, env) | |
def getPluginClass(name): | |
try: | |
return pList[name] | |
except KeyError: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment