#! python: #w#VIC oi.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC oi.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 import input #w#class OI class OI: "Obtain Input" #s#def getName def getName(self): return "Obtain Input" #s#def parseRun def parseRun(self,state,vic,config,command,stdin): handled = 0 output = [] error = errors.ok args,opts = options.getopt(command,['-? -help --help','-oi list','-ct -rt:','-c:' ]) if (len(args) > 0 and string.lower(args[0]) == 'oi'): 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) if ('-?' in opt_commands or '-help' in opt_commands or '--help' in opt_commands): browser = config.getVar("help.browser","links") url = config.getVar("help.oi_help_url","file://Help/oi.html") os.system(browser+" "+url) return 1,[],errors.ok #vic = state.getCurrentVIC() if (len(args) == 1 and len(opts) == 0): if (vic == None): vic = state.global_vars ### If no VIC focused, store var in global area input_device = vic.getIPInputDevice() var_name = args[0] filename = vic.getRoom()+vic.getOIFilename()+"."+var_name data,filed = input.input(input_device,filename,vic.getRoom(),stdin) #print "DATA: ["+data+"] FILED="+str(filed) if (data != ""): vic.setVariable(var_name,data) vic.saveOI() elif (data == "" and filed): vic.setVariable(var_name,"@"+filename) vic.saveOI() else: error = errors.set_var_failed elif (len(args) == 0 and len(opts) == 0): if (vic == None): error = errors.no_vic else: output.append(vic.getPKFileLine('OI')) elif ('-oi' in opt_commands or 'list' in opt_commands): ### Get a list of all variables if (vic == None): vic = state.global_vars ### If no VIC focused, store var in global area count = 0 for pair in vic.getVariableList(): if (pair[0] != '__VIC_NAME__' and pair[0] != '__VIC_ROOM__'): if (pair[2] != ""): output.append("$"+pair[0]+" = "+pair[1]+" ("+pair[2]+")") ### variable has a tag else: output.append("$"+pair[0]+" = "+pair[1]) count = count +1 if (count == 0): output.append("(no variables defined)") elif ('-c' in opt_commands and (len(args) == 0 or len(args) == 1)): ### Set or reset variables if (vic == None): vic = state.global_vars ### If no VIC focused, store var in global area i = options.getoptindex(opts,'-c') var_name = opts[i][1] if (len(args) == 1): ### Set/Change variable vic.setVariable(var_name,args[-1]) vic.saveOI() else: ### Clear variable if (vic.hasVariable(var_name)): vic.delVariable(var_name) vic.saveOI() else: error = errors.no_var elif ('-ct' in opt_commands and (len(args) == 0 or len(args) == 1)): if (vic == None): vic = state.global_vars ### If no VIC focused, store var in global area i = options.getoptindex(opts,'-ct') var_name = opts[i][1] if (vic.hasVariable(var_name)): if (len(args) == 0): ### Remove variable tag vic.delVariableTag(var_name) vic.saveOI() else: ### Set variable tag vic.setVariableTag(var_name,args[-1]) vic.saveOI() else: error = errors.no_var elif ('-rt' in opt_commands and len(args) == 0): if (vic == None): vic = state.global_vars ### If no VIC focused, store var in global area ### Return variable tag i = options.getoptindex(opts,'-rt') var_name = opts[i][1] if (vic.hasVariable(var_name)): output.append(vic.getVariableTag(var_name)) else: error = errors.no_var else: output.append("OI Syntax Error") error = errors.unknown_command return handled,output,error