Mercurial > cpython-lru_cache-weakref
annotate Lib/gopherlib.py @ 37850:ebe59cd18273
Use explicit relative import for an, ehm, relative import.
| author | Thomas Wouters <thomas@python.org> |
|---|---|
| date | Tue, 18 Apr 2006 21:41:36 +0000 |
| parents | a1232602262c |
| children | 4cdf21a0dbbc |
| 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 |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
12081
diff
changeset
|
3 __all__ = ["send_selector","send_query"] |
|
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
12081
diff
changeset
|
4 |
|
36552
c5650dbf936e
Add deprecation warning to modules deprecated since 2000.
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
5 import warnings |
|
36562
a1232602262c
Changed the new deprecation warning to show the
Tim Peters <tim.peters@gmail.com>
parents:
36552
diff
changeset
|
6 warnings.warn("the gopherlib module is deprecated", DeprecationWarning, |
|
a1232602262c
Changed the new deprecation warning to show the
Tim Peters <tim.peters@gmail.com>
parents:
36552
diff
changeset
|
7 stacklevel=2) |
|
36552
c5650dbf936e
Add deprecation warning to modules deprecated since 2000.
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
8 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 # Default selector, host and port |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 DEF_SELECTOR = '1/' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 DEF_HOST = 'gopher.micro.umn.edu' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 DEF_PORT = 70 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 # Recognized file types |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 A_TEXT = '0' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 A_MENU = '1' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 A_CSO = '2' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 A_ERROR = '3' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 A_MACBINHEX = '4' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 A_PCBINHEX = '5' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 A_UUENCODED = '6' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 A_INDEX = '7' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 A_TELNET = '8' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 A_BINARY = '9' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 A_DUPLICATE = '+' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 A_SOUND = 's' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 A_EVENT = 'e' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 A_CALENDAR = 'c' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 A_HTML = 'h' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 A_TN3270 = 'T' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 A_MIME = 'M' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 A_IMAGE = 'I' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 A_WHOIS = 'w' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 A_QUERY = 'q' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 A_GIF = 'g' |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
36 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
|
37 A_WWW = 'w' # WWW address |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 A_PLUS_IMAGE = ':' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 A_PLUS_MOVIE = ';' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 A_PLUS_SOUND = '<' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 _names = dir() |
|
7112
ece4123e36ea
Some patches by Lars Marius Garshol:
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
44 _type_to_name_map = {} |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 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
|
46 """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
|
47 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
|
48 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
|
49 for name in _names: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
50 if name[:2] == 'A_': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
51 _type_to_name_map[eval(name)] = name[2:] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
21963
diff
changeset
|
52 if gtype in _type_to_name_map: |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
53 return _type_to_name_map[gtype] |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30497
diff
changeset
|
54 return 'TYPE=%r' % (gtype,) |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 # Names for characters and strings |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 CRLF = '\r\n' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 TAB = '\t' |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
1618
7f891f32864c
Changes to use default argument values where possible
Guido van Rossum <guido@python.org>
parents:
1555
diff
changeset
|
60 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
|
61 """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
|
62 import socket |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
63 if not port: |
|
16505
88c87c89b406
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
64 i = host.find(':') |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
65 if i >= 0: |
|
16505
88c87c89b406
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
66 host, port = host[:i], int(host[i+1:]) |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
67 if not port: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
68 port = DEF_PORT |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
69 elif type(port) == type(''): |
|
16505
88c87c89b406
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
70 port = int(port) |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
71 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 12081 | 72 s.connect((host, port)) |
|
21963
2faa23facef7
The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents:
19216
diff
changeset
|
73 s.sendall(selector + CRLF) |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
74 s.shutdown(1) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
75 return s.makefile('rb') |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
1618
7f891f32864c
Changes to use default argument values where possible
Guido van Rossum <guido@python.org>
parents:
1555
diff
changeset
|
77 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
|
78 """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
|
79 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
|
80 |
|
7112
ece4123e36ea
Some patches by Lars Marius Garshol:
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
81 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
|
82 """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
|
83 if path=="/": |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
84 return "/" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
85 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
86 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
|
87 |
|
ece4123e36ea
Some patches by Lars Marius Garshol:
Guido van Rossum <guido@python.org>
parents:
3398
diff
changeset
|
88 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
|
89 """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
|
90 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
|
91 if path=="/": |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
92 # 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
|
93 return "TYPE='unknown'" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
94 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
95 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
|
96 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 # 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
|
98 # 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
|
99 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 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
|
101 """Get a directory in the form of a list of entries.""" |
| 30497 | 102 entries = [] |
|
11706
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] |
|
16505
88c87c89b406
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
118 parts = line[1:].split(TAB) |
|
11706
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: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30497
diff
changeset
|
120 print '(Bad line from server: %r)' % (line,) |
|
11706
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) |
| 30497 | 129 entries.append(parts) |
| 130 return entries | |
|
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.""" |
| 30497 | 134 lines = [] |
| 135 get_alt_textfile(f, lines.append) | |
| 136 return lines | |
|
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 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
177 host = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
178 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
179 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
180 type = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
181 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
182 if len(type) > 1: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
183 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
|
184 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
185 selector = '' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
186 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
187 selector = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
188 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
189 query = '' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
190 if args: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
191 query = args[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
192 args = args[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
193 if type == A_INDEX: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
194 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
|
195 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
196 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
|
197 if type == A_TEXT: |
| 30497 | 198 lines = get_textfile(f) |
| 199 for item in lines: print item | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
200 elif type in (A_MENU, A_INDEX): |
| 30497 | 201 entries = get_directory(f) |
| 202 for item in entries: print item | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
203 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
204 data = get_binary(f) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30497
diff
changeset
|
205 print 'binary data:', len(data), 'bytes:', repr(data[:100])[:40] |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 # Run the test when run as script |
|
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 if __name__ == '__main__': |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
209 test() |
