Mercurial > cpython-lru_cache-weakref
annotate Lib/gopherlib.py @ 16413:8819867f14ef legacy-trunk
Patch derived from Trent's 101162: a Python/C API testing framework.
STILL NEEDS UNIX BUILD CHANGES.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Fri, 02 Feb 2001 05:57:15 +0000 |
| parents | 46920b60cd33 |
| children | 88c87c89b406 |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
1 """Gopher protocol client interface.""" |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 import string |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
12081
diff
changeset
|
5 __all__ = ["send_selector","send_query"] |
|
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
12081
diff
changeset
|
6 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 # Default selector, host and port |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 DEF_SELECTOR = '1/' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 DEF_HOST = 'gopher.micro.umn.edu' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 DEF_PORT = 70 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # Recognized file types |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 A_TEXT = '0' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 A_MENU = '1' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 A_CSO = '2' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 A_ERROR = '3' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 A_MACBINHEX = '4' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 A_PCBINHEX = '5' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 A_UUENCODED = '6' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 A_INDEX = '7' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 A_TELNET = '8' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 A_BINARY = '9' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 A_DUPLICATE = '+' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 A_SOUND = 's' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 A_EVENT = 'e' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 A_CALENDAR = 'c' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 A_HTML = 'h' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 A_TN3270 = 'T' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 A_MIME = 'M' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 A_IMAGE = 'I' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 A_WHOIS = 'w' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 A_QUERY = 'q' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 A_GIF = 'g' |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
34 A_HTML = 'h' # HTML file |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
35 A_WWW = 'w' # WWW address |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 A_PLUS_IMAGE = ':' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 A_PLUS_MOVIE = ';' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 A_PLUS_SOUND = '<' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 _names = dir() |
|
7112
ece4123e36ea
Some patches by Lars Marius Garshol:
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
42 _type_to_name_map = {} |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 def type_to_name(gtype): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
44 """Map all file types to strings; unknown types become TYPE='x'.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
45 global _type_to_name_map |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
46 if _type_to_name_map=={}: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
47 for name in _names: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
48 if name[:2] == 'A_': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
49 _type_to_name_map[eval(name)] = name[2:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
50 if _type_to_name_map.has_key(gtype): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
51 return _type_to_name_map[gtype] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
52 return 'TYPE=' + `gtype` |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 # Names for characters and strings |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 CRLF = '\r\n' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 TAB = '\t' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
1618
7f891f32864c
Changes to use default argument values where possible
Guido van Rossum <guido@python.org>
parents:
1555
diff
changeset
|
58 def send_selector(selector, host, port = 0): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
59 """Send a selector to a given host and port, return a file with the reply.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
60 import socket |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
61 import string |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
62 if not port: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
63 i = string.find(host, ':') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
64 if i >= 0: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
65 host, port = host[:i], string.atoi(host[i+1:]) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
66 if not port: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
67 port = DEF_PORT |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
68 elif type(port) == type(''): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
69 port = string.atoi(port) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
70 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 12081 | 71 s.connect((host, port)) |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
72 s.send(selector + CRLF) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
73 s.shutdown(1) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
74 return s.makefile('rb') |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
1618
7f891f32864c
Changes to use default argument values where possible
Guido van Rossum <guido@python.org>
parents:
1555
diff
changeset
|
76 def send_query(selector, query, host, port = 0): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
77 """Send a selector and a query string.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
78 return send_selector(selector + '\t' + query, host, port) |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
7112
ece4123e36ea
Some patches by Lars Marius Garshol:
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
80 def path_to_selector(path): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
81 """Takes a path as returned by urlparse and returns the appropriate selector.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
82 if path=="/": |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
83 return "/" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
84 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
85 return path[2:] # Cuts initial slash and data type identifier |
|
7112
ece4123e36ea
Some patches by Lars Marius Garshol:
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
86 |
|
ece4123e36ea
Some patches by Lars Marius Garshol:
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
87 def path_to_datatype_name(path): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
88 """Takes a path as returned by urlparse and maps it to a string. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
89 See section 3.4 of RFC 1738 for details.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
90 if path=="/": |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
91 # No way to tell, although "INDEX" is likely |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
92 return "TYPE='unknown'" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
93 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
94 return type_to_name(path[1]) |
|
7752
b4b0e4e759c5
A few lines were indented using spaces instead of tabs -- fix them.
Guido van Rossum <guido@python.org>
parents:
7112
diff
changeset
|
95 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 # The following functions interpret the data returned by the gopher |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 # server according to the expected type, e.g. textfile or directory |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 def get_directory(f): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
100 """Get a directory in the form of a list of entries.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
101 import string |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
102 list = [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
103 while 1: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
104 line = f.readline() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
105 if not line: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
106 print '(Unexpected EOF from server)' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
107 break |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
108 if line[-2:] == CRLF: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
109 line = line[:-2] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
110 elif line[-1:] in CRLF: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
111 line = line[:-1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
112 if line == '.': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
113 break |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
114 if not line: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
115 print '(Empty line from server)' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
116 continue |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
117 gtype = line[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
118 parts = string.splitfields(line[1:], TAB) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
119 if len(parts) < 4: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
120 print '(Bad line from server:', `line`, ')' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
121 continue |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
122 if len(parts) > 4: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
123 if parts[4:] != ['+']: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
124 print '(Extra info from server:', |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
125 print parts[4:], ')' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
126 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
127 parts.append('') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
128 parts.insert(0, gtype) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
129 list.append(parts) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
130 return list |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 def get_textfile(f): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
133 """Get a text file as a list of lines, with trailing CRLF stripped.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
134 list = [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
135 get_alt_textfile(f, list.append) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
136 return list |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 def get_alt_textfile(f, func): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
139 """Get a text file and pass each line to a function, with trailing CRLF stripped.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
140 while 1: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
141 line = f.readline() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
142 if not line: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
143 print '(Unexpected EOF from server)' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
144 break |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
145 if line[-2:] == CRLF: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
146 line = line[:-2] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
147 elif line[-1:] in CRLF: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
148 line = line[:-1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
149 if line == '.': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
150 break |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
151 if line[:2] == '..': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
152 line = line[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
153 func(line) |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 def get_binary(f): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
156 """Get a binary file as one solid data block.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
157 data = f.read() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
158 return data |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 def get_alt_binary(f, func, blocksize): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
161 """Get a binary file and pass each block to a function.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
162 while 1: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
163 data = f.read(blocksize) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
164 if not data: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
165 break |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
166 func(data) |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 def test(): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
169 """Trivial test program.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
170 import sys |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
171 import getopt |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
172 opts, args = getopt.getopt(sys.argv[1:], '') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
173 selector = DEF_SELECTOR |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
174 type = selector[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
175 host = DEF_HOST |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
176 port = DEF_PORT |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
177 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
178 host = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
179 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
180 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
181 type = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
182 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
183 if len(type) > 1: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
184 type, selector = type[0], type |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
185 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
186 selector = '' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
187 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
188 selector = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
189 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
190 query = '' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
191 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
192 query = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
193 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
194 if type == A_INDEX: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
195 f = send_query(selector, query, host) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
196 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
197 f = send_selector(selector, host) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
198 if type == A_TEXT: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
199 list = get_textfile(f) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
200 for item in list: print item |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
201 elif type in (A_MENU, A_INDEX): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
202 list = get_directory(f) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
203 for item in list: print item |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
204 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
205 data = get_binary(f) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
206 print 'binary data:', len(data), 'bytes:', `data[:100]`[:40] |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 # Run the test when run as script |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 if __name__ == '__main__': |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
210 test() |
