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