Startup and shutdown now use signals to call functions
This commit is contained in:
parent
d31dd1ea8e
commit
956cfa5bee
|
@ -8,25 +8,31 @@ __date__ ="$Jan 23, 2010 2:33:21 PM$"
|
|||
|
||||
from bt.file import *
|
||||
from bt import signal
|
||||
#import session
|
||||
|
||||
|
||||
class Alias(dict):
|
||||
def __init__(self):
|
||||
dict.__init__(self)
|
||||
signal.register("scion-plugins-loaded",self.load,3)
|
||||
signal.register("scion-end",self.save,90)
|
||||
|
||||
|
||||
def has(self,key):
|
||||
return ( key in self.keys() )
|
||||
|
||||
|
||||
def save(self,path):
|
||||
file = fopen( join(path,"aliases"), 'w' )
|
||||
def save(self):
|
||||
import session
|
||||
write("Saving aliases",1)
|
||||
file = fopen( join(session.settings["session"],"aliases"), 'w' )
|
||||
sp = " "
|
||||
for key in self.keys():
|
||||
file.write("alias "+key+"="+self[key]+"\n")
|
||||
|
||||
|
||||
def load(self,path):
|
||||
path = join(path,"aliases")
|
||||
def load(self):
|
||||
import session
|
||||
path = join(session.settings["session"],"aliases")
|
||||
write("Alias file: "+path,1)
|
||||
signal.attachScript("scion-plugins-loaded", path )
|
||||
|
|
|
@ -22,7 +22,7 @@ def runCmd(input):
|
|||
if len(split) > 1:
|
||||
args = split[1]
|
||||
|
||||
# Translate a command to an alias
|
||||
# Translate an alias to a command
|
||||
if alias.has(cmd):
|
||||
new = alias[cmd]
|
||||
if args==None:
|
||||
|
@ -42,8 +42,11 @@ def run(string):
|
|||
ans = []
|
||||
for command in split:
|
||||
ans += [runCmd(command.strip())]
|
||||
if len(ans) == 1:
|
||||
return ans[0]
|
||||
return ans
|
||||
|
||||
|
||||
def call(string):
|
||||
# disable text printing
|
||||
disable()
|
||||
|
|
|
@ -5,6 +5,8 @@ __date__ ="$Dec 5, 2009 6:33:50 PM$"
|
|||
|
||||
import session
|
||||
from message import write
|
||||
from bt import signal
|
||||
from bt.file import join
|
||||
|
||||
# Set our process name to name
|
||||
def setname(name):
|
||||
|
@ -27,4 +29,11 @@ def setname(name):
|
|||
except:
|
||||
message += "failed."
|
||||
|
||||
write(message,True)
|
||||
write(message,1)
|
||||
|
||||
|
||||
def setup():
|
||||
app = session.settings["appname"]
|
||||
write("Welcome to "+app+"!")
|
||||
setname(app)
|
||||
session.settings["appdir"] = join(session.settings["user"], "."+app)
|
|
@ -10,6 +10,7 @@ from sys import argv,platform
|
|||
import os
|
||||
|
||||
from bt.message import write
|
||||
from bt import signal
|
||||
from bt.file import join
|
||||
from bt import xm
|
||||
|
||||
|
@ -17,6 +18,8 @@ from bt import xm
|
|||
class Settings(dict):
|
||||
def __init__(self):
|
||||
dict.__init__(self)
|
||||
signal.register("scion-plugins-loaded",self.load,2)
|
||||
signal.register("scion-end",self.save)
|
||||
|
||||
|
||||
def setdefaults(self):
|
||||
|
@ -41,7 +44,6 @@ class Settings(dict):
|
|||
del self['verbose']
|
||||
|
||||
|
||||
|
||||
def has(self,key):
|
||||
return (key.upper() in self)
|
||||
|
||||
|
@ -67,9 +69,9 @@ class Settings(dict):
|
|||
dict.__delitem__(self,key.upper())
|
||||
|
||||
|
||||
def load(self,path):
|
||||
def load(self):
|
||||
write("Loading saved settings",1)
|
||||
path = join(path,"settings")
|
||||
path = join(self["session"],"settings")
|
||||
xm.load(path)
|
||||
elm = xm.child()
|
||||
if elm == None:
|
||||
|
@ -83,13 +85,12 @@ class Settings(dict):
|
|||
self[key] = val
|
||||
|
||||
|
||||
def save(self,path):
|
||||
def save(self):
|
||||
write("Saving settings",1)
|
||||
path = join(path,"settings")
|
||||
path = join(self["session"],"settings")
|
||||
xm.new()
|
||||
elm = xm.element("settings")
|
||||
xm.append(elm)
|
||||
verbose = self['verbose']
|
||||
self.clean()
|
||||
for key in self.keys():
|
||||
value = self[key]
|
||||
|
|
|
@ -7,13 +7,14 @@ from bt.message import *
|
|||
from bt.file import *
|
||||
from bt.signal import *
|
||||
|
||||
|
||||
import session
|
||||
import inspect
|
||||
|
||||
class Manager:
|
||||
def __init__(self):
|
||||
write("Creating plugin manager", True)
|
||||
register("scion-end",self.shutdown,1)
|
||||
write("Creating plugin manager", 1)
|
||||
register("scion-plugins-loaded",self.startup)
|
||||
register("scion-end",self.shutdown,0)
|
||||
# Map the plugin name to a dictionary
|
||||
self.enabled = dict()
|
||||
self.disabled = dict()
|
||||
|
@ -39,7 +40,7 @@ class Manager:
|
|||
# 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)
|
||||
write("Disabling plugin: "+name, 2)
|
||||
plugin = self.movePlugin(name, self.enabled, self.disabled)
|
||||
plugin.close()
|
||||
return True
|
||||
|
@ -64,7 +65,7 @@ class Manager:
|
|||
def reloadPlugin(self,name):
|
||||
# Check if plugin has been loaded
|
||||
if (name in sys.modules) == False:
|
||||
write("Plugin not loaded: "+name, True)
|
||||
write("Plugin not loaded: "+name, 1)
|
||||
return
|
||||
## Disable the plugin and pop from sys.modules
|
||||
reenable = self.disablePlugin(name)
|
||||
|
@ -82,5 +83,22 @@ class Manager:
|
|||
def shutdown(self):
|
||||
write("Shutting down manager, disabling all active plugins", 1)
|
||||
keys = self.enabled.keys()
|
||||
save = fopen( join(session.settings["session"],"enabled"), 'w' )
|
||||
|
||||
for plugin in keys:
|
||||
self.disablePlugin(plugin)
|
||||
save.write(plugin+"\n")
|
||||
save.close()
|
||||
|
||||
|
||||
def startup(self):
|
||||
# Restore enabled plugins (new plugins are automatically disabled)
|
||||
enabled = fopen( join(session.settings["session"],"enabled") )
|
||||
if not enabled == None:
|
||||
for line in enabled:
|
||||
self.enablePlugin(line.strip())
|
||||
enabled.close()
|
||||
# Enable everything if we don't have an enabled list
|
||||
else:
|
||||
for key in self.disabled.keys():
|
||||
self.enablePlugin(key)
|
||||
|
|
|
@ -1,19 +1,9 @@
|
|||
# To change this template, choose Tools | Templates
|
||||
# and open the template in the editor.
|
||||
|
||||
__author__="bjschuma"
|
||||
__date__ ="$Dec 4, 2009 3:37:21 PM$"
|
||||
|
||||
# We need to import settings before we can use disp()
|
||||
import session
|
||||
|
||||
# The first thing we do is import write() so we can occasionally print messages
|
||||
from bt.message import write
|
||||
|
||||
# We need this next import to set the process name
|
||||
from bt.proc import setname
|
||||
from bt.signal import emit
|
||||
from bt.file import join
|
||||
|
||||
# Import the plugin loader class!
|
||||
import loader
|
||||
|
@ -29,14 +19,8 @@ def main():
|
|||
for path in session.settings.get("PLUGPATH"):
|
||||
loadPluginPath(path)
|
||||
|
||||
app = session.settings["appname"]
|
||||
write("Welcome to "+app+"!")
|
||||
setname(app)
|
||||
session.settings["appdir"] = join(session.settings["user"], "."+app)
|
||||
|
||||
emit("scion-plugins-loaded")
|
||||
emit("scion-begin")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":main()
|
||||
|
|
|
@ -12,72 +12,32 @@ from bt.alias import Alias
|
|||
from bt.file import *
|
||||
from bt.message import write
|
||||
from bt import signal
|
||||
from manager import Manager
|
||||
from bt import proc
|
||||
|
||||
global alias
|
||||
#alias = Alias()
|
||||
|
||||
from manager import Manager
|
||||
|
||||
global settings
|
||||
global manager
|
||||
|
||||
|
||||
def getSession():
|
||||
def setSession():
|
||||
write("Finding session...",2)
|
||||
path = settings["appdir"]
|
||||
mkdir(path)
|
||||
path = join(path,"session")
|
||||
mkdir(path)
|
||||
return path
|
||||
|
||||
|
||||
def restore():
|
||||
write("Restoring session",1)
|
||||
path = getSession()
|
||||
write(path,2)
|
||||
# Load settings from file
|
||||
settings.load(path)
|
||||
# Load aliases from file
|
||||
alias.load(path)
|
||||
# Restore enabled plugins (new plugins are automatically disabled)
|
||||
enabled = fopen( join(path,"enabled") )
|
||||
if not enabled == None:
|
||||
for line in enabled:
|
||||
manager.enablePlugin(line.strip())
|
||||
enabled.close()
|
||||
# Enable everything if we don't have an enabled list
|
||||
else:
|
||||
for key in manager.disabled.keys():
|
||||
manager.enablePlugin(key)
|
||||
|
||||
|
||||
def save():
|
||||
write("Saving session")
|
||||
path = getSession()
|
||||
write(path,2)
|
||||
# Find list of enabled plugins
|
||||
enabledList = manager.enabled.keys()
|
||||
# Shutdown plugins
|
||||
manager.shutdown()
|
||||
write(enabledList,2)
|
||||
# Record enabled plugins to a file
|
||||
enabled = fopen( join(path,"enabled"), 'w' )
|
||||
if not enabled == None:
|
||||
for plugin in enabledList:
|
||||
enabled.write(plugin+"\n")
|
||||
enabled.close()
|
||||
settings["session"] = path
|
||||
write("Using session: " + path,2)
|
||||
|
||||
# Save aliases and settings
|
||||
alias.save(path)
|
||||
settings.save(path)
|
||||
|
||||
|
||||
|
||||
settings = Settings()
|
||||
manager = Manager()
|
||||
alias = Alias()
|
||||
#print alias
|
||||
# We want to restore settings as early as possible
|
||||
# We want to configure settings as early as possible
|
||||
settings.setdefaults()
|
||||
|
||||
signal.register("scion-plugins-loaded",restore,0)
|
||||
signal.register("scion-end",save,0)
|
||||
manager = Manager()
|
||||
alias = Alias()
|
||||
|
||||
# Register functions to call when all plugins have been loaded
|
||||
signal.register("scion-plugins-loaded",proc.setup,0)
|
||||
signal.register("scion-plugins-loaded",setSession,0)
|
||||
|
|
|
@ -10,4 +10,7 @@ Scion 1.3 New Features
|
|||
- About plugin to give scion version information
|
||||
|
||||
2 / 17 / 2010
|
||||
- Began scripting
|
||||
- Began scripting
|
||||
|
||||
2 / 18 / 2010
|
||||
- Settings.init()
|
|
@ -6,10 +6,9 @@ Scion To-Do List
|
|||
- Scripting
|
||||
- Check during startup, register as callback if some token in first line
|
||||
- Run script if given as an input arg, then exit (2/17/2010)
|
||||
- Run script after a signal has executed
|
||||
- Run script after a signal has executed (2/20/2010)
|
||||
- Variables (dict with name->string)
|
||||
- *.scion == scripts. Allow adding to list of extensions
|
||||
- Settings.init()
|
||||
- Rename dirs
|
||||
scion/
|
||||
core/
|
||||
|
|
|
@ -27,7 +27,7 @@ class Plugin(plugin.Plugin):
|
|||
|
||||
|
||||
def open(self):
|
||||
write("Starting CLI", True)
|
||||
write("Starting CLI", 1)
|
||||
cline.history.read()
|
||||
|
||||
# Register our run function and close function
|
||||
|
@ -45,7 +45,7 @@ class Plugin(plugin.Plugin):
|
|||
def close(self):
|
||||
cline.loop.quit()
|
||||
cline.history.save()
|
||||
write("CLI has been stopped", True)
|
||||
write("CLI has been stopped", 1)
|
||||
del settings["keyinput"]
|
||||
remove("scion-begin",cline.loop.loop)
|
||||
remove("scion-end",cline.loop.quit)
|
||||
|
|
|
@ -4,9 +4,10 @@ __author__="bjschuma"
|
|||
__date__ ="$Jan 20, 2010 8:01:55 PM$"
|
||||
|
||||
|
||||
from bt.message import write, disable, enable
|
||||
from bt.message import write
|
||||
from bt import plugin
|
||||
from session import manager
|
||||
from bt import cmd
|
||||
#from session import manager
|
||||
|
||||
|
||||
class Plugin(plugin.Plugin):
|
||||
|
@ -18,10 +19,8 @@ class Plugin(plugin.Plugin):
|
|||
|
||||
def run(self, args=None):
|
||||
if args == None:
|
||||
disable()
|
||||
list = manager.run("plugins", ["list", "enabled"])
|
||||
list = cmd.call("plugins list enabled")
|
||||
join = ", "
|
||||
enable()
|
||||
write("Usage: " + self.usage)
|
||||
write("Valid commands are: ")
|
||||
write(join.join(list))
|
||||
|
|
Loading…
Reference in New Issue