Changed how commands are processed (before executing) to allow for

variable assignment and replacement
This commit is contained in:
bjschuma 2010-02-26 00:31:35 -05:00
parent 2dd819475f
commit e074a74063
8 changed files with 72 additions and 13 deletions

View File

@ -9,6 +9,7 @@ __date__ ="$Feb 20, 2010 2:19:10 PM$"
from bt.message import *
from session import alias
from session import manager
from session import vars
@ -22,26 +23,72 @@ def runCmd(input):
if len(split) > 1:
args = split[1]
# Translate an alias to a command
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())
else:
return input
# Check if we are storing in a variable
def varCheck(cmd):
split = cmd.split('=', 1)
if len(split)==2 and len(split[0].split())==1:
var = "$" + split[0].strip()
write("Using variable: "+var, 2)
return ( var, split[1].strip() )
else:
return (None, cmd)
# Replace variables in the command with their real values
def varReplace(cmd):
split = cmd.split()
out = ""
for index,word in enumerate(split):
if index > 0:
out += " "
if vars.has(word)==True:
new = str(vars[word])
write(word + " ==> " + new, 2)
out += new
else:
out += word
return out
# Replace aliases with their real values
def aliasReplace(cmd):
split = cmd.split()
out = ""
for index,word in enumerate(split):
if index > 0:
out += " "
if alias.has(word) == True:
write(word + " ==> " + alias[word], 2)
out += aliasReplace(alias[word])
else:
out += word
return out
def run(string):
split = string.split(";")
ans = []
for command in split:
ans += [runCmd(command.strip())]
for cmd in split:
(var,cmd) = varCheck(cmd)
cmd = varReplace(cmd)
cmd = aliasReplace(cmd)
result = runCmd(cmd)
if var == None:
ans += [ result ]
else:
vars[var] = result
if len(ans) == 1:
return ans[0]
return ans

View File

@ -18,10 +18,13 @@ from bt.file import *
from bt.message import write
from manager import Manager
from bt import proc
from bt import dict
global alias
global settings
global manager
global vars
vars = dict.Dict()
#global events

View File

@ -13,4 +13,7 @@ Scion 1.3 New Features
- Began scripting
2 / 18 / 2010
- Settings.init()
- Settings.init()
2/ 24 / 2010
- Moved from using signals to using events

View File

@ -24,5 +24,5 @@ Scion To-Do List
- Scripts
- Better way to list signals, aliases, and settings
- Command line options for plugins
- Command line options to disable signals and/or scripting
- Command line options to disable events and/or scripting
- Plugin sub-function help ("help plugins disable")

View File

@ -1 +1 @@
exec file tests/test.py
exec file tests/test3.py

4
src/tests/test4.py Normal file
View File

@ -0,0 +1,4 @@
import session
from bt.message import write
write(session.vars)

2
src/tests/test4.scion Normal file
View File

@ -0,0 +1,2 @@
a = plugins list enabled
exec file tests/test4.py