blob: 1cfe827c6d681d6381885ea07628d5beaea29726 [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
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000010 import string, urllib.parse
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 :-)
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000022 return urllib.parse.unquote('\\'.join(components))
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000023 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
Collin Winterce36ad82007-08-30 01:19:48 +000026 raise IOError(error)
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000027 drive = comp[0][-1].upper()
28 components = comp[1].split('/')
Tim Peters2344fae2001-01-15 00:50:52 +000029 path = drive + ':'
30 for comp in components:
31 if comp:
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000032 path = path + '\\' + urllib.parse.unquote(comp)
Tim Peters2344fae2001-01-15 00:50:52 +000033 return path
Guido van Rossum746ea351996-06-26 19:47:56 +000034
35def pathname2url(p):
Georg Brandlc0b24732005-12-26 22:53:56 +000036 """OS-specific conversion from a file system path to a relative URL
37 of the 'file' scheme; not recommended for general use."""
38 # e.g.
39 # C:\foo\bar\spam.foo
40 # becomes
41 # ///C|/foo/bar/spam.foo
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000042 import urllib.parse
Tim Peters2344fae2001-01-15 00:50:52 +000043 if not ':' in p:
44 # No drive specifier, just convert slashes and quote the name
45 if p[:2] == '\\\\':
46 # path is something like \\host\path\on\remote\host
47 # convert this to ////host/path/on/remote/host
48 # (notice doubling of slashes at the start of the path)
49 p = '\\\\' + p
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000050 components = p.split('\\')
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000051 return urllib.parse.quote('/'.join(components))
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000052 comp = p.split(':')
Tim Peters2344fae2001-01-15 00:50:52 +000053 if len(comp) != 2 or len(comp[0]) > 1:
54 error = 'Bad path: ' + p
Collin Winterce36ad82007-08-30 01:19:48 +000055 raise IOError(error)
Guido van Rossum746ea351996-06-26 19:47:56 +000056
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000057 drive = urllib.parse.quote(comp[0].upper())
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000058 components = comp[1].split('\\')
Tim Peters2344fae2001-01-15 00:50:52 +000059 path = '///' + drive + '|'
60 for comp in components:
61 if comp:
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000062 path = path + '/' + urllib.parse.quote(comp)
Tim Peters2344fae2001-01-15 00:50:52 +000063 return path