blob: 7e78cb1932f2ccaf07392fdcd179722c45d7f8e7 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Convert a NT pathname to a file URL and vice versa."""
Guido van Rossum746ea351996-06-26 19:47:56 +00002
Skip Montanaro269b83b2001-02-06 01:07:02 +00003__all__ = ["url2pathname"]
4
Guido van Rossum746ea351996-06-26 19:47:56 +00005def url2pathname(url):
Tim Peters2344fae2001-01-15 00:50:52 +00006 r"""Convert a URL to a DOS path.
Guido van Rossum7f91cf92000-05-30 13:25:35 +00007
Tim Peters2344fae2001-01-15 00:50:52 +00008 ///C|/foo/bar/spam.foo
Guido van Rossum746ea351996-06-26 19:47:56 +00009
Tim Peters2344fae2001-01-15 00:50:52 +000010 becomes
Guido van Rossum746ea351996-06-26 19:47:56 +000011
Tim Peters2344fae2001-01-15 00:50:52 +000012 C:\foo\bar\spam.foo
13 """
14 import string, urllib
15 if not '|' in url:
16 # No drive specifier, just convert slashes
17 if url[:4] == '////':
18 # path is something like ////host/path/on/remote/host
19 # convert this to \\host\path\on\remote\host
20 # (notice halving of slashes at the start of the path)
21 url = url[2:]
22 components = string.split(url, '/')
23 # make sure not to convert quoted slashes :-)
24 return urllib.unquote(string.join(components, '\\'))
25 comp = string.split(url, '|')
26 if len(comp) != 2 or comp[0][-1] not in string.letters:
27 error = 'Bad URL: ' + url
28 raise IOError, error
29 drive = string.upper(comp[0][-1])
30 components = string.split(comp[1], '/')
31 path = drive + ':'
32 for comp in components:
33 if comp:
34 path = path + '\\' + urllib.unquote(comp)
35 return path
Guido van Rossum746ea351996-06-26 19:47:56 +000036
37def pathname2url(p):
Tim Peters2344fae2001-01-15 00:50:52 +000038 r"""Convert a DOS path name to a file url.
Guido van Rossum7f91cf92000-05-30 13:25:35 +000039
Tim Peters2344fae2001-01-15 00:50:52 +000040 C:\foo\bar\spam.foo
Guido van Rossum746ea351996-06-26 19:47:56 +000041
Tim Peters2344fae2001-01-15 00:50:52 +000042 becomes
Guido van Rossum746ea351996-06-26 19:47:56 +000043
Tim Peters2344fae2001-01-15 00:50:52 +000044 ///C|/foo/bar/spam.foo
45 """
Guido van Rossum746ea351996-06-26 19:47:56 +000046
Tim Peters2344fae2001-01-15 00:50:52 +000047 import string, urllib
48 if not ':' in p:
49 # No drive specifier, just convert slashes and quote the name
50 if p[:2] == '\\\\':
51 # path is something like \\host\path\on\remote\host
52 # convert this to ////host/path/on/remote/host
53 # (notice doubling of slashes at the start of the path)
54 p = '\\\\' + p
55 components = string.split(p, '\\')
56 return urllib.quote(string.join(components, '/'))
57 comp = string.split(p, ':')
58 if len(comp) != 2 or len(comp[0]) > 1:
59 error = 'Bad path: ' + p
60 raise IOError, error
Guido van Rossum746ea351996-06-26 19:47:56 +000061
Tim Peters2344fae2001-01-15 00:50:52 +000062 drive = urllib.quote(string.upper(comp[0]))
63 components = string.split(comp[1], '\\')
64 path = '///' + drive + '|'
65 for comp in components:
66 if comp:
67 path = path + '/' + urllib.quote(comp)
68 return path