blob: c971edaa22dad13a1ffe4b57cff15255334ef45a [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
9def url2pathname(pathname):
10 "Convert /-delimited pathname to mac pathname"
11 #
12 # XXXX The .. handling should be fixed...
13 #
14 tp = urllib.splittype(pathname)[0]
15 if tp and tp <> 'file':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000016 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossum116b31b1999-06-01 14:36:56 +000017 # Turn starting /// into /, an empty hostname means current host
18 if pathname[:3] == '///':
19 pathname = pathname[2:]
20 elif pathname[:2] == '//':
21 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000022 components = string.split(pathname, '/')
23 # Remove . and embedded ..
24 i = 0
25 while i < len(components):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000026 if components[i] == '.':
27 del components[i]
28 elif components[i] == '..' and i > 0 and \
29 components[i-1] not in ('', '..'):
30 del components[i-1:i+1]
31 i = i-1
32 elif components[i] == '' and i > 0 and components[i-1] <> '':
33 del components[i]
34 else:
35 i = i+1
Guido van Rossum1acbffe1996-05-28 23:52:06 +000036 if not components[0]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000037 # Absolute unix path, don't start with colon
Guido van Rossum4ff6d271998-08-06 13:37:21 +000038 rv = string.join(components[1:], ':')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000039 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000040 # relative unix path, start with colon. First replace
41 # leading .. by empty strings (giving ::file)
42 i = 0
43 while i < len(components) and components[i] == '..':
44 components[i] = ''
45 i = i + 1
Guido van Rossum4ff6d271998-08-06 13:37:21 +000046 rv = ':' + string.join(components, ':')
47 # and finally unquote slashes and other funny characters
48 return urllib.unquote(rv)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000049
50def pathname2url(pathname):
51 "convert mac pathname to /-delimited pathname"
52 if '/' in pathname:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000053 raise RuntimeError, "Cannot convert pathname containing slashes"
Guido van Rossum1acbffe1996-05-28 23:52:06 +000054 components = string.split(pathname, ':')
Guido van Rossum32f92ca1997-05-20 16:00:07 +000055 # Remove empty first and/or last component
56 if components[0] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000057 del components[0]
Guido van Rossum32f92ca1997-05-20 16:00:07 +000058 if components[-1] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000059 del components[-1]
Guido van Rossum1acbffe1996-05-28 23:52:06 +000060 # Replace empty string ('::') by .. (will result in '/../' later)
Guido van Rossum32f92ca1997-05-20 16:00:07 +000061 for i in range(len(components)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000062 if components[i] == '':
63 components[i] = '..'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000064 # Truncate names longer than 31 bytes
Guido van Rossum4ff6d271998-08-06 13:37:21 +000065 components = map(_pncomp2url, components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000066
67 if os.path.isabs(pathname):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000068 return '/' + string.join(components, '/')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000069 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000070 return string.join(components, '/')
Guido van Rossum4ff6d271998-08-06 13:37:21 +000071
72def _pncomp2url(component):
73 component = urllib.quote(component[:31], safe='') # We want to quote slashes
74 return component
75
Guido van Rossum1acbffe1996-05-28 23:52:06 +000076def test():
77 for url in ["index.html",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000078 "bar/index.html",
79 "/foo/bar/index.html",
80 "/foo/bar/",
81 "/"]:
82 print `url`, '->', `url2pathname(url)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000083 for path in ["drive:",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000084 "drive:dir:",
85 "drive:dir:file",
86 "drive:file",
87 "file",
88 ":file",
89 ":dir:",
90 ":dir:file"]:
91 print `path`, '->', `pathname2url(path)`
Guido van Rossum1acbffe1996-05-28 23:52:06 +000092
93if __name__ == '__main__':
94 test()