From e074a740635665c9fae783c4a4c72c078ea2003c Mon Sep 17 00:00:00 2001 From: bjschuma Date: Fri, 26 Feb 2010 00:31:35 -0500 Subject: [PATCH] Changed how commands are processed (before executing) to allow for variable assignment and replacement --- src/core/bt/cmd.py | 67 ++++++++++++++++++++++++++++----- src/core/session.py | 3 ++ src/doc/newFeatures.txt | 5 ++- src/doc/todo.txt | 2 +- src/tests/{test.py => test3.py} | 0 src/tests/test3.scion | 2 +- src/tests/test4.py | 4 ++ src/tests/test4.scion | 2 + 8 files changed, 72 insertions(+), 13 deletions(-) rename src/tests/{test.py => test3.py} (100%) create mode 100644 src/tests/test4.py create mode 100644 src/tests/test4.scion diff --git a/src/core/bt/cmd.py b/src/core/bt/cmd.py index 668d19ca..432a594b 100644 --- a/src/core/bt/cmd.py +++ b/src/core/bt/cmd.py @@ -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 diff --git a/src/core/session.py b/src/core/session.py index aaa982b5..f90e9b18 100644 --- a/src/core/session.py +++ b/src/core/session.py @@ -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 diff --git a/src/doc/newFeatures.txt b/src/doc/newFeatures.txt index 03f36ce1..8fd9a0ad 100644 --- a/src/doc/newFeatures.txt +++ b/src/doc/newFeatures.txt @@ -13,4 +13,7 @@ Scion 1.3 New Features - Began scripting 2 / 18 / 2010 -- Settings.init() \ No newline at end of file +- Settings.init() + +2/ 24 / 2010 +- Moved from using signals to using events \ No newline at end of file diff --git a/src/doc/todo.txt b/src/doc/todo.txt index d645691f..ce013fcc 100644 --- a/src/doc/todo.txt +++ b/src/doc/todo.txt @@ -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") diff --git a/src/tests/test.py b/src/tests/test3.py similarity index 100% rename from src/tests/test.py rename to src/tests/test3.py diff --git a/src/tests/test3.scion b/src/tests/test3.scion index a84b6639..899dfe11 100644 --- a/src/tests/test3.scion +++ b/src/tests/test3.scion @@ -1 +1 @@ -exec file tests/test.py +exec file tests/test3.py diff --git a/src/tests/test4.py b/src/tests/test4.py new file mode 100644 index 00000000..f3b3c3bc --- /dev/null +++ b/src/tests/test4.py @@ -0,0 +1,4 @@ +import session +from bt.message import write + +write(session.vars) diff --git a/src/tests/test4.scion b/src/tests/test4.scion new file mode 100644 index 00000000..c455ea82 --- /dev/null +++ b/src/tests/test4.scion @@ -0,0 +1,2 @@ +a = plugins list enabled +exec file tests/test4.py