1 """riscos specific module for conversion between pathnames and URLs.
3 Do not import directly, use urllib instead."""
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
23 raise RuntimeError, 'Cannot convert non-local URL to pathname'
24 components
= string
.split(url
, '/')
30 # Remove . and embedded ..
32 while i
< len(components
):
33 if components
[i
] == '.':
35 elif components
[i
] == '..' and i
> 0 and \
36 components
[i
-1] not in ('', '..'):
37 del components
[i
-1:i
+1]
39 elif components
[i
] == '..':
42 elif components
[i
] == '' and i
> 0 and components
[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
), "/$:")
55 for url
in ["index.html",
56 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
57 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/../!Fresco/Welcome",
60 "/foo/bar/index.html",
63 print '%r -> %r' % (url
, url2pathname(url
))
64 print "*******************************************************"
65 for path
in ["SCSI::SCSI4.$.Anwendung",
67 "PythonApp:Lib.rourl2path/py"]:
68 print '%r -> %r' % (path
, pathname2url(path
))
70 if __name__
== '__main__':