blob: a8be19884ce2dd8143835abe91b79b6b9825510d [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
5import string
6import urllib
7import os
8
Skip Montanaro17ab1232001-01-24 06:27:27 +00009__all__ = ["url2pathname","pathname2url"]
10
Guido van Rossum1acbffe1996-05-28 23:52:06 +000011def url2pathname(pathname):
12 "Convert /-delimited pathname to mac pathname"
13 #
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'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000024 components = string.split(pathname, '/')
25 # 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
Guido van Rossum4ff6d271998-08-06 13:37:21 +000040 rv = string.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
Guido van Rossum4ff6d271998-08-06 13:37:21 +000048 rv = ':' + string.join(components, ':')
49 # 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):
53 "convert mac pathname to /-delimited pathname"
54 if '/' in pathname:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000055 raise RuntimeError, "Cannot convert pathname containing slashes"
Guido van Rossum1acbffe1996-05-28 23:52:06 +000056 components = string.split(pathname, ':')
Guido van Rossum32f92ca1997-05-20 16:00:07 +000057 # Remove empty first and/or last component
58 if components[0] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000059 del components[0]
Guido van Rossum32f92ca1997-05-20 16:00:07 +000060 if components[-1] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000061 del components[-1]
Guido van Rossum1acbffe1996-05-28 23:52:06 +000062 # Replace empty string ('::') by .. (will result in '/../' later)
Guido van Rossum32f92ca1997-05-20 16:00:07 +000063 for i in range(len(components)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 if components[i] == '':
65 components[i] = '..'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000066 # Truncate names longer than 31 bytes
Guido van Rossum4ff6d271998-08-06 13:37:21 +000067 components = map(_pncomp2url, components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000068
69 if os.path.isabs(pathname):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000070 return '/' + string.join(components, '/')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000071 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000072 return string.join(components, '/')
Tim Peters07e99cb2001-01-14 23:47:14 +000073
Guido van Rossum4ff6d271998-08-06 13:37:21 +000074def _pncomp2url(component):
Tim Peters07e99cb2001-01-14 23:47:14 +000075 component = urllib.quote(component[:31], safe='') # We want to quote slashes
76 return component
77
Guido van Rossum1acbffe1996-05-28 23:52:06 +000078def test():
79 for url in ["index.html",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000080 "bar/index.html",
81 "/foo/bar/index.html",
82 "/foo/bar/",
83 "/"]:
84 print `url`, '->', `url2pathname(url)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000085 for path in ["drive:",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000086 "drive:dir:",
87 "drive:dir:file",
88 "drive:file",
89 "file",
90 ":file",
91 ":dir:",
92 ":dir:file"]:
93 print `path`, '->', `pathname2url(path)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000094
95if __name__ == '__main__':
96 test()