blob: 4c43d2110f54cd6ad8dd60e3744ba9e1b138cc8e [file] [log] [blame]
Guido van Rossum008ec681996-10-15 14:40:41 +00001"""Mac specific module for conversion between pathnames and URLs.
2Do not import directly, use urllib instead."""
Guido van Rossum1acbffe1996-05-28 23:52:06 +00003
4import string
5import urllib
6import os
7
8def url2pathname(pathname):
9 "Convert /-delimited pathname to mac pathname"
10 #
11 # XXXX The .. handling should be fixed...
12 #
13 tp = urllib.splittype(pathname)[0]
14 if tp and tp <> 'file':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000015 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossum116b31b1999-06-01 14:36:56 +000016 # Turn starting /// into /, an empty hostname means current host
17 if pathname[:3] == '///':
18 pathname = pathname[2:]
19 elif pathname[:2] == '//':
20 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000021 components = string.split(pathname, '/')
22 # Remove . and embedded ..
23 i = 0
24 while i < len(components):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000025 if components[i] == '.':
26 del components[i]
27 elif components[i] == '..' and i > 0 and \
28 components[i-1] not in ('', '..'):
29 del components[i-1:i+1]
30 i = i-1
31 elif components[i] == '' and i > 0 and components[i-1] <> '':
32 del components[i]
33 else:
34 i = i+1
Guido van Rossum1acbffe1996-05-28 23:52:06 +000035 if not components[0]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000036 # Absolute unix path, don't start with colon
Guido van Rossum4ff6d271998-08-06 13:37:21 +000037 rv = string.join(components[1:], ':')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000038 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000039 # relative unix path, start with colon. First replace
40 # leading .. by empty strings (giving ::file)
41 i = 0
42 while i < len(components) and components[i] == '..':
43 components[i] = ''
44 i = i + 1
Guido van Rossum4ff6d271998-08-06 13:37:21 +000045 rv = ':' + string.join(components, ':')
46 # and finally unquote slashes and other funny characters
47 return urllib.unquote(rv)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000048
49def pathname2url(pathname):
50 "convert mac pathname to /-delimited pathname"
51 if '/' in pathname:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000052 raise RuntimeError, "Cannot convert pathname containing slashes"
Guido van Rossum1acbffe1996-05-28 23:52:06 +000053 components = string.split(pathname, ':')
Guido van Rossum32f92ca1997-05-20 16:00:07 +000054 # Remove empty first and/or last component
55 if components[0] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000056 del components[0]
Guido van Rossum32f92ca1997-05-20 16:00:07 +000057 if components[-1] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000058 del components[-1]
Guido van Rossum1acbffe1996-05-28 23:52:06 +000059 # Replace empty string ('::') by .. (will result in '/../' later)
Guido van Rossum32f92ca1997-05-20 16:00:07 +000060 for i in range(len(components)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000061 if components[i] == '':
62 components[i] = '..'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000063 # Truncate names longer than 31 bytes
Guido van Rossum4ff6d271998-08-06 13:37:21 +000064 components = map(_pncomp2url, components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000065
66 if os.path.isabs(pathname):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000067 return '/' + string.join(components, '/')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000068 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000069 return string.join(components, '/')
Guido van Rossum4ff6d271998-08-06 13:37:21 +000070
71def _pncomp2url(component):
72 component = urllib.quote(component[:31], safe='') # We want to quote slashes
73 return component
74
Guido van Rossum1acbffe1996-05-28 23:52:06 +000075def test():
76 for url in ["index.html",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000077 "bar/index.html",
78 "/foo/bar/index.html",
79 "/foo/bar/",
80 "/"]:
81 print `url`, '->', `url2pathname(url)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000082 for path in ["drive:",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000083 "drive:dir:",
84 "drive:dir:file",
85 "drive:file",
86 "file",
87 ":file",
88 ":dir:",
89 ":dir:file"]:
90 print `path`, '->', `pathname2url(path)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000091
92if __name__ == '__main__':
93 test()