1 """riscos specific module for conversion between pathnames and URLs.
3 Do not import directly, use urllib instead."""
8 __all__
= ["url2pathname","pathname2url"]
10 __slash_dot
= string
.maketrans("/.", "./")
12 def url2pathname(url
):
13 """OS-specific conversion from a relative URL of the 'file' scheme
14 to a file system path; not recommended for general use."""
15 tp
= urllib
.splittype(url
)[0]
16 if tp
and tp
<> 'file':
17 raise RuntimeError, 'Cannot convert non-local URL to pathname'
18 # Turn starting /// into /, an empty hostname means current host
22 raise RuntimeError, 'Cannot convert non-local URL to pathname'
23 components
= string
.split(url
, '/')
29 # Remove . and embedded ..
31 while i
< len(components
):
32 if components
[i
] == '.':
34 elif components
[i
] == '..' and i
> 0 and \
35 components
[i
-1] not in ('', '..'):
36 del components
[i
-1:i
+1]
38 elif components
[i
] == '..':
41 elif components
[i
] == '' and i
> 0 and components
[i
-1] <> '':
45 components
= map(lambda x
: urllib
.unquote(x
).translate(__slash_dot
), components
)
46 return '.'.join(components
)
48 def pathname2url(pathname
):
49 """OS-specific conversion from a file system path to a relative URL
50 of the 'file' scheme; not recommended for general use."""
51 return urllib
.quote('///' + pathname
.translate(__slash_dot
), "/$:")
54 for url
in ["index.html",
55 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
56 "/SCSI::SCSI4/$/Anwendung/Comm/Apps/../!Fresco/Welcome",
59 "/foo/bar/index.html",
62 print '%r -> %r' % (url
, url2pathname(url
))
63 print "*******************************************************"
64 for path
in ["SCSI::SCSI4.$.Anwendung",
66 "PythonApp:Lib.rourl2path/py"]:
67 print '%r -> %r' % (path
, pathname2url(path
))
69 if __name__
== '__main__':