Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 1 | """riscos specific module for conversion between pathnames and URLs. |
| 2 | Based on macurl2path. |
| 3 | Do not import directly, use urllib instead.""" |
| 4 | |
| 5 | import string |
| 6 | import urllib |
| 7 | import os |
| 8 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 9 | __all__ = ["url2pathname","pathname2url"] |
| 10 | |
| 11 | __slash_dot = string.maketrans("/.", "./") |
| 12 | |
| 13 | def url2pathname(url): |
Georg Brandl | c0b2473 | 2005-12-26 22:53:56 +0000 | [diff] [blame] | 14 | """OS-specific conversion from a relative URL of the 'file' scheme |
| 15 | to a file system path; not recommended for general use.""" |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 16 | tp = urllib.splittype(url)[0] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 17 | if tp and tp <> 'file': |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 18 | raise RuntimeError, 'Cannot convert non-local URL to pathname' |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 19 | # Turn starting /// into /, an empty hostname means current host |
| 20 | if url[:3] == '///': |
| 21 | url = url[2:] |
| 22 | elif url[:2] == '//': |
| 23 | raise RuntimeError, 'Cannot convert non-local URL to pathname' |
| 24 | components = string.split(url, '/') |
| 25 | if not components[0]: |
| 26 | if '$' in components: |
| 27 | del components[0] |
| 28 | else: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 29 | components[0] = '$' |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 30 | # Remove . and embedded .. |
| 31 | i = 0 |
| 32 | while i < len(components): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 33 | if components[i] == '.': |
| 34 | del components[i] |
| 35 | elif components[i] == '..' and i > 0 and \ |
| 36 | components[i-1] not in ('', '..'): |
| 37 | del components[i-1:i+1] |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 38 | i -= 1 |
| 39 | elif components[i] == '..': |
| 40 | components[i] = '^' |
| 41 | i += 1 |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 42 | elif components[i] == '' and i > 0 and components[i-1] <> '': |
| 43 | del components[i] |
| 44 | else: |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 45 | i += 1 |
| 46 | components = map(lambda x: urllib.unquote(x).translate(__slash_dot), components) |
| 47 | return '.'.join(components) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 48 | |
| 49 | def pathname2url(pathname): |
Georg Brandl | c0b2473 | 2005-12-26 22:53:56 +0000 | [diff] [blame] | 50 | """OS-specific conversion from a file system path to a relative URL |
| 51 | of the 'file' scheme; not recommended for general use.""" |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 52 | return urllib.quote('///' + pathname.translate(__slash_dot), "/$:") |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 53 | |
| 54 | def test(): |
| 55 | for url in ["index.html", |
| 56 | "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome", |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 57 | "/SCSI::SCSI4/$/Anwendung/Comm/Apps/../!Fresco/Welcome", |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 58 | "../index.html", |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 59 | "bar/index.html", |
| 60 | "/foo/bar/index.html", |
| 61 | "/foo/bar/", |
| 62 | "/"]: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 63 | print '%r -> %r' % (url, url2pathname(url)) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 64 | print "*******************************************************" |
| 65 | for path in ["SCSI::SCSI4.$.Anwendung", |
| 66 | "PythonApp:Lib", |
| 67 | "PythonApp:Lib.rourl2path/py"]: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 68 | print '%r -> %r' % (path, pathname2url(path)) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 69 | |
| 70 | if __name__ == '__main__': |
| 71 | test() |