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 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 3 | __all__ = ["url2pathname"] |
| 4 | |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 5 | def url2pathname(url): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 6 | r"""Convert a URL to a DOS path. |
Guido van Rossum | 7f91cf9 | 2000-05-30 13:25:35 +0000 | [diff] [blame] | 7 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 8 | ///C|/foo/bar/spam.foo |
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 | becomes |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 11 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 12 | C:\foo\bar\spam.foo |
| 13 | """ |
| 14 | import string, urllib |
| 15 | if not '|' in url: |
| 16 | # No drive specifier, just convert slashes |
| 17 | if url[:4] == '////': |
| 18 | # path is something like ////host/path/on/remote/host |
| 19 | # convert this to \\host\path\on\remote\host |
| 20 | # (notice halving of slashes at the start of the path) |
| 21 | url = url[2:] |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame^] | 22 | components = url.split('/') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 23 | # make sure not to convert quoted slashes :-) |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame^] | 24 | return urllib.unquote('\\'.join(components)) |
| 25 | comp = url.split('|') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 26 | if len(comp) != 2 or comp[0][-1] not in string.letters: |
| 27 | error = 'Bad URL: ' + url |
| 28 | raise IOError, error |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame^] | 29 | drive = comp[0][-1].upper() |
| 30 | components = comp[1].split('/') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 31 | path = drive + ':' |
| 32 | for comp in components: |
| 33 | if comp: |
| 34 | path = path + '\\' + urllib.unquote(comp) |
| 35 | return path |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 36 | |
| 37 | def pathname2url(p): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 38 | r"""Convert a DOS path name to a file url. |
Guido van Rossum | 7f91cf9 | 2000-05-30 13:25:35 +0000 | [diff] [blame] | 39 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 40 | C:\foo\bar\spam.foo |
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 | becomes |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 43 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 44 | ///C|/foo/bar/spam.foo |
| 45 | """ |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 46 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 47 | import string, urllib |
| 48 | if not ':' in p: |
| 49 | # No drive specifier, just convert slashes and quote the name |
| 50 | if p[:2] == '\\\\': |
| 51 | # path is something like \\host\path\on\remote\host |
| 52 | # convert this to ////host/path/on/remote/host |
| 53 | # (notice doubling of slashes at the start of the path) |
| 54 | p = '\\\\' + p |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame^] | 55 | components = p.split('\\') |
| 56 | return urllib.quote('/'.join(components)) |
| 57 | comp = p.split(':') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 58 | if len(comp) != 2 or len(comp[0]) > 1: |
| 59 | error = 'Bad path: ' + p |
| 60 | raise IOError, error |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 61 | |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame^] | 62 | drive = urllib.quote(comp[0].upper()) |
| 63 | components = comp[1].split('\\') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 64 | path = '///' + drive + '|' |
| 65 | for comp in components: |
| 66 | if comp: |
| 67 | path = path + '/' + urllib.quote(comp) |
| 68 | return path |