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