blob: 5d5386e65cc2d5d776b20746fd0ceeaa7c9f798e [file] [log] [blame]
Guido van Rossum228d8072001-03-02 05:58:11 +00001"""riscos specific module for conversion between pathnames and URLs.
2Based on macurl2path.
3Do not import directly, use urllib instead."""
4
5import string
6import urllib
Guido van Rossum228d8072001-03-02 05:58:11 +00007
Guido van Rossume2ae77b2001-10-24 20:42:55 +00008__all__ = ["url2pathname","pathname2url"]
9
10__slash_dot = string.maketrans("/.", "./")
11
12def url2pathname(url):
Georg Brandlc0b24732005-12-26 22:53:56 +000013 """OS-specific conversion from a relative URL of the 'file' scheme
14 to a file system path; not recommended for general use."""
Guido van Rossume2ae77b2001-10-24 20:42:55 +000015 tp = urllib.splittype(url)[0]
Guido van Rossum228d8072001-03-02 05:58:11 +000016 if tp and tp <> 'file':
Tim Peters683ecc72001-07-02 04:59:35 +000017 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossume2ae77b2001-10-24 20:42:55 +000018 # Turn starting /// into /, an empty hostname means current host
19 if url[:3] == '///':
20 url = url[2:]
21 elif url[:2] == '//':
22 raise RuntimeError, 'Cannot convert non-local URL to pathname'
23 components = string.split(url, '/')
24 if not components[0]:
25 if '$' in components:
26 del components[0]
27 else:
Tim Peters182b5ac2004-07-18 06:16:08 +000028 components[0] = '$'
Guido van Rossum228d8072001-03-02 05:58:11 +000029 # Remove . and embedded ..
30 i = 0
31 while i < len(components):
Tim Peters683ecc72001-07-02 04:59:35 +000032 if components[i] == '.':
33 del components[i]
34 elif components[i] == '..' and i > 0 and \
35 components[i-1] not in ('', '..'):
36 del components[i-1:i+1]
Guido van Rossume2ae77b2001-10-24 20:42:55 +000037 i -= 1
38 elif components[i] == '..':
39 components[i] = '^'
40 i += 1
Tim Peters683ecc72001-07-02 04:59:35 +000041 elif components[i] == '' and i > 0 and components[i-1] <> '':
42 del components[i]
43 else:
Guido van Rossume2ae77b2001-10-24 20:42:55 +000044 i += 1
45 components = map(lambda x: urllib.unquote(x).translate(__slash_dot), components)
46 return '.'.join(components)
Guido van Rossum228d8072001-03-02 05:58:11 +000047
48def pathname2url(pathname):
Georg Brandlc0b24732005-12-26 22:53:56 +000049 """OS-specific conversion from a file system path to a relative URL
50 of the 'file' scheme; not recommended for general use."""
Guido van Rossume2ae77b2001-10-24 20:42:55 +000051 return urllib.quote('///' + pathname.translate(__slash_dot), "/$:")
Guido van Rossum228d8072001-03-02 05:58:11 +000052
53def test():
54 for url in ["index.html",
55 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
Guido van Rossume2ae77b2001-10-24 20:42:55 +000056 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/../!Fresco/Welcome",
Guido van Rossum228d8072001-03-02 05:58:11 +000057 "../index.html",
Tim Peters683ecc72001-07-02 04:59:35 +000058 "bar/index.html",
59 "/foo/bar/index.html",
60 "/foo/bar/",
61 "/"]:
Walter Dörwald70a6b492004-02-12 17:35:32 +000062 print '%r -> %r' % (url, url2pathname(url))
Guido van Rossume2ae77b2001-10-24 20:42:55 +000063 print "*******************************************************"
64 for path in ["SCSI::SCSI4.$.Anwendung",
65 "PythonApp:Lib",
66 "PythonApp:Lib.rourl2path/py"]:
Walter Dörwald70a6b492004-02-12 17:35:32 +000067 print '%r -> %r' % (path, pathname2url(path))
Guido van Rossum228d8072001-03-02 05:58:11 +000068
69if __name__ == '__main__':
70 test()