blob: 9e6eb0d559d78fe4766ee4ecc5747e14110f5651 [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.
Serhiy Storchaka9186a6a2015-10-24 17:39:36 +03007 # ///C|/foo/bar/spam.foo
8 # and
9 # ///C:/foo/bar/spam.foo
10 # become
11 # C:\foo\bar\spam.foo
Tim Peters2344fae2001-01-15 00:50:52 +000012 import string, urllib
Georg Brandl07f159d2005-12-15 21:59:00 +000013 # Windows itself uses ":" even in URLs.
14 url = url.replace(':', '|')
Tim Peters2344fae2001-01-15 00:50:52 +000015 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('|')
Fred Drake27eebb82001-07-20 18:52:02 +000026 if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
Tim Peters2344fae2001-01-15 00:50:52 +000027 error = 'Bad URL: ' + url
28 raise IOError, error
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000029 drive = comp[0][-1].upper()
Tim Peters2344fae2001-01-15 00:50:52 +000030 path = drive + ':'
Senthil Kumarana99b7612011-04-14 12:54:35 +080031 components = comp[1].split('/')
32 for comp in components:
Tim Peters2344fae2001-01-15 00:50:52 +000033 if comp:
34 path = path + '\\' + urllib.unquote(comp)
Senthil Kumarana99b7612011-04-14 12:54:35 +080035 # Issue #11474: url like '/C|/' should convert into 'C:\\'
36 if path.endswith(':') and url.endswith('/'):
37 path += '\\'
Tim Peters2344fae2001-01-15 00:50:52 +000038 return path
Guido van Rossum746ea351996-06-26 19:47:56 +000039
40def pathname2url(p):
Georg Brandlc0b24732005-12-26 22:53:56 +000041 """OS-specific conversion from a file system path to a relative URL
42 of the 'file' scheme; not recommended for general use."""
43 # e.g.
Serhiy Storchaka9186a6a2015-10-24 17:39:36 +030044 # C:\foo\bar\spam.foo
Georg Brandlc0b24732005-12-26 22:53:56 +000045 # becomes
Serhiy Storchaka9186a6a2015-10-24 17:39:36 +030046 # ///C:/foo/bar/spam.foo
Fred Drake27eebb82001-07-20 18:52:02 +000047 import urllib
Tim Peters2344fae2001-01-15 00:50:52 +000048 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('\\')
Senthil Kumaran2c724202009-05-05 17:34:42 +000064 path = '///' + drive + ':'
Tim Peters2344fae2001-01-15 00:50:52 +000065 for comp in components:
66 if comp:
67 path = path + '/' + urllib.quote(comp)
68 return path