annotate Demo/sockets/gopher.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 2bc66f1df7cc
children 7ada16a14968
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 1429
diff changeset
1 #! /usr/bin/env python
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # A simple gopher client.
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Usage: gopher [ [selector] host [port] ]
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import string
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import sys
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import os
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import socket
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # Default selector, host and port
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 DEF_SELECTOR = ''
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 DEF_HOST = 'gopher.micro.umn.edu'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 DEF_PORT = 70
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # Recognized file types
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 T_TEXTFILE = '0'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 T_MENU = '1'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 T_CSO = '2'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 T_ERROR = '3'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 T_BINHEX = '4'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 T_DOS = '5'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 T_UUENCODE = '6'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 T_SEARCH = '7'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 T_TELNET = '8'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 T_BINARY = '9'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 T_REDUNDANT = '+'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 T_SOUND = 's'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Dictionary mapping types to strings
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 typename = {'0': '<TEXT>', '1': '<DIR>', '2': '<CSO>', '3': '<ERROR>', \
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
33 '4': '<BINHEX>', '5': '<DOS>', '6': '<UUENCODE>', '7': '<SEARCH>', \
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
34 '8': '<TELNET>', '9': '<BINARY>', '+': '<REDUNDANT>', 's': '<SOUND>'}
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # Oft-used characters and strings
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 CRLF = '\r\n'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 TAB = '\t'
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # Open a TCP connection to a given host and port
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 def open_socket(host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
42 if not port:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
43 port = DEF_PORT
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
44 elif type(port) == type(''):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
45 port = string.atoi(port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
46 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
47 s.connect((host, port))
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
48 return s
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # Send a selector to a given host and port, return a file with the reply
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def send_request(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
52 s = open_socket(host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
53 s.send(selector + CRLF)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
54 s.shutdown(1)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
55 return s.makefile('r')
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # Get a menu in the form of a list of entries
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def get_menu(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
59 f = send_request(selector, host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
60 list = []
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
61 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
62 line = f.readline()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
63 if not line:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
64 print '(Unexpected EOF from server)'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
65 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
66 if line[-2:] == CRLF:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
67 line = line[:-2]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
68 elif line[-1:] in CRLF:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
69 line = line[:-1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
70 if line == '.':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
71 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
72 if not line:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
73 print '(Empty line from server)'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
74 continue
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
75 typechar = line[0]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
76 parts = string.splitfields(line[1:], TAB)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
77 if len(parts) < 4:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
78 print '(Bad line from server: %r)' % (line,)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
79 continue
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
80 if len(parts) > 4:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
81 print '(Extra info from server: %r)' % (parts[4:],)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
82 parts.insert(0, typechar)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
83 list.append(parts)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
84 f.close()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
85 return list
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 # Get a text file as a list of lines, with trailing CRLF stripped
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def get_textfile(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
89 list = []
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
90 get_alt_textfile(selector, host, port, list.append)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
91 return list
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # Get a text file and pass each line to a function, with trailing CRLF stripped
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 def get_alt_textfile(selector, host, port, func):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
95 f = send_request(selector, host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
96 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
97 line = f.readline()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
98 if not line:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
99 print '(Unexpected EOF from server)'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
100 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
101 if line[-2:] == CRLF:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
102 line = line[:-2]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
103 elif line[-1:] in CRLF:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
104 line = line[:-1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
105 if line == '.':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
106 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
107 if line[:2] == '..':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
108 line = line[1:]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
109 func(line)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
110 f.close()
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # Get a binary file as one solid data block
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def get_binary(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
114 f = send_request(selector, host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
115 data = f.read()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
116 f.close()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
117 return data
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 # Get a binary file and pass each block to a function
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 def get_alt_binary(selector, host, port, func, blocksize):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
121 f = send_request(selector, host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
122 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
123 data = f.read(blocksize)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
124 if not data:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
125 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
126 func(data)
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 # A *very* simple interactive browser
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 # Browser main command, has default arguments
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 def browser(*args):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
132 selector = DEF_SELECTOR
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
133 host = DEF_HOST
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
134 port = DEF_PORT
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
135 n = len(args)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
136 if n > 0 and args[0]:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
137 selector = args[0]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
138 if n > 1 and args[1]:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
139 host = args[1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
140 if n > 2 and args[2]:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
141 port = args[2]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
142 if n > 3:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
143 raise RuntimeError, 'too many args'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
144 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
145 browse_menu(selector, host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
146 except socket.error, msg:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
147 print 'Socket error:', msg
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
148 sys.exit(1)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
149 except KeyboardInterrupt:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
150 print '\n[Goodbye]'
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 # Browse a menu
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 def browse_menu(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
154 list = get_menu(selector, host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
155 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
156 print '----- MENU -----'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
157 print 'Selector:', repr(selector)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
158 print 'Host:', host, ' Port:', port
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
159 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
160 for i in range(len(list)):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
161 item = list[i]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
162 typechar, description = item[0], item[1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
163 print string.rjust(repr(i+1), 3) + ':', description,
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
164 if typename.has_key(typechar):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
165 print typename[typechar]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
166 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
167 print '<TYPE=' + repr(typechar) + '>'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
168 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
169 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
170 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
171 str = raw_input('Choice [CR == up a level]: ')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
172 except EOFError:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
173 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
174 return
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
175 if not str:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
176 return
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
177 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
178 choice = string.atoi(str)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
179 except string.atoi_error:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
180 print 'Choice must be a number; try again:'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
181 continue
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
182 if not 0 < choice <= len(list):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
183 print 'Choice out of range; try again:'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
184 continue
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
185 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
186 item = list[choice-1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
187 typechar = item[0]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
188 [i_selector, i_host, i_port] = item[2:5]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
189 if typebrowser.has_key(typechar):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
190 browserfunc = typebrowser[typechar]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
191 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
192 browserfunc(i_selector, i_host, i_port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
193 except (IOError, socket.error):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
194 print '***', sys.exc_type, ':', sys.exc_value
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
195 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
196 print 'Unsupported object type'
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 # Browse a text file
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 def browse_textfile(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
200 x = None
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
201 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
202 p = os.popen('${PAGER-more}', 'w')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
203 x = SaveLines(p)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
204 get_alt_textfile(selector, host, port, x.writeln)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
205 except IOError, msg:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
206 print 'IOError:', msg
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
207 if x:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
208 x.close()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
209 f = open_savefile()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
210 if not f:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
211 return
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
212 x = SaveLines(f)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
213 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
214 get_alt_textfile(selector, host, port, x.writeln)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
215 print 'Done.'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
216 except IOError, msg:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
217 print 'IOError:', msg
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
218 x.close()
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 # Browse a search index
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 def browse_search(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
222 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
223 print '----- SEARCH -----'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
224 print 'Selector:', repr(selector)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
225 print 'Host:', host, ' Port:', port
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
226 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
227 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
228 query = raw_input('Query [CR == up a level]: ')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
229 except EOFError:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
230 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
231 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
232 query = string.strip(query)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
233 if not query:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
234 break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
235 if '\t' in query:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
236 print 'Sorry, queries cannot contain tabs'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
237 continue
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
238 browse_menu(selector + TAB + query, host, port)
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 # "Browse" telnet-based information, i.e. open a telnet session
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 def browse_telnet(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
242 if selector:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
243 print 'Log in as', repr(selector)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
244 if type(port) <> type(''):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
245 port = repr(port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
246 sts = os.system('set -x; exec telnet ' + host + ' ' + port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
247 if sts:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
248 print 'Exit status:', sts
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 # "Browse" a binary file, i.e. save it to a file
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 def browse_binary(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
252 f = open_savefile()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
253 if not f:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
254 return
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
255 x = SaveWithProgress(f)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
256 get_alt_binary(selector, host, port, x.write, 8*1024)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
257 x.close()
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 # "Browse" a sound file, i.e. play it or save it
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 def browse_sound(selector, host, port):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
261 browse_binary(selector, host, port)
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 # Dictionary mapping types to browser functions
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 typebrowser = {'0': browse_textfile, '1': browse_menu, \
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
265 '4': browse_binary, '5': browse_binary, '6': browse_textfile, \
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
266 '7': browse_search, \
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
267 '8': browse_telnet, '9': browse_binary, 's': browse_sound}
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 # Class used to save lines, appending a newline to each line
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 class SaveLines:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
271 def __init__(self, f):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
272 self.f = f
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
273 def writeln(self, line):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
274 self.f.write(line + '\n')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
275 def close(self):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
276 sts = self.f.close()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
277 if sts:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
278 print 'Exit status:', sts
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 # Class used to save data while showing progress
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 class SaveWithProgress:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
282 def __init__(self, f):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
283 self.f = f
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
284 def write(self, data):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
285 sys.stdout.write('#')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
286 sys.stdout.flush()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
287 self.f.write(data)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
288 def close(self):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
289 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
290 sts = self.f.close()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
291 if sts:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
292 print 'Exit status:', sts
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 # Ask for and open a save file, or return None if not to save
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 def open_savefile():
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
296 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
297 savefile = raw_input( \
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
298 'Save as file [CR == don\'t save; |pipeline or ~user/... OK]: ')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
299 except EOFError:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
300 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
301 return None
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
302 savefile = string.strip(savefile)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
303 if not savefile:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
304 return None
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
305 if savefile[0] == '|':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
306 cmd = string.strip(savefile[1:])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
307 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
308 p = os.popen(cmd, 'w')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
309 except IOError, msg:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
310 print repr(cmd), ':', msg
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
311 return None
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
312 print 'Piping through', repr(cmd), '...'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
313 return p
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
314 if savefile[0] == '~':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
315 savefile = os.path.expanduser(savefile)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
316 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
317 f = open(savefile, 'w')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
318 except IOError, msg:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
319 print repr(savefile), ':', msg
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
320 return None
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
321 print 'Saving to', repr(savefile), '...'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
322 return f
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 # Test program
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 def test():
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
326 if sys.argv[4:]:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
327 print 'usage: gopher [ [selector] host [port] ]'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
328 sys.exit(2)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
329 elif sys.argv[3:]:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
330 browser(sys.argv[1], sys.argv[2], sys.argv[3])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
331 elif sys.argv[2:]:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
332 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
333 port = string.atoi(sys.argv[2])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
334 selector = ''
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
335 host = sys.argv[1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
336 except string.atoi_error:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
337 selector = sys.argv[1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
338 host = sys.argv[2]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
339 port = ''
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
340 browser(selector, host, port)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
341 elif sys.argv[1:]:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
342 browser('', sys.argv[1])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
343 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
344 browser()
1080
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 # Call the test program as a main program
7657a75094f1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 test()