Guido van Rossum | 008ec68 | 1996-10-15 14:40:41 +0000 | [diff] [blame] | 1 | """Mac specific module for conversion between pathnames and URLs. |
| 2 | Do not import directly, use urllib instead.""" |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 3 | |
| 4 | import string |
| 5 | import urllib |
| 6 | import os |
| 7 | |
| 8 | def 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 Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 15 | raise RuntimeError, 'Cannot convert non-local URL to pathname' |
Guido van Rossum | 116b31b | 1999-06-01 14:36:56 +0000 | [diff] [blame] | 16 | # 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 Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 21 | components = string.split(pathname, '/') |
| 22 | # Remove . and embedded .. |
| 23 | i = 0 |
| 24 | while i < len(components): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 25 | 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 Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 35 | if not components[0]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 36 | # Absolute unix path, don't start with colon |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 37 | rv = string.join(components[1:], ':') |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 38 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 39 | # 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 Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 45 | rv = ':' + string.join(components, ':') |
| 46 | # and finally unquote slashes and other funny characters |
| 47 | return urllib.unquote(rv) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 48 | |
| 49 | def pathname2url(pathname): |
| 50 | "convert mac pathname to /-delimited pathname" |
| 51 | if '/' in pathname: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 52 | raise RuntimeError, "Cannot convert pathname containing slashes" |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 53 | components = string.split(pathname, ':') |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 54 | # Remove empty first and/or last component |
| 55 | if components[0] == '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 56 | del components[0] |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 57 | if components[-1] == '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 58 | del components[-1] |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 59 | # Replace empty string ('::') by .. (will result in '/../' later) |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 60 | for i in range(len(components)): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 61 | if components[i] == '': |
| 62 | components[i] = '..' |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 63 | # Truncate names longer than 31 bytes |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 64 | components = map(_pncomp2url, components) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 65 | |
| 66 | if os.path.isabs(pathname): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 67 | return '/' + string.join(components, '/') |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 68 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 69 | return string.join(components, '/') |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 70 | |
| 71 | def _pncomp2url(component): |
| 72 | component = urllib.quote(component[:31], safe='') # We want to quote slashes |
| 73 | return component |
| 74 | |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 75 | def test(): |
| 76 | for url in ["index.html", |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | "bar/index.html", |
| 78 | "/foo/bar/index.html", |
| 79 | "/foo/bar/", |
| 80 | "/"]: |
| 81 | print `url`, '->', `url2pathname(url)` |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 82 | for path in ["drive:", |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 83 | "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 Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 91 | |
| 92 | if __name__ == '__main__': |
| 93 | test() |