1 """riscos specific module for conversion between pathnames and URLs.
3 Do not import directly, use urllib instead."""
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 ..
20 while i
< len(components
):
21 if components
[i
] == '.':
23 elif components
[i
] == '..' and i
> 0 and \
24 components
[i
-1] not in ('', '..'):
25 del components
[i
-1:i
+1]
27 elif components
[i
] == '' and i
> 0 and components
[i
-1] <> '':
30 if components
[i
]<>'..' and string
.find(components
[i
], '.')<>-1 :
31 components
[i
] = string
.join(string
.split(components
[i
],'.'),'/')
34 # Absolute unix path, don't start with colon
35 return string
.join(components
[1:], '.')
37 # relative unix path, start with colon. First replace
38 # leading .. by empty strings (giving ::file)
40 while i
< len(components
) and components
[i
] == '..':
43 return string
.join(components
, '.')
45 def pathname2url(pathname
):
46 "convert mac pathname to /-delimited 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
] == '':
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
, '/')
60 return string
.join(components
, '/')
63 for url
in ["index.html",
64 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
67 "/foo/bar/index.html",
70 print `url`
, '->', `
url2pathname(url
)`
71 for path
in ["drive:",
79 print `path`
, '->', `
pathname2url(path
)`
81 if __name__
== '__main__':