blob: 36dc7658870e9695f5864246d54a5ceef8133d67 [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 Storchaka458123b2015-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
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000012 import string, urllib.parse
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 :-)
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000024 return urllib.parse.unquote('\\'.join(components))
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000025 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
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020028 raise OSError(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 + ':'
Senthil Kumaran2d2ea1b2011-04-14 13:16:30 +080032 for comp in components:
Tim Peters2344fae2001-01-15 00:50:52 +000033 if comp:
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000034 path = path + '\\' + urllib.parse.unquote(comp)
Senthil Kumaran2d2ea1b2011-04-14 13:16:30 +080035 # Issue #11474 - handing url such as |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 Storchaka458123b2015-10-24 17:39:36 +030044 # C:\foo\bar\spam.foo
Georg Brandlc0b24732005-12-26 22:53:56 +000045 # becomes
Serhiy Storchaka458123b2015-10-24 17:39:36 +030046 # ///C:/foo/bar/spam.foo
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000047 import urllib.parse
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('\\')
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000056 return urllib.parse.quote('/'.join(components))
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000057 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
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020060 raise OSError(error)
Guido van Rossum746ea351996-06-26 19:47:56 +000061
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000062 drive = urllib.parse.quote(comp[0].upper())
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000063 components = comp[1].split('\\')
Senthil Kumaran690ce9b2009-05-05 18:41:13 +000064 path = '///' + drive + ':'
Tim Peters2344fae2001-01-15 00:50:52 +000065 for comp in components:
66 if comp:
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000067 path = path + '/' + urllib.parse.quote(comp)
Tim Peters2344fae2001-01-15 00:50:52 +000068 return path