annotate Lib/nturl2path.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents de0da2759c8c
children 7d69d04522e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10419
diff changeset
1 """Convert a NT pathname to a file URL and vice versa."""
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 def url2pathname(url):
36124
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
4 """OS-specific conversion from a relative URL of the 'file' scheme
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
5 to a file system path; not recommended for general use."""
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
6 # e.g.
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
7 # ///C|/foo/bar/spam.foo
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
8 # becomes
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
9 # C:\foo\bar\spam.foo
48336
2c1068328b5b follow-up of r64385: rename urllib.quote in nturl2path
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43020
diff changeset
10 import string, urllib.parse
36039
de0c2fea65c2 Bug #1378455: a problem of urllib using open_local_file
Georg Brandl <georg@python.org>
parents: 18777
diff changeset
11 # Windows itself uses ":" even in URLs.
de0c2fea65c2 Bug #1378455: a problem of urllib using open_local_file
Georg Brandl <georg@python.org>
parents: 18777
diff changeset
12 url = url.replace(':', '|')
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
13 if not '|' in url:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
14 # No drive specifier, just convert slashes
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
15 if url[:4] == '////':
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
16 # path is something like ////host/path/on/remote/host
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
17 # convert this to \\host\path\on\remote\host
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
18 # (notice halving of slashes at the start of the path)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
19 url = url[2:]
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
20 components = url.split('/')
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
21 # make sure not to convert quoted slashes :-)
48336
2c1068328b5b follow-up of r64385: rename urllib.quote in nturl2path
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43020
diff changeset
22 return urllib.parse.unquote('\\'.join(components))
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
23 comp = url.split('|')
18777
4d33acbdc508 Use string.ascii_letters instead of string.letters.
Fred Drake <fdrake@acm.org>
parents: 16657
diff changeset
24 if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
25 error = 'Bad URL: ' + url
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 36124
diff changeset
26 raise IOError(error)
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
27 drive = comp[0][-1].upper()
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
28 components = comp[1].split('/')
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
29 path = drive + ':'
69348
de0da2759c8c Fix Issue11474 - fix url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents: 53805
diff changeset
30 for comp in components:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
31 if comp:
48336
2c1068328b5b follow-up of r64385: rename urllib.quote in nturl2path
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43020
diff changeset
32 path = path + '\\' + urllib.parse.unquote(comp)
69348
de0da2759c8c Fix Issue11474 - fix url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents: 53805
diff changeset
33 # Issue #11474 - handing url such as |c/|
de0da2759c8c Fix Issue11474 - fix url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents: 53805
diff changeset
34 if path.endswith(':') and url.endswith('/'):
de0da2759c8c Fix Issue11474 - fix url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents: 53805
diff changeset
35 path += '\\'
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
36 return path
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 def pathname2url(p):
36124
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
39 """OS-specific conversion from a file system path to a relative URL
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
40 of the 'file' scheme; not recommended for general use."""
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
41 # e.g.
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
42 # C:\foo\bar\spam.foo
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
43 # becomes
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
44 # ///C|/foo/bar/spam.foo
48336
2c1068328b5b follow-up of r64385: rename urllib.quote in nturl2path
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43020
diff changeset
45 import urllib.parse
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
46 if not ':' in p:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
47 # No drive specifier, just convert slashes and quote the name
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
48 if p[:2] == '\\\\':
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
49 # path is something like \\host\path\on\remote\host
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
50 # convert this to ////host/path/on/remote/host
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
51 # (notice doubling of slashes at the start of the path)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
52 p = '\\\\' + p
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
53 components = p.split('\\')
48336
2c1068328b5b follow-up of r64385: rename urllib.quote in nturl2path
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43020
diff changeset
54 return urllib.parse.quote('/'.join(components))
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
55 comp = p.split(':')
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
56 if len(comp) != 2 or len(comp[0]) > 1:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
57 error = 'Bad path: ' + p
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 36124
diff changeset
58 raise IOError(error)
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
48336
2c1068328b5b follow-up of r64385: rename urllib.quote in nturl2path
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43020
diff changeset
60 drive = urllib.parse.quote(comp[0].upper())
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
61 components = comp[1].split('\\')
53805
bbe2aa431692 Fix for issue1153027, making Py3k changes similar to fix in issue918368.
Senthil Kumaran <orsenthil@gmail.com>
parents: 48336
diff changeset
62 path = '///' + drive + ':'
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
63 for comp in components:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
64 if comp:
48336
2c1068328b5b follow-up of r64385: rename urllib.quote in nturl2path
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43020
diff changeset
65 path = path + '/' + urllib.parse.quote(comp)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
66 return path