Created
July 8, 2011 11:53
-
-
Save averrin/1071676 to your computer and use it in GitHub Desktop.
WinterPlugins
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
class WinterPM(object): | |
""""" | |
Plugin manager | |
""""" | |
def activate(self, plugin): | |
""""" | |
Activate plugin | |
""""" | |
try: | |
if plugin.activate(): | |
plugin.state = 'Activated' | |
if not plugin in self.plugins: | |
self.plugins.append(plugin) | |
except Exception as e: | |
plugin.active = False | |
plugin.state = e | |
self.api.error(e) | |
def deactivate(self, plugin): | |
""""" | |
Deactivate plugin | |
""""" | |
if plugin.state == 'Activated': | |
plugin.deactivate() | |
self.plugins.remove(plugin) | |
plugin.state = 'Deactivated' | |
def findModules(self): | |
""""" | |
Search plugin files | |
http://wiki.python.org/moin/ModulesAsPlugins | |
""""" | |
modules = set() | |
for folder in os.listdir(CWD + 'plugins'): | |
for filename in os.listdir(CWD + 'plugins/' + folder): | |
module = None | |
if filename.endswith(".py"): | |
module = filename[:-3] | |
elif filename.endswith(".pyc"): | |
module = filename[:-4] | |
if module is not None: | |
modules.add(module) | |
return list(modules) | |
def loadModule(self, name, path="plugins/"): | |
""" | |
Return a named module found in a given path. | |
http://wiki.python.org/moin/ModulesAsPlugins | |
""" | |
(file, pathname, description) = imp.find_module(name, [CWD + path + name]) | |
return imp.load_module(name, file, pathname, description) | |
def processPlugin(self, module): | |
""""" | |
Create plugin instance from module | |
""""" | |
for obj in module.__dict__.values(): | |
try: | |
if issubclass(obj, WinterPlugin) and obj is not WinterPlugin: | |
plugin = obj() | |
plugin.name = module.__name__ | |
try: | |
plugin.onLoad() | |
return plugin | |
except Exception as e: | |
return | |
except Exception as e: | |
# print obj | |
# raise e | |
pass | |
def activateAll(self): | |
for plugin in self.plugins: | |
if plugin.name in self.config.plugins.active: | |
try: | |
plugin.activate() | |
plugin.active = True | |
plugin.state = 'Activated' | |
except Exception as e: | |
plugin.active = False | |
plugin.state = e | |
self.api.error(e) | |
else: | |
plugin.state = 'Deactivated' | |
plugin.active = False | |
def __init__(self, config): | |
self.config = config | |
self.api = API() | |
self.modules = [self.loadModule(name) for name in self.findModules()] | |
self.plugins = [self.processPlugin(module) for module in self.modules] | |
class WinterPlugin(WinterObject): | |
""""" | |
Base plugin class | |
""""" | |
def __init__(self): | |
self.api = API() | |
WinterObject.__init__(self) | |
WinterObject.__refs__[WinterPlugin].append(weakref.ref(self)) | |
def onLoad(self): | |
""""" | |
Some base actions after create instance | |
""""" | |
cfgfile = file(CWD + 'plugins/%s/plugin.cfg' % self.name) | |
self.config = Config(cfgfile) | |
def activate(self): | |
""""" | |
Overload...able method for activate | |
""""" | |
self.active = True | |
return True | |
def deactivate(self): | |
""""" | |
Overload...able method for activate | |
""""" | |
self.active = False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment