#! python: #w#VIC input.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC input.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 import sys ### #w#Input ### ### Given a 'pipe name', reads from that file/device/etc until ### EOF. If the input is greater than then the ### input is written to disk ### input_memory_limit = 65536 ### characters #s#input ### ### Acceptable input URLs are: ### stdin: ### file://path/to/file ### exec://command:arguments ### http://... ### ftp://... ### socket://host:port ... ### def input(url,filename,room_dir,stdin=""): ### Set working path so (possible) saved files go into room/ old_cwd = os.getcwd() os.chdir(room_dir) output = "" if (string.count(url,"stdin:") == 1): output,used_file = inputStdinUntilEOF(filename,stdin) elif (string.count(url,"file://")): output,used_file = inputFile(url,filename) elif (string.count(url,"http://")): output,used_file = inputHTTP(url,filename) else: print "Unknown input URL '"+url+"'" return "","" os.chdir(old_cwd) return output,filename #s#def inputStdin (to '.') ### ### Read from stdin until we get a single line containing only a '.' ### This would be better if we could read until a line containg ### the default system EOF character was entered, but this is provided ### in case it's impossible to read this character ### def inputStdin(filename,stdin): f = sys.stdin bytecount = 0 using_file = 0 file_failed = 0 output = "" if (stdin != ""): if (len(stdin) > input_memory_limit): try: file_output = open(filename,"w") file_output.write(output) output = "" using_file = 1 except: file_failed = 1 pass ### file failed, continue using memory if (len(stdin) <= input_memory_limit or file_failed): output = stdin else: while (1): try: input = f.readline() except: input = "" bytecount = bytecount + len(input) if (using_file == 0): if (len(input) >= 2 and input[0] == "." and (input[1] == '\r' or input[1] == '\n')): break else: output = output + string.rstrip(input) if (not file_failed and bytecount > input_memory_limit): try: file_output = open(filename,"w") file_output.write(output) output = "" using_file = 1 except: file_failed = 1 pass ### file failed, continue using memory else: ### Write to the file if (len(input) >= 2 and input[0] == "." and (input[1] == '\r' or input[1] == '\n')): file_output.close() break else: file_output.write(string.rstrip(input)) ### either it's in or stored in the given filename print "returning = ["+output+","+str(using_file)+"]" return output,using_file #s#def inoutStdinUntilEOF ### ### Read from stdin until we get an EOF ### The ^D/^Z/whatever must be entered on a line by itself ### def inputStdinUntilEOF(filename,stdin): f = sys.stdin bytecount = 0 using_file = 0 file_failed = 0 output = "" if (stdin != ""): ### have stdin from previous command, so use it if (len(stdin) > input_memory_limit): try: file_output = open(filename,"w") file_output.write(output) output = "" using_file = 1 except: file_failed = 1 pass ### file failed, continue using memory if (len(stdin) <= input_memory_limit or file_failed): output = stdin else: ### no stdin passed, read from console input = "non-blank" while (input != ""): try: input = f.readline() except: input = "" bytecount = bytecount + len(input) if (using_file == 0): if (len(input) >= 2 and input[0] == "." and (input[1] == '\r' or input[1] == '\n')): break else: output = output + string.rstrip(input) if (not file_failed and bytecount > input_memory_limit): try: file_output = open(filename,"w") file_output.write(output) output = "" using_file = 1 except: file_failed = 1 pass ### file failed, continue using memory else: ### Write to the file if (len(input) >= 2 and input[0] == "." and (input[1] == '\r' or input[1] == '\n')): file_output.close() break else: file_output.write(string.rstrip(input)) ### either it's in or stored in the given filename #print "returning = ["+output+","+str(using_file)+"]" return output,using_file #s#def inputFile ### ### Read input from a file instead of a pipe ### def inputFile(url,filename): output = "" using_file = 0 try: input_file = string.split(url,"file://",1)[1] f = open(input_file,"r") f.seek(0,2) ### goto EOF size = f.tell() f.seek(0,0) ### rewind if (size <= input_memory_limit): output = f.read() else: try: out = open(filename,"w") out.write(f.read()) out.close() using_file = 1 except: pass f.close() except: pass return output,using_file #s#def inputHTTP ### ### Read input from a http serer ### def inputHTTP(url,filename): ### parse the URL try: ### http://foo:bar@thishost.com:8888/path/to/file.txt ignore,url = string.split(url,"http://",1) host,path = string.split(url,"/",1) path = "/"+path if (string.count(host,'@') > 0): user_pass,host = string.split(host,"@",1) else: user_pass = '' if (string.count(host,':') > 0): host,port = string.split(host,":",1) else: port = "80" except: return "",0 ### Now do the HTTP fetching output = "" using_file = 0 try: import httplib http = httplib.HTTP(host,int(port)) http.putrequest('GET',path) http.endheaders() errcode, errmsg, headers = http.getreply() if (errcode == 200): ### Ok f = http.getfile() output = f.read() f.close() if (len(output) > input_memory_limit): try: out = open(filename,"w") out.write(output) out.close() using_file = 1 output = "" except: pass except: pass return output,using_file