172 lines
3.7 KiB
Python
172 lines
3.7 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)
|
|
# 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, True)
|
|
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,True)
|
|
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):
|
|
emit("scion-end")
|
|
write("Shutting down manager, disabling all active plugins", True)
|
|
session = self.findsession()
|
|
self.savesession(session)
|
|
keys = self.enabled.keys()
|
|
for plugin in keys:
|
|
self.disablePlugin(plugin)
|
|
settings.save(session)
|
|
|
|
|
|
def startup(self):
|
|
if len(self.restored) == 0:
|
|
for key in self.disabled.keys():
|
|
if self.disabled[key].type == "core":
|
|
self.enablePlugin(key)
|
|
return
|
|
|
|
for name in self.restored:
|
|
self.enablePlugin(name)
|
|
|
|
|
|
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 findsession(self):
|
|
# path = session.session.settings.get("user")
|
|
# if path == expandPath("~"):
|
|
# #path = join(path,".scion")
|
|
# path = join(path,"."+settings.get("appname"))
|
|
# mkdir(path)
|
|
# path = join(path,"session")
|
|
# mkdir(path)
|
|
# return path
|
|
|
|
|
|
def savesession(self,session):
|
|
path = join(session,"enabled")
|
|
file = fopen(path,'w')
|
|
for key in self.enabled.keys():
|
|
file.write(key+"\n")
|
|
file.close()
|
|
|
|
|
|
def restoresession(self):
|
|
path = session.getSession()
|
|
session.settings.load(path)
|
|
path = join(path,"enabled")
|
|
file = fopen(path)
|
|
self.restored = []
|
|
if file==None:
|
|
return
|
|
for line in file:
|
|
self.restored+=[line.strip()]
|
|
|
|
|
|
|
|
global manager
|
|
manager = Manager()
|
|
|
|
|
|
def run(name,args=None):
|
|
global manager
|
|
return manager.run(name,args)
|
|
|
|
|
|
def call(name,args=None):
|
|
global manager
|
|
disable()
|
|
result = manager.run(name,args)
|
|
enable()
|
|
return result |