Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +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 | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 5 | import urllib |
| 6 | import os |
| 7 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 8 | __all__ = ["url2pathname","pathname2url"] |
| 9 | |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 10 | def url2pathname(pathname): |
| 11 | "Convert /-delimited pathname to mac pathname" |
| 12 | # |
| 13 | # XXXX The .. handling should be fixed... |
| 14 | # |
| 15 | tp = urllib.splittype(pathname)[0] |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 16 | if tp and tp != 'file': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 17 | raise RuntimeError, 'Cannot convert non-local URL to pathname' |
Guido van Rossum | 116b31b | 1999-06-01 14:36:56 +0000 | [diff] [blame] | 18 | # Turn starting /// into /, an empty hostname means current host |
| 19 | if pathname[:3] == '///': |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 20 | pathname = pathname[2:] |
Guido van Rossum | 116b31b | 1999-06-01 14:36:56 +0000 | [diff] [blame] | 21 | elif pathname[:2] == '//': |
| 22 | raise RuntimeError, 'Cannot convert non-local URL to pathname' |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 23 | components = pathname.split('/') |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 24 | # Remove . and embedded .. |
| 25 | i = 0 |
| 26 | while i < len(components): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 27 | if components[i] == '.': |
| 28 | del components[i] |
| 29 | elif components[i] == '..' and i > 0 and \ |
| 30 | components[i-1] not in ('', '..'): |
| 31 | del components[i-1:i+1] |
| 32 | i = i-1 |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 33 | elif components[i] == '' and i > 0 and components[i-1] != '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 34 | del components[i] |
| 35 | else: |
| 36 | i = i+1 |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 37 | if not components[0]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 38 | # Absolute unix path, don't start with colon |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 39 | rv = ':'.join(components[1:]) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 40 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 41 | # relative unix path, start with colon. First replace |
| 42 | # leading .. by empty strings (giving ::file) |
| 43 | i = 0 |
| 44 | while i < len(components) and components[i] == '..': |
| 45 | components[i] = '' |
| 46 | i = i + 1 |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 47 | rv = ':' + ':'.join(components) |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 48 | # and finally unquote slashes and other funny characters |
| 49 | return urllib.unquote(rv) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 50 | |
| 51 | def pathname2url(pathname): |
| 52 | "convert mac pathname to /-delimited pathname" |
| 53 | if '/' in pathname: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 54 | raise RuntimeError, "Cannot convert pathname containing slashes" |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 55 | components = pathname.split(':') |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 56 | # Remove empty first and/or last component |
| 57 | if components[0] == '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 58 | del components[0] |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 59 | if components[-1] == '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 60 | del components[-1] |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 61 | # Replace empty string ('::') by .. (will result in '/../' later) |
Guido van Rossum | 32f92ca | 1997-05-20 16:00:07 +0000 | [diff] [blame] | 62 | for i in range(len(components)): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 63 | if components[i] == '': |
| 64 | components[i] = '..' |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 65 | # Truncate names longer than 31 bytes |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 66 | components = map(_pncomp2url, components) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 67 | |
| 68 | if os.path.isabs(pathname): |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 69 | return '/' + '/'.join(components) |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 70 | else: |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 71 | return '/'.join(components) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 4ff6d27 | 1998-08-06 13:37:21 +0000 | [diff] [blame] | 73 | def _pncomp2url(component): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 74 | component = urllib.quote(component[:31], safe='') # We want to quote slashes |
| 75 | return component |
| 76 | |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 77 | def test(): |
| 78 | for url in ["index.html", |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 79 | "bar/index.html", |
| 80 | "/foo/bar/index.html", |
| 81 | "/foo/bar/", |
| 82 | "/"]: |
| 83 | print `url`, '->', `url2pathname(url)` |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 84 | for path in ["drive:", |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 85 | "drive:dir:", |
| 86 | "drive:dir:file", |
| 87 | "drive:file", |
| 88 | "file", |
| 89 | ":file", |
| 90 | ":dir:", |
| 91 | ":dir:file"]: |
| 92 | print `path`, '->', `pathname2url(path)` |
Guido van Rossum | 1acbffe | 1996-05-28 23:52:06 +0000 | [diff] [blame] | 93 | |
| 94 | if __name__ == '__main__': |
| 95 | test() |