annotate Lib/nturl2path.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 4d33acbdc508
children de0c2fea65c2
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):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
4 r"""Convert a URL to a DOS path.
12797
09f70f91bdae Add 'r' to docstrings to avoid expanded backslash escapes. (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents: 11706
diff changeset
5
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
6 ///C|/foo/bar/spam.foo
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
8 becomes
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
10 C:\foo\bar\spam.foo
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
11 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
12 import string, urllib
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):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
36 r"""Convert a DOS path name to a file url.
12797
09f70f91bdae Add 'r' to docstrings to avoid expanded backslash escapes. (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents: 11706
diff changeset
37
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
38 C:\foo\bar\spam.foo
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
40 becomes
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
42 ///C|/foo/bar/spam.foo
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
43 """
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
18777
4d33acbdc508 Use string.ascii_letters instead of string.letters.
Fred Drake <fdrake@acm.org>
parents: 16657
diff changeset
45 import urllib
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('\\')
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
54 return urllib.quote('/'.join(components))
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
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
58 raise IOError, error
3725
0ff5bd8ebb01 url2path for NT
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
60 drive = urllib.quote(comp[0].upper())
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16460
diff changeset
61 components = comp[1].split('\\')
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
62 path = '///' + drive + '|'
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:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
65 path = path + '/' + urllib.quote(comp)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12797
diff changeset
66 return path