blob: 4c5ae64572e26ab59f299ce83b16ca42827b44a2 [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):
Georg Brandlc0b24732005-12-26 22:53:56 +000011 """OS-specific conversion from a relative URL of the 'file' scheme
12 to a file system path; not recommended for general use."""
Guido van Rossum1acbffe1996-05-28 23:52:06 +000013 #
14 # XXXX The .. handling should be fixed...
15 #
16 tp = urllib.splittype(pathname)[0]
Fred Drake8152d322000-12-12 23:20:45 +000017 if tp and tp != 'file':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000018 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossum116b31b1999-06-01 14:36:56 +000019 # Turn starting /// into /, an empty hostname means current host
20 if pathname[:3] == '///':
Tim Peters07e99cb2001-01-14 23:47:14 +000021 pathname = pathname[2:]
Guido van Rossum116b31b1999-06-01 14:36:56 +000022 elif pathname[:2] == '//':
23 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000024 components = pathname.split('/')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000025 # Remove . and embedded ..
26 i = 0
27 while i < len(components):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000028 if components[i] == '.':
29 del components[i]
30 elif components[i] == '..' and i > 0 and \
31 components[i-1] not in ('', '..'):
32 del components[i-1:i+1]
33 i = i-1
Fred Drake8152d322000-12-12 23:20:45 +000034 elif components[i] == '' and i > 0 and components[i-1] != '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000035 del components[i]
36 else:
37 i = i+1
Guido van Rossum1acbffe1996-05-28 23:52:06 +000038 if not components[0]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000039 # Absolute unix path, don't start with colon
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000040 rv = ':'.join(components[1:])
Guido van Rossum1acbffe1996-05-28 23:52:06 +000041 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000042 # relative unix path, start with colon. First replace
43 # leading .. by empty strings (giving ::file)
44 i = 0
45 while i < len(components) and components[i] == '..':
46 components[i] = ''
47 i = i + 1
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000048 rv = ':' + ':'.join(components)
Guido van Rossum4ff6d271998-08-06 13:37:21 +000049 # and finally unquote slashes and other funny characters
50 return urllib.unquote(rv)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000051
52def pathname2url(pathname):
Georg Brandlc0b24732005-12-26 22:53:56 +000053 """OS-specific conversion from a file system path to a relative URL
54 of the 'file' scheme; not recommended for general use."""
Guido van Rossum1acbffe1996-05-28 23:52:06 +000055 if '/' in pathname:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000056 raise RuntimeError, "Cannot convert pathname containing slashes"
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000057 components = pathname.split(':')
Guido van Rossum32f92ca1997-05-20 16:00:07 +000058 # Remove empty first and/or last component
59 if components[0] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000060 del components[0]
Guido van Rossum32f92ca1997-05-20 16:00:07 +000061 if components[-1] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000062 del components[-1]
Guido van Rossum1acbffe1996-05-28 23:52:06 +000063 # Replace empty string ('::') by .. (will result in '/../' later)
Guido van Rossum32f92ca1997-05-20 16:00:07 +000064 for i in range(len(components)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000065 if components[i] == '':
66 components[i] = '..'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000067 # Truncate names longer than 31 bytes
Guido van Rossum4ff6d271998-08-06 13:37:21 +000068 components = map(_pncomp2url, components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000069
70 if os.path.isabs(pathname):
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000071 return '/' + '/'.join(components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000072 else:
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000073 return '/'.join(components)
Tim Peters07e99cb2001-01-14 23:47:14 +000074
Guido van Rossum4ff6d271998-08-06 13:37:21 +000075def _pncomp2url(component):
Tim Peters07e99cb2001-01-14 23:47:14 +000076 component = urllib.quote(component[:31], safe='') # We want to quote slashes
77 return component
78
Guido van Rossum1acbffe1996-05-28 23:52:06 +000079def test():
80 for url in ["index.html",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000081 "bar/index.html",
82 "/foo/bar/index.html",
83 "/foo/bar/",
84 "/"]:
Walter Dörwald70a6b492004-02-12 17:35:32 +000085 print '%r -> %r' % (url, url2pathname(url))
Guido van Rossum1acbffe1996-05-28 23:52:06 +000086 for path in ["drive:",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000087 "drive:dir:",
88 "drive:dir:file",
89 "drive:file",
90 "file",
91 ":file",
92 ":dir:",
93 ":dir:file"]:
Walter Dörwald70a6b492004-02-12 17:35:32 +000094 print '%r -> %r' % (path, pathname2url(path))
Guido van Rossum1acbffe1996-05-28 23:52:06 +000095
96if __name__ == '__main__':
97 test()