#! python: #w#VIC main.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC main.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 sys import string import os #s#local modules from options import * from terminal import * from configuration import * from files import * from state import * from redirect import * import errors #s#Command Modules from ai import * from pk import * from oi import * from sf import * from sh import * #s#ID variables ### Used to make unique ID numbers global_counter = 0 global_command_history = [] global_dir_seperator = "" global_dir_home = "" global_dir_room = "" #s#def readConfiguration ### ### Read the given config filename (or 'VIC.RC' of none given) ### and setup the few global variables we use ### def readConfiguration(filename='VIC.RC',ai_name='AI.RC'): global global_dir_seperator,global_dir_home,global_dir_room try: config = Configuration(filename,ai_name) global_dir_seperator = config.getVar("dirs.seperator","/") global_dir_home = config.getVar("dirs.home","Home") global_dir_room = config.getVar("dirs.room","Room") except: print "Can't parse '"+filename+"' - abort" sys.exit(1) return config #s#def makeCheckHomeRooms ### ### Check the 'Home' directory from the config, also ### check the 'Rooms' dir. if either does not exist ### then create it ### def makeCheckHomeRooms(config): error = 0 home_dir = config.getVar("dirs.home","") if (home_dir == ""): print "Error: 'home' is not set" error = error + 1 rooms_dir = config.getVar("dirs.rooms","") if (rooms_dir == ""): print "Error: 'rooms' is not set" error = error + 1 ### Game over. if (error > 0): print "Cannot continue" sys.exit(1) ### Make Home if (not isDir(home_dir)): print "'"+home_dir+"' doesn't exist already... creating" if (not makeDir(home_dir)): print "Failed to create directory: '"+home_dir+"'" sys.exit(1) ### CD to Home os.chdir(home_dir) if (home_dir[-1] != os.sep): home_dir = home_dir + os.sep #rooms_dir = home_dir + rooms_dir if (not isDir(rooms_dir)): print "'"+rooms_dir+"' doesn't exist already... creating" if (not makeDir(rooms_dir)): print "Failed to create directory: '"+rooms_dir+"'" sys.exit(1) if (rooms_dir[-1] != os.sep): rooms_dir = rooms_dir + os.sep return home_dir,rooms_dir #s#def commandPrompt ### ### Draw the command prompt to the screen, remembering the history ### (which isn't used on simple 'stdin' style command prompts) ### Also allow a longer commnad prompt by ending in a backslash ### In which case a new line is prompted for, and the command ### line parts concatenated. ### def commandPrompt(stdscr,normal_prompt,more_prompt,idle_exec_list=None): command = "" done = 0 prompt = normal_prompt while (not done): command = command + commandLine(stdscr,global_command_history,prompt,"",idle_exec_list) if (len(command) > 0): if (command[-1] != "\\"): done = 1 else: command = command[:-1] prompt = more_prompt global_command_history.append(command) else: done = 1 ### just pushed [Enter] for i in range(len(global_command_history)): ### Keep empty lines out of the history if (global_command_history[i] == ""): del(global_command_history[i]) i=0 return command ### #w#MAIN ### #s#Read and promulgate config information config = readConfiguration() home_dir,rooms_dir = makeCheckHomeRooms(config) os.chdir(home_dir) prompt_vic_more = "...> " next_ai_id = config.getNextAI() #s#Load command implementations global_command_modules = [] ai = AI() pk = PK() oi = OI() sf = SF() sh = SH() global_command_modules.append(ai) global_command_modules.append(pk) global_command_modules.append(oi) global_command_modules.append(sf) global_command_modules.append(sh) ### Passes any other commands to the shell, e.g.: ls ### #s#Command Loop ### stdscr = cursesOn() state = State(home_dir,rooms_dir,next_ai_id) command = None while not (state.shouldExit() or command in ["quit","exit"]): command = None command_ready = 0 ### Do we have a current VIC? vic = state.getCurrentVIC() engine_args = StateEnginePack(state,stdscr,config,global_command_modules) ### May have a command from a script, but if not - read one from stdin if (vic != None and vic.inStepMode()): ### In step mode, prompt with command, and step command = vic.getNextCommand() if (command == None): vic.setStepMode(0) ### ran out of commands to step through, switch back to normal else: ### prompt for the step, and if they type 'break' abort step_input = commandPrompt(stdscr,vic.getNameID()+" step: ("+command+") ","(...)",[state.processingEngine,engine_args]) if (string.strip(step_input) == 'break'): vic.leaveScriptMode() vic.rewindScriptCommand() else: ### Must execute command here, otherwise a script command gets ### passed in as a 'typed' command #state.executeCommand(state,vic,stdscr,config,command,global_command_modules) state.executeCommand(engine_args,vic,command) ### Command is handled already, so erase it command = '' else: ### If there is a need to input a commmand, then do it now vic_prompt = "AI> " if (vic != None): vic_prompt = vic.getNameID()+"> " if (vic == None or vic.shouldGetInput()): command = commandPrompt(stdscr,vic_prompt,prompt_vic_more,[state.processingEngine,engine_args]) #writeToConsole(stdscr,"COMMAND: "+command) if (command != '' and command != None and not state.routeCommand(command)): ### No focused vic, execute command without one #state.executeCommand(state,None,stdscr,config,command,global_command_modules) state.executeCommand(engine_args,None,command) else: ### Execute commands in any VIC with commands pending state.processingEngine(engine_args) #state,stdscr,config,global_command_modules) #s#Clean up before exit config.setNextAI(state.getNextVicID()) ### save the .id number cursesOff(stdscr)