Updates of recent changes to logging.
[python.git] / Lib / plat-riscos / rourl2path.py
blob7a8badf446320bf3f86a0cfff782d94305212059
1 """riscos specific module for conversion between pathnames and URLs.
2 Based on macurl2path.
3 Do not import directly, use urllib instead."""
5 import string
6 import urllib
7 import os
9 __all__ = ["url2pathname","pathname2url"]
11 __slash_dot = string.maketrans("/.", "./")
13 def url2pathname(url):
14 """OS-specific conversion from a relative URL of the 'file' scheme
15 to a file system path; not recommended for general use."""
16 tp = urllib.splittype(url)[0]
17 if tp and tp <> 'file':
18 raise RuntimeError, 'Cannot convert non-local URL to pathname'
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:
29 components[0] = '$'
30 # Remove . and embedded ..
31 i = 0
32 while i < len(components):
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]
38 i -= 1
39 elif components[i] == '..':
40 components[i] = '^'
41 i += 1
42 elif components[i] == '' and i > 0 and components[i-1] <> '':
43 del components[i]
44 else:
45 i += 1
46 components = map(lambda x: urllib.unquote(x).translate(__slash_dot), components)
47 return '.'.join(components)
49 def pathname2url(pathname):
50 """OS-specific conversion from a file system path to a relative URL
51 of the 'file' scheme; not recommended for general use."""
52 return urllib.quote('///' + pathname.translate(__slash_dot), "/$:")
54 def test():
55 for url in ["index.html",
56 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
57 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/../!Fresco/Welcome",
58 "../index.html",
59 "bar/index.html",
60 "/foo/bar/index.html",
61 "/foo/bar/",
62 "/"]:
63 print '%r -> %r' % (url, url2pathname(url))
64 print "*******************************************************"
65 for path in ["SCSI::SCSI4.$.Anwendung",
66 "PythonApp:Lib",
67 "PythonApp:Lib.rourl2path/py"]:
68 print '%r -> %r' % (path, pathname2url(path))
70 if __name__ == '__main__':
71 test()