New way to call commands. Can attach scripts to signals

This commit is contained in:
bjschuma 2010-02-20 15:37:09 -05:00
parent be07a50c20
commit b208a55cf5
12 changed files with 165 additions and 149 deletions

View File

@ -1,5 +1,5 @@
# This is the base tools package
# It contains various tools needed by the base layer of ocarina2
__all__ = ["alias", "file", "map", "message", "needle", "plugin", "proc",
__all__ = ["alias", "cmd", "file", "map", "message", "needle", "plugin", "proc",
"scripting", "settings", "signal", "sql", "xm"]

View File

@ -6,27 +6,27 @@
__author__="bjschuma"
__date__ ="$Jan 23, 2010 2:33:21 PM$"
global aliases
aliases = dict()
def register(key,value):
aliases[key] = value
from bt.file import *
from bt import signal
def unregister(key):
del aliases[key]
class Alias(dict):
def __init__(self):
dict.__init__(self)
def get(key):
vals = aliases[key]
if len(vals) > 1:
return vals[0], vals[1:]
return vals[0], None
def has(self,key):
return ( key in self.keys() )
def has(key):
return key in aliases.keys()
def save(self,path):
file = fopen( join(path,"aliases"), 'w' )
sp = " "
for key in self.keys():
file.write("alias "+key+"="+self[key]+"\n")
#register("lsmod",["plugins","list"])
def load(self,path):
path = join(path,"aliases")
write("Alias file: "+path,1)
signal.attachScript("scion-plugins-loaded", path )

54
src/core/bt/cmd.py Normal file
View File

@ -0,0 +1,54 @@
#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Feb 20, 2010 2:19:10 PM$"
from bt.message import *
from session import alias
from session import manager
def runCmd(input):
write("Running command: "+input,2)
# Find command
split = input.split(' ',1)
cmd = split[0]
args=None
if len(split) > 1:
args = split[1]
# Translate a command to an alias
if alias.has(cmd):
new = alias[cmd]
if args==None:
return runCmd(new)
else:
return runCmd(new+" "+args)
if (cmd in manager.enabled) == True:
if args == None:
return manager.enabled[cmd].start()
else:
return manager.enabled[cmd].start(args.split())
def run(string):
split = string.split(";")
ans = []
for command in split:
ans += [runCmd(command.strip())]
return ans
def call(string):
# disable text printing
disable()
result = run(string)
# enable text printing
enable()
return result

View File

@ -10,10 +10,11 @@ __date__ ="$Feb 16, 2010 11:56:00 PM$"
global scripts
scripts = []
from bt.message import write
#from bt.message import write
from bt.signal import *
from bt import file
import manager
from bt import cmd
#import manager
def add(script):
@ -24,22 +25,15 @@ def add(script):
def runScript(script):
f = file.fopen(script)
for line in f:
write(line.strip(),True)
split = line.split(';')
for word in split:
cmd = word.strip().split()
if len(cmd)>1:
manager.run(cmd[0],cmd[1:])
else:
manager.run(line)
#write(line.strip())
cmd.run(line)
f.close()
def runScripts():
#write(scripts)
for script in scripts:
runScript(script)
manager.run("exit")
cmd.run("exit")
def checkForScripts():

View File

@ -32,6 +32,15 @@ class Settings(dict):
write(self,3)
def clean(self):
del self['appdir']
del self['appname']
del self['arch']
del self['plugpath']
del self['user']
del self['verbose']
def has(self,key):
return (key.upper() in self)
@ -72,4 +81,23 @@ class Settings(dict):
val = xm.value( xm.child(node) )
write(" ( "+key + ", " + str(val) + " )",2)
self[key] = val
pass
def save(self,path):
write("Saving settings",1)
path = join(path,"settings")
xm.new()
elm = xm.element("settings")
xm.append(elm)
verbose = self['verbose']
self.clean()
for key in self.keys():
value = self[key]
if value == None:
continue
e = xm.element(key)
t = xm.text( value )
xm.append(t,e)
xm.append(e,elm)
xm.write(path)

View File

@ -7,9 +7,18 @@ __author__="bjschuma"
__date__ ="$Jan 20, 2010 12:13:21 AM$"
from map import Map
global signals, status
global signals, status, scripts
signals = Map()
status = dict()
scripts = dict()
def attachScript(signal,script):
global scripts
if (signal in scripts) == False:
scripts[signal] = [script]
else:
scripts[signal] += [script]
def register(signal,func,priority=100):
@ -46,3 +55,8 @@ def emit(signal,args=None):
stop(signal)
if (signal in scripts) == True:
import scripting
for script in scripts[signal]:
scripting.runScript(script)

View File

@ -26,7 +26,7 @@ def element(name):
def text(name):
global document
return document.createTextNode(name)
return document.createTextNode( str(name) )
def append(child,root=None):

View File

@ -6,10 +6,7 @@ import sys
from bt.message import *
from bt.file import *
from bt.signal import *
from bt import alias
#import loader
#import session
import inspect
@ -87,39 +84,3 @@ class Manager:
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()

View File

@ -8,9 +8,14 @@ __date__ ="$Feb 17, 2010 9:18:50 PM$"
from bt.settings import Settings
from bt.alias import Alias
from bt.file import *
from bt.message import write
from bt import signal
global alias
#alias = Alias()
from manager import Manager
global settings
@ -29,20 +34,44 @@ 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()
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()
# 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
settings.setdefaults()

View File

@ -2,18 +2,6 @@ import os.path
__author__="bjschuma"
__date__ ="$Dec 4, 2009 4:04:24 PM$"
#import bt.message.disp
import os
import sys
import getopt
# Create a dictionary to hold the settings
global settings
settings = dict()
# Parse the user input
def parseInput():
@ -36,32 +24,3 @@ def parseInput():
error(e.msg)
return
def clean():
delete("appdir")
delete("appname")
delete("arch")
delete("args")
delete("plugpath")
delete("user")
delete("verbose")
def save(path):
write("Saving settings",True)
clean()
global settings
path = join(path,"settings")
xm.new()
elm = xm.element("settings")
xm.append(elm)
a = settings.keys()
for key in settings.keys():
value = get(key)
if value == None:
continue
e = xm.element(key)
t = xm.text(str(value))
xm.append(t,e)
xm.append(e,elm)
xm.write(path)

View File

@ -5,10 +5,11 @@ __date__ ="$Jan 23, 2010 2:46:07 PM$"
from bt import plugin
from bt import alias
#from bt import alias
from bt.message import write
from bt.file import *
from session import settings
from session import alias
class Plugin(plugin.Plugin):
@ -18,40 +19,22 @@ class Plugin(plugin.Plugin):
self.usage = "alias [list, new old]"
def getfile(self,flags='r'):
#path = join( settings.get("user"), "."+settings.get("appname") )
path = join(settings["appdir"], "aliases")
return fopen(path,flags)
def open(self):
file = self.getfile()
if file == None:
return
for line in file:
self.run(line.split())
def close(self):
file = self.getfile('w')
join = " "
for key in alias.aliases:
line = key + " " + join.join(alias.aliases[key] )
file.write(line + "\n")
file.close()
def run(self, args=None):
if args==None or len(args)<1:
write("Usage: "+self.usage)
return
if len(args)==1 and args[0]=="list":
for key in alias.aliases.keys():
write(key + " = " + str(alias.aliases[key]))
for key in alias.keys():
write(key + " = " + str(alias[key]))
return
alias.register(args[0],args[1:])
sp = ' '
line = sp.join(args)
(key,cmd) = line.split('=',1)
key = key.split()[0]
alias[key] = cmd
#alias.register(args[0],args[1:])

View File

@ -6,8 +6,9 @@
__author__="bjschuma"
__date__ ="$Dec 19, 2009 11:38:46 PM$"
from session import manager
#from session import manager
from session import settings
from bt import cmd
#from bt.message import write
import history
@ -20,14 +21,7 @@ def run(input):
if len(input) == 0:
return prompt
split = input.split(';')
for line in split:
cmd = line.strip().split(' ')
if len(cmd)>1:
manager.run(cmd[0],cmd[1:])
else:
manager.run(line)
cmd.run(input)
history.insert(input)