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 | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 16 | components = string.split(pathname, '/') |
| 17 | # Remove . and embedded .. |
| 18 | i = 0 |
| 19 | while i < len(components): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 20 | 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 Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 30 | if not components[0]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 31 | # Absolute unix path, don't start with colon |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 32 | rv = string.join(components[1:], ':') |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 33 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 34 | # 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 Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 40 | rv = ':' + string.join(components, ':') |
| 41 | # and finally unquote slashes and other funny characters |
| 42 | return urllib.unquote(rv) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 43 | |
| 44 | def pathname2url(pathname): |
| 45 | "convert mac pathname to /-delimited pathname" |
| 46 | if '/' in pathname: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 47 | raise RuntimeError, "Cannot convert pathname containing slashes" |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 48 | components = string.split(pathname, ':') |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 49 | # Remove empty first and/or last component |
| 50 | if components[0] == '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | del components[0] |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 52 | if components[-1] == '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 53 | del components[-1] |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 54 | # Replace empty string ('::') by .. (will result in '/../' later) |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 55 | for i in range(len(components)): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 56 | if components[i] == '': |
| 57 | components[i] = '..' |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 58 | # Truncate names longer than 31 bytes |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 59 | components = map(_pncomp2url, components) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 60 | |
| 61 | if os.path.isabs(pathname): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 62 | return '/' + string.join(components, '/') |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 63 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | return string.join(components, '/') |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 65 | |
| 66 | def _pncomp2url(component): |
| 67 | component = urllib.quote(component[:31], safe='') # We want to quote slashes |
| 68 | return component |
| 69 | |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 70 | def test(): |
| 71 | for url in ["index.html", |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 72 | "bar/index.html", |
| 73 | "/foo/bar/index.html", |
| 74 | "/foo/bar/", |
| 75 | "/"]: |
| 76 | print `url`, '->', `url2pathname(url)` |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 77 | for path in ["drive:", |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 78 | "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 Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 86 | |
| 87 | if __name__ == '__main__': |
| 88 | test() |