blob: 7694f1c3ff3ce3bde0447ff96bea752ba5e651ed [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
7import os
8
Guido van Rossume2ae77b2001-10-24 20:42:55 +00009__all__ = ["url2pathname","pathname2url"]
10
11__slash_dot = string.maketrans("/.", "./")
12
13def url2pathname(url):
Georg Brandlc0b24732005-12-26 22:53:56 +000014 """OS-specific conversion from a relative URL of the 'file' scheme
15 to a file system path; not recommended for general use."""
Guido van Rossume2ae77b2001-10-24 20:42:55 +000016 tp = urllib.splittype(url)[0]
Brett Cannon9ca0eca2006-08-25 01:00:47 +000017 if tp and tp != 'file':
Tim Peters683ecc72001-07-02 04:59:35 +000018 raise RuntimeError, 'Cannot convert non-local URL to pathname'
Guido van Rossume2ae77b2001-10-24 20:42:55 +000019 # 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'
Neal Norwitz9d72bb42007-04-17 08:48:32 +000024 components = url.split('/')
Guido van Rossume2ae77b2001-10-24 20:42:55 +000025 if not components[0]:
26 if '$' in components:
27 del components[0]
28 else:
Tim Peters182b5ac2004-07-18 06:16:08 +000029 components[0] = '$'
Guido van Rossum228d8072001-03-02 05:58:11 +000030 # Remove . and embedded ..
31 i = 0
32 while i < len(components):
Tim Peters683ecc72001-07-02 04:59:35 +000033 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 Rossume2ae77b2001-10-24 20:42:55 +000038 i -= 1
39 elif components[i] == '..':
40 components[i] = '^'
41 i += 1
Brett Cannon0fc91392006-08-25 01:06:13 +000042 elif components[i] == '' and i > 0 and components[i-1] != '':
Tim Peters683ecc72001-07-02 04:59:35 +000043 del components[i]
44 else:
Guido van Rossume2ae77b2001-10-24 20:42:55 +000045 i += 1
46 components = map(lambda x: urllib.unquote(x).translate(__slash_dot), components)
47 return '.'.join(components)
Guido van Rossum228d8072001-03-02 05:58:11 +000048
49def pathname2url(pathname):
Georg Brandlc0b24732005-12-26 22:53:56 +000050 """OS-specific conversion from a file system path to a relative URL
51 of the 'file' scheme; not recommended for general use."""
Guido van Rossume2ae77b2001-10-24 20:42:55 +000052 return urllib.quote('///' + pathname.translate(__slash_dot), "/$:")
Guido van Rossum228d8072001-03-02 05:58:11 +000053
54def test():
55 for url in ["index.html",
56 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
Guido van Rossume2ae77b2001-10-24 20:42:55 +000057 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/../!Fresco/Welcome",
Guido van Rossum228d8072001-03-02 05:58:11 +000058 "../index.html",
Tim Peters683ecc72001-07-02 04:59:35 +000059 "bar/index.html",
60 "/foo/bar/index.html",
61 "/foo/bar/",
62 "/"]:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000063 print('%r -> %r' % (url, url2pathname(url)))
64 print("*******************************************************")
Guido van Rossume2ae77b2001-10-24 20:42:55 +000065 for path in ["SCSI::SCSI4.$.Anwendung",
66 "PythonApp:Lib",
67 "PythonApp:Lib.rourl2path/py"]:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000068 print('%r -> %r' % (path, pathname2url(path)))
Guido van Rossum228d8072001-03-02 05:58:11 +000069
70if __name__ == '__main__':
71 test()