Mercurial > cpython-lru_cache-weakref
annotate Lib/urlparse.py @ 38664:7619216bbc19 2.4
Bug #1502728: Correctly link against librt library on HP-UX.
(backport from rev. 46743)
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Thu, 08 Jun 2006 12:54:16 +0000 |
| parents | 3a92e5654748 |
| children |
| 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 |
|
25814
b8cbdb735cf2
Added missing entries to __all__.
Fred Drake <fdrake@acm.org>
parents:
25787
diff
changeset
|
7 __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag", |
|
b8cbdb735cf2
Added missing entries to __all__.
Fred Drake <fdrake@acm.org>
parents:
25787
diff
changeset
|
8 "urlsplit", "urlunsplit"] |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
9 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 # A classification of schemes ('' means apply by default) |
| 32004 | 11 uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'imap', |
| 36354 | 12 'wais', 'file', 'https', 'shttp', 'mms', |
| 13 'prospero', 'rtsp', 'rtspu', ''] | |
| 32004 | 14 uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet', |
| 36354 | 15 'imap', 'wais', 'file', 'mms', 'https', 'shttp', |
| 16 'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '', | |
| 17 'svn', 'svn+ssh'] | |
| 32004 | 18 non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', |
| 36354 | 19 'telnet', 'wais', 'imap', 'snews', 'sip'] |
| 32004 | 20 uses_params = ['ftp', 'hdl', 'prospero', 'http', 'imap', |
| 36354 | 21 'https', 'shttp', 'rtsp', 'rtspu', 'sip', |
| 22 'mms', ''] | |
| 32004 | 23 uses_query = ['http', 'wais', 'imap', 'https', 'shttp', 'mms', |
| 36354 | 24 'gopher', 'rtsp', 'rtspu', 'sip', ''] |
| 32004 | 25 uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news', |
| 36354 | 26 'nntp', 'wais', 'https', 'shttp', 'snews', |
| 27 'file', 'prospero', ''] | |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 # 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
|
30 scheme_chars = ('abcdefghijklmnopqrstuvwxyz' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
31 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
32 '0123456789' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
33 '+-.') |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
5785
b247297be9a0
After some discussion with Jeremy and Fred, decided to limit the
Guido van Rossum <guido@python.org>
parents:
5776
diff
changeset
|
35 MAX_CACHE_SIZE = 20 |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
36 _parse_cache = {} |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
37 |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
38 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
|
39 """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
|
40 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
|
41 _parse_cache = {} |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
42 |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
43 |
|
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
|
44 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
|
45 """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
|
46 <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
|
47 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
|
48 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
|
49 (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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 |
|
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 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 |
|
34379
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
67 def _splitnetloc(url, start=0): |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
68 for c in '/?#': # the order is important! |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
69 delim = url.find(c, start) |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
70 if delim >= 0: |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
71 break |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
72 else: |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
73 delim = len(url) |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
74 return url[start:delim], url[delim:] |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
75 |
|
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
|
76 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
|
77 """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
|
78 <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
|
79 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
|
80 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
|
81 (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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 if url[:2] == '//': |
|
34379
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
95 netloc, url = _splitnetloc(url, 2) |
|
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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 _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
|
102 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
|
103 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
|
104 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
|
105 break |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
106 else: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
107 scheme, url = url[:i].lower(), url[i+1:] |
|
34379
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
108 if scheme in uses_netloc and url[:2] == '//': |
|
287e71f44d79
Backport patch #712317: In URLs such as http://www.example.com?query=spam,
Johannes Gijsbers <jlg@dds.nl>
parents:
32435
diff
changeset
|
109 netloc, url = _splitnetloc(url, 2) |
|
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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 _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
|
116 return tuple |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 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
|
119 """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
|
120 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
|
121 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
|
122 (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
|
123 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
|
124 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
|
125 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
|
126 |
|
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 def urlunsplit((scheme, netloc, url, query, fragment)): |
|
25787
bf384111b3b3
Backporting urlparse.py:1.34, test_urlparse.py:1.8:
Guido van Rossum <guido@python.org>
parents:
25425
diff
changeset
|
128 if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'): |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 return url |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
|
9051
839c6622d15a
fix typo in keyword argument 'allow_frament' should be 'allow_fragment'
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
7125
diff
changeset
|
139 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
|
140 """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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 params, query, fragment)) |
|
30675
fce80ad83477
See rev. 1.42 for log message
Brett Cannon <bcannon@gmail.com>
parents:
28449
diff
changeset
|
160 if not (path or params or query): |
|
15874
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, bpath, |
|
30675
fce80ad83477
See rev. 1.42 for log message
Brett Cannon <bcannon@gmail.com>
parents:
28449
diff
changeset
|
162 bparams, bquery, 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
|
163 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
|
164 # 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 break |
|
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 = 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 else: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
179 break |
|
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 == ['', '..']: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
181 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
|
182 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
|
183 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
|
184 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
|
185 params, query, fragment)) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
187 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
|
188 """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
|
189 |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
190 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
|
191 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
|
192 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
|
193 """ |
|
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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 return url, '' |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
200 |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
201 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 test_input = """ |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 http://a/b/c/d |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 g:h = <URL:g:h> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 ./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
|
210 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
|
211 /g = <URL:http://a/g> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 //g = <URL:http://g> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 ?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
|
214 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
|
215 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
|
216 . = <URL:http://a/b/c/> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 ./ = <URL:http://a/b/c/> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 .. = <URL:http://a/b/> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 ../ = <URL:http://a/b/> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 ../g = <URL:http://a/b/g> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 ../.. = <URL:http://a/> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 ../../g = <URL:http://a/g> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 ../../../g = <URL:http://a/../g> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 ./../g = <URL:http://a/b/g> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 ./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
|
226 /./g = <URL:http://a/./g> |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 """ |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 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
|
237 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
|
238 base = '' |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 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
|
243 else: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
244 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
|
245 else: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 continue |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
254 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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 print 'EXPECTED', words[2], '!!!!!!!!!!' |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 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
|
267 test() |
