#! python: #w#VIC pk.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC pk.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 os import os.path #s#local modules import options import files import state import errors #w#class PK class PK: "Place Keeper" #s#def parseRun def parseRun(self,state,vic,config,command,stdin): handled = 0 output = [] error = errors.ok args = options.splitArgs(command) if (len(args) > 1 and string.lower(args[0]) == 'pk'): handled = 1 del(args[0]) ### -? -help --help --> Launch help system if (args[0] == '-?' or args[0] == '-help' or args[0] == '--help'): browser = config.getVar("help.browser","links") url = config.getVar("help.pk_help_url","file://Help/pk.html") os.system(browser+" "+url) return 1,[],errors.ok #vic = state.getCurrentVIC() if (vic == None): error = errors.no_vic ### -s [opt path:][opt. filename] elif (args[0] == '-s' and len(args) <= 2): if (len(args) == 1): vic.savePK() else: filename = args[1] ### Save function will work out all the path problems ### since the path must be stored in the .pk file if (not vic.savePK(filename)): error = errors.save_failed ### -rn [opt. filename] elif (args[0] == '-rn' and len(args) <= 2): ### has optional argument, must do some fancy checking if (len(args) == 1): pk_filename = vic.getPKFilename() else: pk_filename = vic.getRoom()+args[1] if (files.isPlainFilename(pk_filename)): pk_filename = vic.getRoomDir()+args[1] new_file = files.fileVersion(pk_filename) #print "GOT ["+new_file+"]" if (new_file == ""): error = errors.rename_no_ext ### ran out of extensions?! new_file = files.fileName(new_file) if (new_file != ""): vic.setPKFilename(new_file) else: error = errors.rename_failed ### -del filename elif (args[0] == '-del'): if (len(args) != 2): output.append("Expecting a single filename argument") else: filename = vic.getRoom()+args[1] if (files.isFile(filename)): files.delFile(filename) else: output.append("'"+ filename + "' does not exist") error = errors.delete_failed ### The field manipulation commands elif (string.upper(args[0]) in ['AI','PK','OI','SF','IP','OP','IQ','ID','KE']): args[0] = string.upper(args[0]) if (len(args) == 1 and args[0] != 'AI'): ### Switch field 1 amd field 2 tag = args[0] vic.switchCurrentAndLast(tag) if (tag in ['PK','OI']): vic.ifCurrentBlankSetDefault(tag,'') if (tag == 'PK'): vic.savePK() else: vic.saveOI() elif (tag in ['IP','OP']): vic.ifCurrentBlankSetDefault(tag,[ '','','','','' ]) elif (tag in ['SF']): if (vic.currentIsBlank(tag,'')): vic.setSFFlags(vic.getDefaultForTag('SF',0)) ### Default for SF elif (tag in ['IQ','ID']): if (vic.currentIsBlank(tag)): output.append("Warning: field empty, but default empty too") ### KE tag has no '' check elif (args[1] == '-r' and len(args) == 2): if (args[0] == 'KE'): output.append(vic.getKELine1()) else: output.append(vic.getPKFileLine(args[0])) elif (args[1] == '-c' and len(args) == 2): vic.setPKFileLineDefault(args[0]) #output.append(vic.getPKFileLine(args[0])) elif (args[0] == 'KE' and args[1] == 'x' and len(args) == 2): vic.swapKELines() elif (args[0] == 'PK' and args[1] == '-c' and len(args) == 2): vic.savePK() error = vic.doPKPK_MinusC() elif (args[0] != 'AI' and args[1] == '-c' and len(args) == 3): if (args[0] == 'PK'): try: pk_files = string.split(args[2],';') for filename in pk_files: if (filename != ''): error = vic.PKLoadOrCreate(filename) if (error == errors.ok): output.append(filename+": ok") else: error = errors.bad_pk_file except: error = errors.incorrect_args else: if (args[0] in [ 'AI','PK','OI','IP','OP','KE'] and string.count(args[2],';') != 2): error = errors.incorrect_args elif (args[0] in ['SF','IQ','ID'] and string.count(args[2],';') != 3): error = errors.incorrect_args else: vic.setTag(args[0],args[2]) elif (args[1] in ['-c1','-c2','-c3','-c4'] and (len(args) == 2 or len(args) == 3)): field_number = int(args[1].split("c",1)[1]) - 1 ### Convert -c into index if (len(args) == 2): ### -cX no argument -> (mostly) clear the field if (args[1] == '-c1'): error = vic.doPK_XX_MinusC1(args[0]) elif (args[1] == '-c2'): error = vic.doPK_XX_MinusC2(args[0]) ### clear field #2 (last/alt) elif (args[1] == '-c3'): if (args[0] == 'AI'): if (not files.chdirOK(vic.getRoom())): error = errors.chdir_failed else: error = vic.doPK_XX_MinusC3(args[0]) ### clear field #3 (default) elif (args[1] == '-c4'): error = vic.doPK_XX_MinusC4(args[0]) else: ### argument to -cX, set the field if (args[1] in ['-c1','-c2'] and args[0] != 'AI'): error = vic.setPKFieldUserValue(args[0],args[2],field_number) elif (args[1] == '-c3'): error = vic.setPKFieldUserValue(args[0],args[2],field_number) elif (args[1] == '-c4' and args[0] in [ 'SF','KE','IQ','ID' ]): error = vic.setPKFieldUserValue(args[0],args[2],field_number) else: error = errors.bad_field_count elif (args[1] == '-r4'): if (args[0] in [ 'KE','SF','IQ','ID' ]): output.append(vic.getPKField(args[0],3)) ### Only a field #4 on some sections else: error = errors.bad_field_count elif (args[1] in ['-r1','-r2','-r3']): field_number = int(args[1].split("r",1)[1]) - 1 ### Convert -r into index output.append(vic.getPKField(args[0],field_number)) elif (args[1] == '-?'): browser = config.getVar("help.browser","links") url = config.getVar("help.pk_help_url","file://Help/pk.html#"+string.lower(args[0])) os.system(browser+" "+url) else: output.append("PK Syntax Error") error = errors.incorrect_args ### Didn't match a PK* command else: output.append("PK Syntax Error") error = errors.unknown_command return handled,output,error