OS-specific conversion from a relative URL of the 'file' scheme to a file system path; not recommended for general use.
(url)
| 6 | # Testing is done through test_urllib. |
| 7 | |
| 8 | def url2pathname(url): |
| 9 | """OS-specific conversion from a relative URL of the 'file' scheme |
| 10 | to a file system path; not recommended for general use.""" |
| 11 | # e.g. |
| 12 | # ///C|/foo/bar/spam.foo |
| 13 | # and |
| 14 | # ///C:/foo/bar/spam.foo |
| 15 | # become |
| 16 | # C:\foo\bar\spam.foo |
| 17 | import string, urllib.parse |
| 18 | # Windows itself uses ":" even in URLs. |
| 19 | url = url.replace(':', '|') |
| 20 | if not '|' in url: |
| 21 | # No drive specifier, just convert slashes |
| 22 | if url[:4] == '////': |
| 23 | # path is something like ////host/path/on/remote/host |
| 24 | # convert this to \\host\path\on\remote\host |
| 25 | # (notice halving of slashes at the start of the path) |
| 26 | url = url[2:] |
| 27 | components = url.split('/') |
| 28 | # make sure not to convert quoted slashes :-) |
| 29 | return urllib.parse.unquote('\\'.join(components)) |
| 30 | comp = url.split('|') |
| 31 | if len(comp) != 2 or comp[0][-1] not in string.ascii_letters: |
| 32 | error = 'Bad URL: ' + url |
| 33 | raise OSError(error) |
| 34 | drive = comp[0][-1].upper() |
| 35 | components = comp[1].split('/') |
| 36 | path = drive + ':' |
| 37 | for comp in components: |
| 38 | if comp: |
| 39 | path = path + '\\' + urllib.parse.unquote(comp) |
| 40 | # Issue #11474 - handing url such as |c/| |
| 41 | if path.endswith(':') and url.endswith('/'): |
| 42 | path += '\\' |
| 43 | return path |
| 44 | |
| 45 | def pathname2url(p): |
| 46 | """OS-specific conversion from a file system path to a relative URL |