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): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 4 | r"""Convert a URL to a DOS path. |
Guido van Rossum | 7f91cf9 | 2000-05-30 13:25:35 +0000 | [diff] [blame] | 5 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 6 | ///C|/foo/bar/spam.foo |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 7 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 8 | becomes |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 9 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 10 | C:\foo\bar\spam.foo |
| 11 | """ |
| 12 | import string, urllib |
| 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 :-) |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 22 | return urllib.unquote('\\'.join(components)) |
| 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 |
| 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 + ':' |
| 30 | for comp in components: |
| 31 | if comp: |
| 32 | path = path + '\\' + urllib.unquote(comp) |
| 33 | return path |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 34 | |
| 35 | def pathname2url(p): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 36 | r"""Convert a DOS path name to a file url. |
Guido van Rossum | 7f91cf9 | 2000-05-30 13:25:35 +0000 | [diff] [blame] | 37 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 38 | C:\foo\bar\spam.foo |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 39 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 40 | becomes |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 41 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 42 | ///C|/foo/bar/spam.foo |
| 43 | """ |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 44 | |
Fred Drake | 27eebb8 | 2001-07-20 18:52:02 +0000 | [diff] [blame] | 45 | import urllib |
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('\\') |
| 54 | return urllib.quote('/'.join(components)) |
| 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 |
| 58 | raise IOError, error |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 59 | |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 60 | drive = urllib.quote(comp[0].upper()) |
| 61 | components = comp[1].split('\\') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 62 | path = '///' + drive + '|' |
| 63 | for comp in components: |
| 64 | if comp: |
| 65 | path = path + '/' + urllib.quote(comp) |
| 66 | return path |