1 """Convert a NT pathname to a file URL and vice versa."""
4 """OS-specific conversion from a relative URL of the 'file' scheme
5 to a file system path; not recommended for general use."""
7 # ///C|/foo/bar/spam.foo
10 import string
, urllib
.parse
11 # Windows itself uses ":" even in URLs.
12 url
= url
.replace(':', '|')
14 # No drive specifier, just convert slashes
16 # path is something like ////host/path/on/remote/host
17 # convert this to \\host\path\on\remote\host
18 # (notice halving of slashes at the start of the path)
20 components
= url
.split('/')
21 # make sure not to convert quoted slashes :-)
22 return urllib
.parse
.unquote('\\'.join(components
))
24 if len(comp
) != 2 or comp
[0][-1] not in string
.ascii_letters
:
25 error
= 'Bad URL: ' + url
27 drive
= comp
[0][-1].upper()
28 components
= comp
[1].split('/')
30 for comp
in components
:
32 path
= path
+ '\\' + urllib
.parse
.unquote(comp
)
36 """OS-specific conversion from a file system path to a relative URL
37 of the 'file' scheme; not recommended for general use."""
41 # ///C|/foo/bar/spam.foo
44 # No drive specifier, just convert slashes and quote the name
46 # path is something like \\host\path\on\remote\host
47 # convert this to ////host/path/on/remote/host
48 # (notice doubling of slashes at the start of the path)
50 components
= p
.split('\\')
51 return urllib
.parse
.quote('/'.join(components
))
53 if len(comp
) != 2 or len(comp
[0]) > 1:
54 error
= 'Bad path: ' + p
57 drive
= urllib
.parse
.quote(comp
[0].upper())
58 components
= comp
[1].split('\\')
59 path
= '///' + drive
+ ':'
60 for comp
in components
:
62 path
= path
+ '/' + urllib
.parse
.quote(comp
)