blob: 3c1acc02b774fa5a8357658cf4ce5f479b2d5f2d [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""Macintosh-specific module for conversion between pathnames and URLs.
2
3Do not import directly; use urllib instead."""
Guido van Rossum1acbffe1996-05-28 23:52:06 +00004
Guido van Rossum1acbffe1996-05-28 23:52:06 +00005import urllib
6import os
7
Skip Montanaro17ab1232001-01-24 06:27:27 +00008__all__ = ["url2pathname","pathname2url"]
9
Guido van Rossum1acbffe1996-05-28 23:52:06 +000010def url2pathname(pathname):
11 "Convert /-delimited pathname to mac pathname"
12 #
13 # XXXX The .. handling should be fixed...
14 #
15 tp = urllib.splittype(pathname)[0]
Fred Drake8152d322000-12-12 23:20:45 +000016 if tp and tp != 'file':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000017 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossum116b31b1999-06-01 14:36:56 +000018 # Turn starting /// into /, an empty hostname means current host
19 if pathname[:3] == '///':
Tim Peters07e99cb2001-01-14 23:47:14 +000020 pathname = pathname[2:]
Guido van Rossum116b31b1999-06-01 14:36:56 +000021 elif pathname[:2] == '//':
22 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000023 components = pathname.split('/')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000024 # Remove . and embedded ..
25 i = 0
26 while i < len(components):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000027 if components[i] == '.':
28 del components[i]
29 elif components[i] == '..' and i > 0 and \
30 components[i-1] not in ('', '..'):
31 del components[i-1:i+1]
32 i = i-1
Fred Drake8152d322000-12-12 23:20:45 +000033 elif components[i] == '' and i > 0 and components[i-1] != '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000034 del components[i]
35 else:
36 i = i+1
Guido van Rossum1acbffe1996-05-28 23:52:06 +000037 if not components[0]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000038 # Absolute unix path, don't start with colon
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000039 rv = ':'.join(components[1:])
Guido van Rossum1acbffe1996-05-28 23:52:06 +000040 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000041 # relative unix path, start with colon. First replace
42 # leading .. by empty strings (giving ::file)
43 i = 0
44 while i < len(components) and components[i] == '..':
45 components[i] = ''
46 i = i + 1
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000047 rv = ':' + ':'.join(components)
Guido van Rossum4ff6d271998-08-06 13:37:21 +000048 # and finally unquote slashes and other funny characters
49 return urllib.unquote(rv)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000050
51def pathname2url(pathname):
52 "convert mac pathname to /-delimited pathname"
53 if '/' in pathname:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000054 raise RuntimeError, "Cannot convert pathname containing slashes"
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000055 components = pathname.split(':')
Guido van Rossum32f92ca1997-05-20 16:00:07 +000056 # Remove empty first and/or last component
57 if components[0] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000058 del components[0]
Guido van Rossum32f92ca1997-05-20 16:00:07 +000059 if components[-1] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000060 del components[-1]
Guido van Rossum1acbffe1996-05-28 23:52:06 +000061 # Replace empty string ('::') by .. (will result in '/../' later)
Guido van Rossum32f92ca1997-05-20 16:00:07 +000062 for i in range(len(components)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000063 if components[i] == '':
64 components[i] = '..'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000065 # Truncate names longer than 31 bytes
Guido van Rossum4ff6d271998-08-06 13:37:21 +000066 components = map(_pncomp2url, components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000067
68 if os.path.isabs(pathname):
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000069 return '/' + '/'.join(components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000070 else:
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000071 return '/'.join(components)
Tim Peters07e99cb2001-01-14 23:47:14 +000072
Guido van Rossum4ff6d271998-08-06 13:37:21 +000073def _pncomp2url(component):
Tim Peters07e99cb2001-01-14 23:47:14 +000074 component = urllib.quote(component[:31], safe='') # We want to quote slashes
75 return component
76
Guido van Rossum1acbffe1996-05-28 23:52:06 +000077def test():
78 for url in ["index.html",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 "bar/index.html",
80 "/foo/bar/index.html",
81 "/foo/bar/",
82 "/"]:
83 print `url`, '->', `url2pathname(url)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000084 for path in ["drive:",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000085 "drive:dir:",
86 "drive:dir:file",
87 "drive:file",
88 "file",
89 ":file",
90 ":dir:",
91 ":dir:file"]:
92 print `path`, '->', `pathname2url(path)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000093
94if __name__ == '__main__':
95 test()