ocarina/src/core/ct/cmd.py

114 lines
2.0 KiB
Python

#! /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 ct.message import *
from ocarina import alias
from ocarina import plugins
from ocarina import vars
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]
try:
result = ocarina.plugins.run(cmd,args)
except:
result = None
if result == None:
return input
else:
return result
# 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()
if not var[0]=="$":
var = "$" + var
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):
for key in vars.keys():
v = "`"+key+"`"
if cmd.find(v) > -1:
new = str(vars[key])
write(key + " => " + new, 2)
cmd = cmd.replace(v, new)
return cmd
# 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 fixType(text):
if text.lower() == "false":
return False
elif text.lower() == "true":
return True
elif text.isdigit()==True:
return int(text)
return text
def run(string):
split = string.split(";")
ans = []
for cmd in split:
(var,cmd) = varCheck(cmd)
cmd = varReplace(cmd)
cmd = aliasReplace(cmd)
if var == None:
ans += [ runCmd(cmd) ]
else:
disable()
vars[var] = fixType( runCmd(cmd) )
enable()
if len(ans) == 1:
return ans[0]
return ans
def call(string):
# disable text printing
disable()
result = run(string)
# enable text printing
enable()
return result