blob: 10ea27280789f5a6bcfc7a2fb6c94ab6b9df5645 [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
3def url2pathname(url):
Georg Brandlc0b24732005-12-26 22:53:56 +00004 """OS-specific conversion from a relative URL of the 'file' scheme
5 to a file system path; not recommended for general use."""
6 # e.g.
7 # ///C|/foo/bar/spam.foo
8 # becomes
9 # C:\foo\bar\spam.foo
Tim Peters2344fae2001-01-15 00:50:52 +000010 import string, urllib
Georg Brandl07f159d2005-12-15 21:59:00 +000011 # Windows itself uses ":" even in URLs.
12 url = url.replace(':', '|')
Tim Peters2344fae2001-01-15 00:50:52 +000013 if not '|' in url:
14 # No drive specifier, just convert slashes
15 if url[:4] == '////':
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)
19 url = url[2:]
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000020 components = url.split('/')
Tim Peters2344fae2001-01-15 00:50:52 +000021 # make sure not to convert quoted slashes :-)
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000022 return urllib.unquote('\\'.join(components))
23 comp = url.split('|')
Fred Drake27eebb82001-07-20 18:52:02 +000024 if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
Tim Peters2344fae2001-01-15 00:50:52 +000025 error = 'Bad URL: ' + url
26 raise IOError, error
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000027 drive = comp[0][-1].upper()
Tim Peters2344fae2001-01-15 00:50:52 +000028 path = drive + ':'
Senthil Kumarana99b7612011-04-14 12:54:35 +080029 components = comp[1].split('/')
30 for comp in components:
Tim Peters2344fae2001-01-15 00:50:52 +000031 if comp:
32 path = path + '\\' + urllib.unquote(comp)
Senthil Kumarana99b7612011-04-14 12:54:35 +080033 # Issue #11474: url like '/C|/' should convert into 'C:\\'
34 if path.endswith(':') and url.endswith('/'):
35 path += '\\'
Tim Peters2344fae2001-01-15 00:50:52 +000036 return path
Guido van Rossum746ea351996-06-26 19:47:56 +000037
38def pathname2url(p):
Georg Brandlc0b24732005-12-26 22:53:56 +000039 """OS-specific conversion from a file system path to a relative URL
40 of the 'file' scheme; not recommended for general use."""
41 # e.g.
42 # C:\foo\bar\spam.foo
43 # becomes
44 # ///C|/foo/bar/spam.foo
Fred Drake27eebb82001-07-20 18:52:02 +000045 import urllib
Tim Peters2344fae2001-01-15 00:50:52 +000046 if not ':' in p:
47 # No drive specifier, just convert slashes and quote the name
48 if p[:2] == '\\\\':
49 # path is something like \\host\path\on\remote\host
50 # convert this to ////host/path/on/remote/host
51 # (notice doubling of slashes at the start of the path)
52 p = '\\\\' + p
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000053 components = p.split('\\')
54 return urllib.quote('/'.join(components))
55 comp = p.split(':')
Tim Peters2344fae2001-01-15 00:50:52 +000056 if len(comp) != 2 or len(comp[0]) > 1:
57 error = 'Bad path: ' + p
58 raise IOError, error
Guido van Rossum746ea351996-06-26 19:47:56 +000059
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000060 drive = urllib.quote(comp[0].upper())
61 components = comp[1].split('\\')
Senthil Kumaran2c724202009-05-05 17:34:42 +000062 path = '///' + drive + ':'
Tim Peters2344fae2001-01-15 00:50:52 +000063 for comp in components:
64 if comp:
65 path = path + '/' + urllib.quote(comp)
66 return path