Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Convert a NT pathname to a file URL and vice versa.""" |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 2 | |
| 3 | def url2pathname(url): |
Georg Brandl | c0b2473 | 2005-12-26 22:53:56 +0000 | [diff] [blame] | 4 | """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'Arc | c80902e | 2008-06-18 22:38:24 +0000 | [diff] [blame] | 10 | import string, urllib.parse |
Georg Brandl | 07f159d | 2005-12-15 21:59:00 +0000 | [diff] [blame] | 11 | # Windows itself uses ":" even in URLs. |
| 12 | url = url.replace(':', '|') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 13 | 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. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 20 | components = url.split('/') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 21 | # make sure not to convert quoted slashes :-) |
Amaury Forgeot d'Arc | c80902e | 2008-06-18 22:38:24 +0000 | [diff] [blame] | 22 | return urllib.parse.unquote('\\'.join(components)) |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 23 | comp = url.split('|') |
Fred Drake | 27eebb8 | 2001-07-20 18:52:02 +0000 | [diff] [blame] | 24 | if len(comp) != 2 or comp[0][-1] not in string.ascii_letters: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 25 | error = 'Bad URL: ' + url |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 26 | raise IOError(error) |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 27 | drive = comp[0][-1].upper() |
| 28 | components = comp[1].split('/') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 29 | path = drive + ':' |
Senthil Kumaran | 2d2ea1b | 2011-04-14 13:16:30 +0800 | [diff] [blame] | 30 | for comp in components: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 31 | if comp: |
Amaury Forgeot d'Arc | c80902e | 2008-06-18 22:38:24 +0000 | [diff] [blame] | 32 | path = path + '\\' + urllib.parse.unquote(comp) |
Senthil Kumaran | 2d2ea1b | 2011-04-14 13:16:30 +0800 | [diff] [blame] | 33 | # Issue #11474 - handing url such as |c/| |
| 34 | if path.endswith(':') and url.endswith('/'): |
| 35 | path += '\\' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 36 | return path |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 37 | |
| 38 | def pathname2url(p): |
Georg Brandl | c0b2473 | 2005-12-26 22:53:56 +0000 | [diff] [blame] | 39 | """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 |
Amaury Forgeot d'Arc | c80902e | 2008-06-18 22:38:24 +0000 | [diff] [blame] | 45 | import urllib.parse |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 46 | 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. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 53 | components = p.split('\\') |
Amaury Forgeot d'Arc | c80902e | 2008-06-18 22:38:24 +0000 | [diff] [blame] | 54 | return urllib.parse.quote('/'.join(components)) |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 55 | comp = p.split(':') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 56 | if len(comp) != 2 or len(comp[0]) > 1: |
| 57 | error = 'Bad path: ' + p |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 58 | raise IOError(error) |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 59 | |
Amaury Forgeot d'Arc | c80902e | 2008-06-18 22:38:24 +0000 | [diff] [blame] | 60 | drive = urllib.parse.quote(comp[0].upper()) |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 61 | components = comp[1].split('\\') |
Senthil Kumaran | 690ce9b | 2009-05-05 18:41:13 +0000 | [diff] [blame] | 62 | path = '///' + drive + ':' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 63 | for comp in components: |
| 64 | if comp: |
Amaury Forgeot d'Arc | c80902e | 2008-06-18 22:38:24 +0000 | [diff] [blame] | 65 | path = path + '/' + urllib.parse.quote(comp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 66 | return path |