#! python: #w#VIC ai.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC ai.py module: version 0.5.1.python (BETA) # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation version 2 # of the License. http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Or access http://www.gnu.org/copyleft/gpl.html ########################################################################## #August 29, 2003 made file IQ parseable 0.5.1 #July 24, 2003 initial release 0.5 # ########################################################################## #s#system modules import string import re import sys #s#processing modules from pk import * from vic import * #s#local modules import options import state import errors #w#class AI class AI: "Alternate Interface" #s#def __init__ def __init__(self): pass #s#def getName def getName(self): return "Alternate Interface" #s#def parseRun def parseRun(self,state,ignored_vic,config,command,stdin): handled = 0 output = [] error = errors.ok if(1): args,opts = options.getopt(command,[ '-s -n:','-pk -oi -ke -iq -id -sf:','-f:','-ip -i -op -o:','-w','-u','-e','-? -help --help','-l','exits: exit:' ]) if (len(args) > 0 and string.lower(args[0]) == 'ai'): handled = 1 ### Some arguments have to be identified by extension: ### .pk .oi .ke .sf[@] .iq[@] .id[@] ### But they also have a correcponding command line switch ### So if we see a plain "filename.XX" rewrite the command line ### such that it becomes "-XX filename.XX" ### Also handle ones with an optional @ on the end ### If we get no number after the '@', remove it for extension in ['pk','oi','ke','sf','iq','id']: ext_check = re.compile("[^\\.]*\\."+extension+"$") line_check = re.compile("[^\\.]*\\."+extension+"@[0-9]*$") ### allow "name.XX@" i = 0 while (i < len(args)): if (ext_check.match(args[i])): ### Matched name.XX #print "matched "+args[i]+" on ext_check" opts.append(["-"+extension,args[i]]) del(args[i]) i=0 ### args index changed, restart changed = 1 elif (extension in ['sf','iq','id']): ### Matched name.XX@ if (line_check.match(args[i])): #print "matched "+args[i]+" on line_check" if (args[i][-1] == "@"): args[i] = args[i][0:-1] ### if ends in @ but no number, cull it opts.append(["-"+extension,args[i]]) del(args[i]) i=0 ### args index changed, restart if (len(args) == 0 or i == len(args)): break i = i + 1 #output.append("AI ARGS: "+str(args[1:])) #output.append("AI OPTS: "+str(opts)) ### Make a seperate list of just the commands used opt_commands = [] for com,arg in opts: opt_commands.append(com) if (len(opts) == 0 and len(args) > 1): ### Insert command into running VICs args[1] = string.replace(args[1],","," ") vics = [] for vic_name in string.split(args[1]): vics = vics + state.getVicNameListByRE(vic_name) command = "" for word in args[2:]: command = command + " " + word command = string.strip(command) done = 0 for vic_name in vics: vic = state.getVicByNameID(vic_name) if (vic != None): vic.addCommand(command) done = done + 1 else: output.append("VIC "+vic_name+" does not exist") if (done == 0): output.append("No matching VICs") ### -l ==> List open VICs elif ('-l' in opt_commands): if (state.vicCount() > 0): for i in range(state.vicCount()): entry = state.getVic(i).getNameID() if (state.getVic(i).inStepMode()): entry = entry + " (stepping)" if (i == state.getCurrentVicIndex()): entry = entry +" (focused)" output.append(entry) elif ('-w' in opt_commands): output.append("AI STUB ") elif ('-u' in opt_commands): output.append("AI STUB ") elif ('-e' in opt_commands): output.append("AI STUB ") elif ('exit' in opt_commands): i = options.getoptindex(opts,'exit') vics_to_exit = opts[i][1] if (vics_to_exit == ""): exiters = [".*"] system_exit = 1 else: exiters = options.splitArgs(vics_to_exit) system_exit = 0 for expression in exiters: index = 0 while (index != -1): ## RE may match more than 1 VIC, loop for them all index = state.getVicIndexByRE(expression) if (index != -1): output.append("Exiting: "+state.getVic(index).getNameID()) state.exitVic(index) if (system_exit): state.exit = 1 elif ('exits' in opt_commands): i = options.getoptindex(opts,'exits') vics_to_exits = opts[i][1] if (vics_to_exits == ""): output.append("AI STUB exitS all VICs") else: output.append("AI STUB exitS VICs"+vics_to_exits) ### -s --> Force Step mode on named VIC(s) elif ('-s' in opt_commands): if (state.vicCount() == 0): error = errors.no_vic else: i = options.getoptindex(opts,'-s') if (opts[i][1] != ''): for vic in string.split(opts[i][1]): if (string.count(vic,".") > 0): name,id = string.split(vic,".",1) elif (vic[0] in string.digits): id = vic name = "" else: name = vic id = "0" index = state.getVicIndex(name,int(id)) if (index != -1): output.append(state.vic_list[index].getNameID() + " step mode") state.vic_list[index].setStepMode(1) else: error = errors.no_vic_by_name else: for i in range(state.vicCount()): state.getVic(i).setStepMode(1) output.append(state.getVic(i).getNameID() + " step mode") ### -? -help --help --> Launch help system elif('-?' in opt_commands or '-help' in opt_commands or '--help' in opt_commands): browser = config.getVar("help.browser","links") url = config.getVar("help.ai_help_url","file://Help/ai.html") #os.spawnv(os.P_WAIT,browser,[url]) os.system(browser+" "+url) ### Create a new VIC/pk elif (len(args) <= 2): new_name = "" if ('-n' in opt_commands): name_idx = options.getoptindex(opts,'-n') new_name = opts[name_idx][1] else: new_name = 'vic' vic = Vic(new_name,str(state.getNextVicID()),state.getRoomDir()) output.append("Created '"+vic.getNameID()+"'") vic = state.addVIC(vic) #vic = state.getCurrentVIC() if ('-pk' in opt_commands): index = options.getoptindex(opts,'-pk') filename = opts[index][1] error = vic.loadCreatePKFile(filename,0) if ('-oi' in opt_commands): index = options.getoptindex(opts,'-oi') filename = opts[index][1] error = vic.setOPStartFile(filename) if ('-f' in opt_commands): index = options.getoptindex(opts,'-f') error = vic.setSFFlags(opts[index][1]) if ('-i' in opt_commands): index = options.getoptindex(opts,'-i') error = vic.setIPOptions(opts[index][1]) if ('-ip' in opt_commands): index = options.getoptindex(opts,'-ip') error = vic.setIPOptions(opts[index][1]) if ('-o' in opt_commands): index = options.getoptindex(opts,'-o') error = vic.setOPOptions(opts[index][1]) if ('-op' in opt_commands): index = options.getoptindex(opts,'-op') error = vic.setOPOptions(opts[index][1]) if ('-sf' in opt_commands): index = options.getoptindex(opts,'-sf') error = vic.runScript(opts[index][1]) if ('-iq' in opt_commands): index = options.getoptindex(opts,'-iq') error = vic.set_XX_File('IQ',opts[index][1]) if ('-id' in opt_commands): index = options.getoptindex(opts,'-id') error = vic.set_XX_File('ID',opts[index][1]) if ('-ke' in opt_commands): index = options.getoptindex(opts,'-ke') error = vic.setKEFile(opts[index][1]) vic.saveFiles() else: error = errors.unknown_command #print "OPTS: "+str(opts) #for opt,ass in opts: # print "OPTION: "+opt+" == ["+ass+"]" else: output.append("AI Syntax Error") error = errors.unknown_command return handled,output,error