ocarina/src/core/manager.py

125 lines
2.8 KiB
Python

__author__="bjschuma"
__date__ ="$Dec 8, 2009 8:40:36 AM$"
import sys
from bt.message import *
from bt.file import *
from bt.signal import *
from bt import alias
#import loader
#import session
import inspect
class Manager:
def __init__(self):
write("Creating plugin manager", True)
register("scion-end",self.shutdown,1)
# Map the plugin name to a dictionary
self.enabled = dict()
self.disabled = dict()
self.restored = []
def addPlugins(self, plugins):
# Add each plugin to the disabled list
for plugin in plugins:
write("Adding plugin: "+plugin.name, 2)
self.disabled[plugin.name] = plugin
# Move plugin from old[name] to new[name]
# Return the plugin
def movePlugin(self, name, old, new):
plugin = old.pop(name)
new[name] = plugin
return plugin
# Disable a plugin
# Returns true if there was a plugin to disable, false otherwise
def disablePlugin(self,name):
if (name in self.enabled) == True:
write("Disabling plugin: "+name, True)
plugin = self.movePlugin(name, self.enabled, self.disabled)
plugin.close()
return True
return False
# Move a plugin to the enabled list
# Returns true if a plugin was enabled, false otherwise
def enablePlugin(self,name):
if (name in self.disabled) == True:
try:
write("Enabling plugin: "+name,2)
plugin = self.movePlugin(name, self.disabled, self.enabled)
plugin.open()
return True
except:
error("Error enabling: "+name, inspect.trace())
return False
# Disable the plugin, reload it, reenable it
def reloadPlugin(self,name):
# Check if plugin has been loaded
if (name in sys.modules) == False:
write("Plugin not loaded: "+name, True)
return
## Disable the plugin and pop from sys.modules
reenable = self.disablePlugin(name)
del self.disabled[name]
#reload(self.disabled[name])
reload(sys.modules[name])
p = sys.modules[name].Plugin()
self.disabled[name]=p
if reenable == True:
self.enablePlugin(name)
# Disable all plugins
def shutdown(self):
write("Shutting down manager, disabling all active plugins", 1)
keys = self.enabled.keys()
for plugin in keys:
self.disablePlugin(plugin)
def run(self,name,args=None):
name = name.strip()
# Aliases have top priority
if alias.has(name) == True:
cmd,arg = alias.get(name)
if not args==None:
arg += args
return self.run(cmd,arg)
if (name in self.enabled)==True:
try:
return self.enabled[name].start(args)
except:
error("Plugin "+name+" has caused an error. Disabling.")
self.disablePlugin(name)
else:
write("Plugin either not loaded or doesn't exist: "+name)
return None
def call(self,name,args):
disable()
result = self.run(name,args)
enable()
return result
def savesession(self,session):
path = join(session,"enabled")
file = fopen(path,'w')
for key in self.enabled.keys():
file.write(key+"\n")
file.close()