Files for 2.1b1 distribution.
[python/dscho.git] / Lib / plat-riscos / rourl2path.py
blob033e621165d19d097bf46ff7b74100c6821e7c98
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 def url2pathname(pathname):
10 "Convert /-delimited pathname to mac pathname"
12 # XXXX The .. handling should be fixed...
14 tp = urllib.splittype(pathname)[0]
15 if tp and tp <> 'file':
16 raise RuntimeError, 'Cannot convert non-local URL to pathname'
17 components = string.split(pathname, '/')
18 # Remove . and embedded ..
19 i = 0
20 while i < len(components):
21 if components[i] == '.':
22 del components[i]
23 elif components[i] == '..' and i > 0 and \
24 components[i-1] not in ('', '..'):
25 del components[i-1:i+1]
26 i = i-1
27 elif components[i] == '' and i > 0 and components[i-1] <> '':
28 del components[i]
29 else:
30 if components[i]<>'..' and string.find(components[i], '.')<>-1 :
31 components[i] = string.join(string.split(components[i],'.'),'/')
32 i = i+1
33 if not components[0]:
34 # Absolute unix path, don't start with colon
35 return string.join(components[1:], '.')
36 else:
37 # relative unix path, start with colon. First replace
38 # leading .. by empty strings (giving ::file)
39 i = 0
40 while i < len(components) and components[i] == '..':
41 components[i] = '^'
42 i = i + 1
43 return string.join(components, '.')
45 def pathname2url(pathname):
46 "convert mac pathname to /-delimited pathname"
47 if '/' in pathname:
48 raise RuntimeError, "Cannot convert pathname containing slashes"
49 components = string.split(pathname, ':')
50 # Replace empty string ('::') by .. (will result in '/../' later)
51 for i in range(1, len(components)):
52 if components[i] == '':
53 components[i] = '..'
54 # Truncate names longer than 31 bytes
55 components = map(lambda x: x[:31], components)
57 if os.path.isabs(pathname):
58 return '/' + string.join(components, '/')
59 else:
60 return string.join(components, '/')
62 def test():
63 for url in ["index.html",
64 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
65 "../index.html",
66 "bar/index.html",
67 "/foo/bar/index.html",
68 "/foo/bar/",
69 "/"]:
70 print `url`, '->', `url2pathname(url)`
71 for path in ["drive:",
72 "drive:dir:",
73 "drive:dir:file",
74 "drive:file",
75 "file",
76 ":file",
77 ":dir:",
78 ":dir:file"]:
79 print `path`, '->', `pathname2url(path)`
81 if __name__ == '__main__':
82 test()