#! python: #w#VIC sf.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC sf.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 #s#local modules import files import options import state import errors #w#class SF class SF: "Sequence Focus" #s#def getName def getName(self): return "Sequence Focus" #s#def parseRun def parseRun(self,state,vic,config,command,stdin): handled = 0 output = [] error = errors.ok args,opts = options.getopt(command,['-? -help --help','-r:','-wr -Wr -wc -Wc -w -W:' ]) if (len(args) > 0 and string.lower(args[0]) == 'sf'): handled = 1 del(args[0]) ### Make a seperate list of just the commands used opt_commands = [] for com,arg in opts: opt_commands.append(com) #print "SF-ARGS: "+str(args) #print "SF-OPTS: "+str(opts) #print "SF-COMM: "+str(opt_commands) if ('-?' in opt_commands or '-help' in opt_commands or '--help' in opt_commands): browser = config.getVar("help.browser","links") url = config.getVar("help.sf_help_url","file://Help/sf.html") os.system(browser+" "+url) return 1,[],errors.ok elif (len(args) == 1 and args[0].upper() == 'AI'): ### Point at no VIC state.setNoVIC() elif (len(args) == 2 and args[0].upper() == 'AI'): ### Switch to different VIC try: name,num = string.split(args[1],'.',1) except: if (args[1][0] in string.digits): name = "" num = args[1] else: name = args[1] num = "0" try: vic_index = state.getVicIndex(name,int(num)) except: vic_index = -1 if (vic_index == -1): error = errors.no_vic_by_name else: state.setCurrentVIC(vic_index) else: ### Handle SF if (vic != None and len(args) == 1 and len(opts) == 0 and args[0].upper() == 'SF'): output.append(vic.getPKFileLine('SF')) ### Handle SF -r ### SF -r N ### SF [@N]|[@N-M] elif ('-r' in opt_commands or (len(opt_commands) == 0 and len(args) == 1)): if (vic != None): filename = vic.getSFFilename(0) ### Not a '-r' option, given filename instead if (len(opt_commands) == 0 and len(args) == 1): line_or_filename = args[0] else: line_or_filename = opts[options.getoptindex(opts,'-r')][1] from_line = 1 to_line = 999999999 ### MAX_INT ? if (line_or_filename != '' and files.fileAllDigits(line_or_filename)): ### probably for SF SF -r NNNNN try: to_line = int(line_or_filename) if (using_current_script): from_line = vic.getScriptLine() else: from_line = 1 to_line = from_line + to_line except: to_line = 999999999 elif (line_or_filename != '' and not files.fileAllDigits(line_or_filename)): ### probably for SF ... if (string.count(line_or_filename,'@') == 1): filename,line_range = string.split(line_or_filename,'@',1) try: start,stop = string.split(line_range,'-',1) from_line = int(start) to_line = int(stop) if (from_line <= 0): from_line = 1 if (to_line <= 0): to_line = 1 if (from_line > to_line): ### reverse range - swap it x = from_line from_line = to_line to_line = x except: try: from_line = int(line_range) except: from_line = 1 to_line = from_line else: ### no line count filename = line_or_filename elif (vic == None): ### plain '-r' or '-r N' but no VIC error = errors.no_vic if (error == errors.ok): if (vic != None): path_filename,plain_filename = vic.getRoomPathAndFile(filename) else: path_filename = filename lines = [] try: fin = open(path_filename,"r") lines = fin.readlines() fin.close() except: error = errors.bad_sf_file #print "dumping lines from "+str(from_line)+" to "+str(to_line)+ " of "+path_filename #print "error = "+str(error) if (error == errors.ok): from_line = from_line -1 ### adjust to 0 -> N-1 index to_line = to_line -1 for i in range(len(lines)): if (i >= from_line and i <= to_line): output.append(string.rstrip(lines[i])) elif (i > to_line): break elif ('-w' in opt_commands): i = options.getoptindex(opts,'-w') vars_to_watch = opts[i][1] output.append("SF-STUB -w "+vars_to_watch) elif ('-W' in opt_commands): i = options.getoptindex(opts,'-w') vars_to_watch = opts[i][1] output.append("SF-STUB -W "+vars_to_watch) elif ('-wr' in opt_commands): output.append("SF-STUB -wr ") elif ('-Wr' in opt_commands): output.append("SF-STUB -Wr ") elif ('-wc' in opt_commands): output.append("SF-STUB -wc ") elif ('-Wc' in opt_commands): output.append("SF-STUB -Wc ") else: output.append("SF Syntax Error") error = errors.unknown_command return handled,output,error