annotate Lib/urlparse.py @ 24042:63fbbed10714 legacy-trunk

Simplify HTTPSConnection constructor. See discussion in SF bug 458463.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Fri, 28 Jun 2002 23:38:14 +0000
parents 55512fe1d6f1
children 6237be27a597
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10830
diff changeset
1 """Parse (absolute and relative) URLs.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10830
diff changeset
2
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10830
diff changeset
3 See RFC 1808: "Relative Uniform Resource Locators", by R. Fielding,
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10830
diff changeset
4 UC Irvine, June 1995.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10830
diff changeset
5 """
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
7 __all__ = ["urlparse", "urlunparse", "urljoin"]
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
8
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # A classification of schemes ('' means apply by default)
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'wais', 'file',
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
11 'https', 'shttp',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
12 'prospero', 'rtsp', 'rtspu', '']
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet', 'wais',
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
14 'file',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
15 'https', 'shttp', 'snews',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
16 'prospero', 'rtsp', 'rtspu', '']
4853
cf90745381bf Added characteristics of shttp, https, and snews.
Guido van Rossum <guido@python.org>
parents: 4831
diff changeset
17 non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', 'telnet', 'wais',
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
18 'snews', 'sip',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
19 ]
4853
cf90745381bf Added characteristics of shttp, https, and snews.
Guido van Rossum <guido@python.org>
parents: 4831
diff changeset
20 uses_params = ['ftp', 'hdl', 'prospero', 'http',
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
21 'https', 'shttp', 'rtsp', 'rtspu', 'sip',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
22 '']
4853
cf90745381bf Added characteristics of shttp, https, and snews.
Guido van Rossum <guido@python.org>
parents: 4831
diff changeset
23 uses_query = ['http', 'wais',
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
24 'https', 'shttp',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
25 'gopher', 'rtsp', 'rtspu', 'sip',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
26 '']
3641
e4faadb4abe2 added hdl protocol properties
Guido van Rossum <guido@python.org>
parents: 3482
diff changeset
27 uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news', 'nntp', 'wais',
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
28 'https', 'shttp', 'snews',
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
29 'file', 'prospero', '']
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Characters valid in scheme names
15623
db69c092c906 Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents: 12967
diff changeset
32 scheme_chars = ('abcdefghijklmnopqrstuvwxyz'
db69c092c906 Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents: 12967
diff changeset
33 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
db69c092c906 Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents: 12967
diff changeset
34 '0123456789'
db69c092c906 Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents: 12967
diff changeset
35 '+-.')
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
5785
b247297be9a0 After some discussion with Jeremy and Fred, decided to limit the
Guido van Rossum <guido@python.org>
parents: 5776
diff changeset
37 MAX_CACHE_SIZE = 20
3652
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
38 _parse_cache = {}
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
39
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
40 def clear_cache():
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
41 """Clear the parse cache."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
42 global _parse_cache
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
43 _parse_cache = {}
3652
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
44
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
45
20995
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
46 def urlparse(url, scheme='', allow_fragments=1):
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
47 """Parse a URL into 6 components:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
48 <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
49 Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
50 Note that we don't break the components up in smaller bits
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
51 (e.g. netloc is a single string) and we don't expand % escapes."""
20995
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
52 tuple = urlsplit(url, scheme, allow_fragments)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
53 scheme, netloc, url, query, fragment = tuple
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
54 if scheme in uses_params and ';' in url:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
55 url, params = _splitparams(url)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
56 else:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
57 params = ''
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
58 return scheme, netloc, url, params, query, fragment
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
59
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
60 def _splitparams(url):
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
61 if '/' in url:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
62 i = url.find(';', url.rfind('/'))
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
63 if i < 0:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
64 return url, ''
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
65 else:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
66 i = url.find(';')
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
67 return url[:i], url[i+1:]
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
68
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
69 def urlsplit(url, scheme='', allow_fragments=1):
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
70 """Parse a URL into 5 components:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
71 <scheme>://<netloc>/<path>?<query>#<fragment>
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
72 Return a 5-tuple: (scheme, netloc, path, query, fragment).
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
73 Note that we don't break the components up in smaller bits
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
74 (e.g. netloc is a single string) and we don't expand % escapes."""
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
75 key = url, scheme, allow_fragments
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
76 cached = _parse_cache.get(key, None)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
77 if cached:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
78 return cached
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
79 if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
80 clear_cache()
20995
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
81 netloc = query = fragment = ''
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
82 i = url.find(':')
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
83 if i > 0:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
84 if url[:i] == 'http': # optimize the common case
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
85 scheme = url[:i].lower()
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
86 url = url[i+1:]
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
87 if url[:2] == '//':
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
88 i = url.find('/', 2)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
89 if i < 0:
22379
55512fe1d6f1 amk's fix attached to
Michael W. Hudson <mwh@python.net>
parents: 20995
diff changeset
90 i = url.find('#')
55512fe1d6f1 amk's fix attached to
Michael W. Hudson <mwh@python.net>
parents: 20995
diff changeset
91 if i < 0:
55512fe1d6f1 amk's fix attached to
Michael W. Hudson <mwh@python.net>
parents: 20995
diff changeset
92 i = len(url)
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
93 netloc = url[2:i]
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
94 url = url[i:]
20995
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
95 if allow_fragments and '#' in url:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
96 url, fragment = url.split('#', 1)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
97 if '?' in url:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
98 url, query = url.split('?', 1)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
99 tuple = scheme, netloc, url, query, fragment
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
100 _parse_cache[key] = tuple
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
101 return tuple
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
102 for c in url[:i]:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
103 if c not in scheme_chars:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
104 break
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
105 else:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
106 scheme, url = url[:i].lower(), url[i+1:]
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
107 if scheme in uses_netloc:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
108 if url[:2] == '//':
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
109 i = url.find('/', 2)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
110 if i < 0:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
111 i = len(url)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
112 netloc, url = url[2:i], url[i:]
20995
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
113 if allow_fragments and scheme in uses_fragment and '#' in url:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
114 url, fragment = url.split('#', 1)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
115 if scheme in uses_query and '?' in url:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
116 url, query = url.split('?', 1)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
117 tuple = scheme, netloc, url, query, fragment
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
118 _parse_cache[key] = tuple
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
119 return tuple
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def urlunparse((scheme, netloc, url, params, query, fragment)):
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
122 """Put a parsed URL back together again. This may result in a
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
123 slightly different, but equivalent URL, if the URL that was parsed
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
124 originally had redundant delimiters, e.g. a ? with an empty query
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
125 (the draft states that these are equivalent)."""
20995
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
126 if params:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
127 url = "%s;%s" % (url, params)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
128 return urlunsplit((scheme, netloc, url, query, fragment))
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
129
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
130 def urlunsplit((scheme, netloc, url, query, fragment)):
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
131 if netloc or (scheme in uses_netloc and url[:2] == '//'):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
132 if url and url[:1] != '/': url = '/' + url
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
133 url = '//' + (netloc or '') + url
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
134 if scheme:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
135 url = scheme + ':' + url
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
136 if query:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
137 url = url + '?' + query
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
138 if fragment:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
139 url = url + '#' + fragment
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
140 return url
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
9051
839c6622d15a fix typo in keyword argument 'allow_frament' should be 'allow_fragment'
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 7125
diff changeset
142 def urljoin(base, url, allow_fragments = 1):
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
143 """Join a base URL and a possibly relative URL to form an absolute
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
144 interpretation of the latter."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
145 if not base:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
146 return url
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
147 if not url:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
148 return base
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
149 bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
150 urlparse(base, '', allow_fragments)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
151 scheme, netloc, path, params, query, fragment = \
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
152 urlparse(url, bscheme, allow_fragments)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
153 if scheme != bscheme or scheme not in uses_relative:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
154 return url
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
155 if scheme in uses_netloc:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
156 if netloc:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
157 return urlunparse((scheme, netloc, path,
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
158 params, query, fragment))
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
159 netloc = bnetloc
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
160 if path[:1] == '/':
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
161 return urlunparse((scheme, netloc, path,
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
162 params, query, fragment))
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
163 if not path:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
164 if not params:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
165 params = bparams
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
166 if not query:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
167 query = bquery
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
168 return urlunparse((scheme, netloc, bpath,
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
169 params, query, fragment))
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
170 segments = bpath.split('/')[:-1] + path.split('/')
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
171 # XXX The stuff below is bogus in various ways...
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
172 if segments[-1] == '.':
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
173 segments[-1] = ''
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
174 while '.' in segments:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
175 segments.remove('.')
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
176 while 1:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
177 i = 1
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
178 n = len(segments) - 1
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
179 while i < n:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
180 if (segments[i] == '..'
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
181 and segments[i-1] not in ('', '..')):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
182 del segments[i-1:i+1]
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
183 break
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
184 i = i+1
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
185 else:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
186 break
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
187 if segments == ['', '..']:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
188 segments[-1] = ''
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
189 elif len(segments) >= 2 and segments[-1] == '..':
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
190 segments[-2:] = ['']
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
191 return urlunparse((scheme, netloc, '/'.join(segments),
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
192 params, query, fragment))
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
3652
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
194 def urldefrag(url):
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
195 """Removes any existing fragment from URL.
3652
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
196
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
197 Returns a tuple of the defragmented URL and the fragment. If
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
198 the URL contained no fragments, the second element is the
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
199 empty string.
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
200 """
20995
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
201 if '#' in url:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
202 s, n, p, a, q, frag = urlparse(url)
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
203 defrag = urlunparse((s, n, p, a, q, ''))
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
204 return defrag, frag
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
205 else:
174ccb035b3d Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents: 19209
diff changeset
206 return url, ''
3652
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
207
728a39a3e0d5 optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents: 3641
diff changeset
208
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 test_input = """
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 http://a/b/c/d
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 g:h = <URL:g:h>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 http:g = <URL:http://a/b/c/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 http: = <URL:http://a/b/c/d>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 g = <URL:http://a/b/c/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 ./g = <URL:http://a/b/c/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 g/ = <URL:http://a/b/c/g/>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 /g = <URL:http://a/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 //g = <URL:http://g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 ?y = <URL:http://a/b/c/d?y>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 g?y = <URL:http://a/b/c/g?y>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 g?y/./x = <URL:http://a/b/c/g?y/./x>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 . = <URL:http://a/b/c/>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 ./ = <URL:http://a/b/c/>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 .. = <URL:http://a/b/>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 ../ = <URL:http://a/b/>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 ../g = <URL:http://a/b/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 ../.. = <URL:http://a/>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 ../../g = <URL:http://a/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 ../../../g = <URL:http://a/../g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 ./../g = <URL:http://a/b/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 ./g/. = <URL:http://a/b/c/g/>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 /./g = <URL:http://a/./g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 g/./h = <URL:http://a/b/c/g/h>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 g/../h = <URL:http://a/b/c/h>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 http:g = <URL:http://a/b/c/g>
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 http: = <URL:http://a/b/c/d>
9793
737d05a9bb5e Fixed bug in the common-case code for HTTP URLs; it would lose the query,
Andrew M. Kuchling <amk@amk.ca>
parents: 9665
diff changeset
238 http:?y = <URL:http://a/b/c/d?y>
737d05a9bb5e Fixed bug in the common-case code for HTTP URLs; it would lose the query,
Andrew M. Kuchling <amk@amk.ca>
parents: 9665
diff changeset
239 http:g?y = <URL:http://a/b/c/g?y>
737d05a9bb5e Fixed bug in the common-case code for HTTP URLs; it would lose the query,
Andrew M. Kuchling <amk@amk.ca>
parents: 9665
diff changeset
240 http:g?y/./x = <URL:http://a/b/c/g?y/./x>
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 """
9665
e11d75005a5a Add XXX comment about a test that doesn't seem right -- no time to
Guido van Rossum <guido@python.org>
parents: 9068
diff changeset
242 # XXX The result for //g is actually http://g/; is this a problem?
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 def test():
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
245 import sys
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
246 base = ''
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
247 if sys.argv[1:]:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
248 fn = sys.argv[1]
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
249 if fn == '-':
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
250 fp = sys.stdin
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
251 else:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
252 fp = open(fn)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
253 else:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
254 import StringIO
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
255 fp = StringIO.StringIO(test_input)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
256 while 1:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
257 line = fp.readline()
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
258 if not line: break
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
259 words = line.split()
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
260 if not words:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
261 continue
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
262 url = words[0]
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
263 parts = urlparse(url)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
264 print '%-10s : %s' % (url, parts)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
265 abs = urljoin(base, url)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
266 if not base:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
267 base = abs
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
268 wrapped = '<URL:%s>' % abs
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
269 print '%-10s = %s' % (url, wrapped)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
270 if len(words) == 3 and words[1] == '=':
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
271 if wrapped != words[2]:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
272 print 'EXPECTED', words[2], '!!!!!!!!!!'
1875
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
8c9cc054c5ed New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 if __name__ == '__main__':
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15741
diff changeset
275 test()