blob: 7d273bc61064b6fa7b419c7b8d811078abcd7c88 [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 Rossum1acbffe1996-05-28 23:52:06 +000016 components = string.split(pathname, '/')
17 # Remove . and embedded ..
18 i = 0
19 while i < len(components):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000020 if components[i] == '.':
21 del components[i]
22 elif components[i] == '..' and i > 0 and \
23 components[i-1] not in ('', '..'):
24 del components[i-1:i+1]
25 i = i-1
26 elif components[i] == '' and i > 0 and components[i-1] <> '':
27 del components[i]
28 else:
29 i = i+1
Guido van Rossum1acbffe1996-05-28 23:52:06 +000030 if not components[0]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000031 # Absolute unix path, don't start with colon
Guido van Rossum4ff6d271998-08-06 13:37:21 +000032 rv = string.join(components[1:], ':')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000033 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000034 # relative unix path, start with colon. First replace
35 # leading .. by empty strings (giving ::file)
36 i = 0
37 while i < len(components) and components[i] == '..':
38 components[i] = ''
39 i = i + 1
Guido van Rossum4ff6d271998-08-06 13:37:21 +000040 rv = ':' + string.join(components, ':')
41 # and finally unquote slashes and other funny characters
42 return urllib.unquote(rv)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000043
44def pathname2url(pathname):
45 "convert mac pathname to /-delimited pathname"
46 if '/' in pathname:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000047 raise RuntimeError, "Cannot convert pathname containing slashes"
Guido van Rossum1acbffe1996-05-28 23:52:06 +000048 components = string.split(pathname, ':')
Guido van Rossum32f92ca1997-05-20 16:00:07 +000049 # Remove empty first and/or last component
50 if components[0] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000051 del components[0]
Guido van Rossum32f92ca1997-05-20 16:00:07 +000052 if components[-1] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000053 del components[-1]
Guido van Rossum1acbffe1996-05-28 23:52:06 +000054 # Replace empty string ('::') by .. (will result in '/../' later)
Guido van Rossum32f92ca1997-05-20 16:00:07 +000055 for i in range(len(components)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000056 if components[i] == '':
57 components[i] = '..'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000058 # Truncate names longer than 31 bytes
Guido van Rossum4ff6d271998-08-06 13:37:21 +000059 components = map(_pncomp2url, components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000060
61 if os.path.isabs(pathname):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000062 return '/' + string.join(components, '/')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000063 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 return string.join(components, '/')
Guido van Rossum4ff6d271998-08-06 13:37:21 +000065
66def _pncomp2url(component):
67 component = urllib.quote(component[:31], safe='') # We want to quote slashes
68 return component
69
Guido van Rossum1acbffe1996-05-28 23:52:06 +000070def test():
71 for url in ["index.html",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000072 "bar/index.html",
73 "/foo/bar/index.html",
74 "/foo/bar/",
75 "/"]:
76 print `url`, '->', `url2pathname(url)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000077 for path in ["drive:",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000078 "drive:dir:",
79 "drive:dir:file",
80 "drive:file",
81 "file",
82 ":file",
83 ":dir:",
84 ":dir:file"]:
85 print `path`, '->', `pathname2url(path)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000086
87if __name__ == '__main__':
88 test()