| 1 |
"""Convert a NT pathname to a file URL and vice versa. |
| 2 |
|
| 3 |
This module only exists to provide OS-specific code |
| 4 |
for urllib.requests, thus do not use directly. |
| 5 |
""" |
| 6 |
|
| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import string, urllib.parse |
| 18 |
|
| 19 |
url = url.replace(':', '|') |
| 20 |
if not '|' in url: |
| 21 |
|
| 22 |
if url[:4] == '////': |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
url = url[2:] |
| 27 |
components = url.split('/') |
| 28 |
|
| 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 |
|
| 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 |
| 47 |
of the 'file' scheme; not recommended for general use.""" |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
import urllib.parse |
| 53 |
|
| 54 |
|
| 55 |
if p[:4] == '\\\\?\\': |
| 56 |
p = p[4:] |
| 57 |
if p[:4].upper() == 'UNC\\': |
| 58 |
p = '\\' + p[4:] |
| 59 |
elif p[1:2] != ':': |
| 60 |
raise OSError('Bad path: ' + p) |
| 61 |
if not ':' in p: |
| 62 |
|
| 63 |
if p[:2] == '\\\\': |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
p = '\\\\' + p |
| 68 |
components = p.split('\\') |
| 69 |
return urllib.parse.quote('/'.join(components)) |
| 70 |
comp = p.split(':', maxsplit=2) |
| 71 |
if len(comp) != 2 or len(comp[0]) > 1: |
| 72 |
error = 'Bad path: ' + p |
| 73 |
raise OSError(error) |
| 74 |
|
| 75 |
drive = urllib.parse.quote(comp[0].upper()) |
| 76 |
components = comp[1].split('\\') |
| 77 |
path = '///' + drive + ':' |
| 78 |
for comp in components: |
| 79 |
if comp: |
| 80 |
path = path + '/' + urllib.parse.quote(comp) |
| 81 |
return path |