blob: f7b0fbe9a8c0a03dc71daad3e0f3114fe3a1ec51 [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:]
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000022 components = url.split('/')
Tim Peters2344fae2001-01-15 00:50:52 +000023 # make sure not to convert quoted slashes :-)
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000024 return urllib.unquote('\\'.join(components))
25 comp = url.split('|')
Tim Peters2344fae2001-01-15 00:50:52 +000026 if len(comp) != 2 or comp[0][-1] not in string.letters:
27 error = 'Bad URL: ' + url
28 raise IOError, error
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000029 drive = comp[0][-1].upper()
30 components = comp[1].split('/')
Tim Peters2344fae2001-01-15 00:50:52 +000031 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
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000055 components = p.split('\\')
56 return urllib.quote('/'.join(components))
57 comp = p.split(':')
Tim Peters2344fae2001-01-15 00:50:52 +000058 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
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000062 drive = urllib.quote(comp[0].upper())
63 components = comp[1].split('\\')
Tim Peters2344fae2001-01-15 00:50:52 +000064 path = '///' + drive + '|'
65 for comp in components:
66 if comp:
67 path = path + '/' + urllib.quote(comp)
68 return path