| 1 | n/a | """Convert a NT pathname to a file URL and vice versa.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | def url2pathname(url): |
|---|
| 4 | n/a | """OS-specific conversion from a relative URL of the 'file' scheme |
|---|
| 5 | n/a | to a file system path; not recommended for general use.""" |
|---|
| 6 | n/a | # e.g. |
|---|
| 7 | n/a | # ///C|/foo/bar/spam.foo |
|---|
| 8 | n/a | # and |
|---|
| 9 | n/a | # ///C:/foo/bar/spam.foo |
|---|
| 10 | n/a | # become |
|---|
| 11 | n/a | # C:\foo\bar\spam.foo |
|---|
| 12 | n/a | import string, urllib.parse |
|---|
| 13 | n/a | # Windows itself uses ":" even in URLs. |
|---|
| 14 | n/a | url = url.replace(':', '|') |
|---|
| 15 | n/a | if not '|' in url: |
|---|
| 16 | n/a | # No drive specifier, just convert slashes |
|---|
| 17 | n/a | if url[:4] == '////': |
|---|
| 18 | n/a | # path is something like ////host/path/on/remote/host |
|---|
| 19 | n/a | # convert this to \\host\path\on\remote\host |
|---|
| 20 | n/a | # (notice halving of slashes at the start of the path) |
|---|
| 21 | n/a | url = url[2:] |
|---|
| 22 | n/a | components = url.split('/') |
|---|
| 23 | n/a | # make sure not to convert quoted slashes :-) |
|---|
| 24 | n/a | return urllib.parse.unquote('\\'.join(components)) |
|---|
| 25 | n/a | comp = url.split('|') |
|---|
| 26 | n/a | if len(comp) != 2 or comp[0][-1] not in string.ascii_letters: |
|---|
| 27 | n/a | error = 'Bad URL: ' + url |
|---|
| 28 | n/a | raise OSError(error) |
|---|
| 29 | n/a | drive = comp[0][-1].upper() |
|---|
| 30 | n/a | components = comp[1].split('/') |
|---|
| 31 | n/a | path = drive + ':' |
|---|
| 32 | n/a | for comp in components: |
|---|
| 33 | n/a | if comp: |
|---|
| 34 | n/a | path = path + '\\' + urllib.parse.unquote(comp) |
|---|
| 35 | n/a | # Issue #11474 - handing url such as |c/| |
|---|
| 36 | n/a | if path.endswith(':') and url.endswith('/'): |
|---|
| 37 | n/a | path += '\\' |
|---|
| 38 | n/a | return path |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | def pathname2url(p): |
|---|
| 41 | n/a | """OS-specific conversion from a file system path to a relative URL |
|---|
| 42 | n/a | of the 'file' scheme; not recommended for general use.""" |
|---|
| 43 | n/a | # e.g. |
|---|
| 44 | n/a | # C:\foo\bar\spam.foo |
|---|
| 45 | n/a | # becomes |
|---|
| 46 | n/a | # ///C:/foo/bar/spam.foo |
|---|
| 47 | n/a | import urllib.parse |
|---|
| 48 | n/a | if not ':' in p: |
|---|
| 49 | n/a | # No drive specifier, just convert slashes and quote the name |
|---|
| 50 | n/a | if p[:2] == '\\\\': |
|---|
| 51 | n/a | # path is something like \\host\path\on\remote\host |
|---|
| 52 | n/a | # convert this to ////host/path/on/remote/host |
|---|
| 53 | n/a | # (notice doubling of slashes at the start of the path) |
|---|
| 54 | n/a | p = '\\\\' + p |
|---|
| 55 | n/a | components = p.split('\\') |
|---|
| 56 | n/a | return urllib.parse.quote('/'.join(components)) |
|---|
| 57 | n/a | comp = p.split(':') |
|---|
| 58 | n/a | if len(comp) != 2 or len(comp[0]) > 1: |
|---|
| 59 | n/a | error = 'Bad path: ' + p |
|---|
| 60 | n/a | raise OSError(error) |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | drive = urllib.parse.quote(comp[0].upper()) |
|---|
| 63 | n/a | components = comp[1].split('\\') |
|---|
| 64 | n/a | path = '///' + drive + ':' |
|---|
| 65 | n/a | for comp in components: |
|---|
| 66 | n/a | if comp: |
|---|
| 67 | n/a | path = path + '/' + urllib.parse.quote(comp) |
|---|
| 68 | n/a | return path |
|---|