annotate Lib/nturl2path.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 278cf5c8f1fa
children 4556f17356f2
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
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
10 import string, urllib
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 :-)
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
22 return urllib.unquote('\\'.join(components))
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
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
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 + ':'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
30 for comp in components:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
31 if comp:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
32 path = path + '\\' + urllib.unquote(comp)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
33 return path
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def pathname2url(p):
36124
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
36 """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
37 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
38 # e.g.
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
39 # C:\foo\bar\spam.foo
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
40 # becomes
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 36039
diff changeset
41 # ///C|/foo/bar/spam.foo
18777
4d33acbdc508 Use string.ascii_letters instead of string.letters.
Fred Drake <fdrake@acm.org>
parents: 16657
diff changeset
42 import urllib
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
43 if not ':' in p:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
44 # No drive specifier, just convert slashes and quote the name
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
45 if p[:2] == '\\\\':
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
46 # path is something like \\host\path\on\remote\host
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
47 # convert this to ////host/path/on/remote/host
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
48 # (notice doubling of slashes at the start of the path)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
49 p = '\\\\' + p
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
50 components = p.split('\\')
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
51 return urllib.quote('/'.join(components))
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
52 comp = p.split(':')
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
53 if len(comp) != 2 or len(comp[0]) > 1:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
54 error = 'Bad path: ' + p
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
55 raise IOError, error
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
57 drive = urllib.quote(comp[0].upper())
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
58 components = comp[1].split('\\')
56388
278cf5c8f1fa merge r72343. Fix for issue7043
Senthil Kumaran <orsenthil@gmail.com>
parents: 36124
diff changeset
59 path = '///' + drive + ':'
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
60 for comp in components:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
61 if comp:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
62 path = path + '/' + urllib.quote(comp)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
63 return path