| 1 |
"""Convert a NT pathname to a file URL and vice versa.""" |
| 2 |
|
| 3 |
def url2pathname(url): |
| 4 |
"""OS-specific conversion from a relative URL of the 'file' scheme |
| 5 |
to a file system path; not recommended for general use.""" |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
import string, urllib |
| 13 |
|
| 14 |
url = url.replace(':', '|') |
| 15 |
if not '|' in url: |
| 16 |
|
| 17 |
if url[:4] == '////': |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
url = url[2:] |
| 22 |
components = url.split('/') |
| 23 |
|
| 24 |
return urllib.unquote('\\'.join(components)) |
| 25 |
comp = url.split('|') |
| 26 |
if len(comp) != 2 or comp[0][-1] not in string.ascii_letters: |
| 27 |
error = 'Bad URL: ' + url |
| 28 |
raise IOError, error |
| 29 |
drive = comp[0][-1].upper() |
| 30 |
path = drive + ':' |
| 31 |
components = comp[1].split('/') |
| 32 |
for comp in components: |
| 33 |
if comp: |
| 34 |
path = path + '\\' + urllib.unquote(comp) |
| 35 |
|
| 36 |
if path.endswith(':') and url.endswith('/'): |
| 37 |
path += '\\' |
| 38 |
return path |
| 39 |
|
| 40 |
def pathname2url(p): |
| 41 |
"""OS-specific conversion from a file system path to a relative URL |
| 42 |
of the 'file' scheme; not recommended for general use.""" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
import urllib |
| 48 |
if not ':' in p: |
| 49 |
|
| 50 |
if p[:2] == '\\\\': |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
p = '\\\\' + p |
| 55 |
components = p.split('\\') |
| 56 |
return urllib.quote('/'.join(components)) |
| 57 |
comp = p.split(':') |
| 58 |
if len(comp) != 2 or len(comp[0]) > 1: |
| 59 |
error = 'Bad path: ' + p |
| 60 |
raise IOError, error |
| 61 |
|
| 62 |
drive = urllib.quote(comp[0].upper()) |
| 63 |
components = comp[1].split('\\') |
| 64 |
path = '///' + drive + ':' |
| 65 |
for comp in components: |
| 66 |
if comp: |
| 67 |
path = path + '/' + urllib.quote(comp) |
| 68 |
return path |