annotate Lib/macurl2path.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 b11780b068fa
children 71a294bd7950
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 10889
diff changeset
1 """Macintosh-specific module for conversion between pathnames and URLs.
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 10889
diff changeset
2
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 10889
diff changeset
3 Do not import directly; use urllib instead."""
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import urllib
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import os
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
16198
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
8 __all__ = ["url2pathname","pathname2url"]
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
9
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def url2pathname(pathname):
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 "Convert /-delimited pathname to mac pathname"
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # XXXX The .. handling should be fixed...
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 tp = urllib.splittype(pathname)[0]
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
16 if tp and tp != 'file':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
17 raise RuntimeError, 'Cannot convert non-local URL to pathname'
10889
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
18 # Turn starting /// into /, an empty hostname means current host
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
19 if pathname[:3] == '///':
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
20 pathname = pathname[2:]
10889
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
21 elif pathname[:2] == '//':
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
22 raise RuntimeError, 'Cannot convert non-local URL to pathname'
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
23 components = pathname.split('/')
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # Remove . and embedded ..
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 i = 0
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 while i < len(components):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
27 if components[i] == '.':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
28 del components[i]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
29 elif components[i] == '..' and i > 0 and \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
30 components[i-1] not in ('', '..'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
31 del components[i-1:i+1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
32 i = i-1
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
33 elif components[i] == '' and i > 0 and components[i-1] != '':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
34 del components[i]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
35 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
36 i = i+1
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 if not components[0]:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
38 # Absolute unix path, don't start with colon
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
39 rv = ':'.join(components[1:])
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
41 # relative unix path, start with colon. First replace
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
42 # leading .. by empty strings (giving ::file)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
43 i = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
44 while i < len(components) and components[i] == '..':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
45 components[i] = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
46 i = i + 1
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
47 rv = ':' + ':'.join(components)
8847
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
48 # and finally unquote slashes and other funny characters
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
49 return urllib.unquote(rv)
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def pathname2url(pathname):
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 "convert mac pathname to /-delimited pathname"
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if '/' in pathname:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
54 raise RuntimeError, "Cannot convert pathname containing slashes"
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
55 components = pathname.split(':')
5556
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
56 # Remove empty first and/or last component
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
57 if components[0] == '':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
58 del components[0]
5556
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
59 if components[-1] == '':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
60 del components[-1]
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # Replace empty string ('::') by .. (will result in '/../' later)
5556
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
62 for i in range(len(components)):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
63 if components[i] == '':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
64 components[i] = '..'
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 # Truncate names longer than 31 bytes
8847
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
66 components = map(_pncomp2url, components)
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if os.path.isabs(pathname):
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
69 return '/' + '/'.join(components)
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 else:
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
71 return '/'.join(components)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
72
8847
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
73 def _pncomp2url(component):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
74 component = urllib.quote(component[:31], safe='') # We want to quote slashes
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
75 return component
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
76
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def test():
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 for url in ["index.html",
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
79 "bar/index.html",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
80 "/foo/bar/index.html",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
81 "/foo/bar/",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
82 "/"]:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
83 print `url`, '->', `url2pathname(url)`
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 for path in ["drive:",
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
85 "drive:dir:",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
86 "drive:dir:file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
87 "drive:file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
88 "file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
89 ":file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
90 ":dir:",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
91 ":dir:file"]:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
92 print `path`, '->', `pathname2url(path)`
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 if __name__ == '__main__':
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 test()