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): |
| 4 | """ Convert a URL to a DOS path... |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 5 | ///C|/foo/bar/spam.foo |
| 6 | |
| 7 | becomes |
| 8 | |
| 9 | C:\foo\bar\spam.foo |
| 10 | """ |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 11 | import string, urllib |
Guido van Rossum | d510b72 | 1997-08-12 14:47:24 +0000 | [diff] [blame] | 12 | if not '|' in url: |
| 13 | # No drive specifier, just convert slashes |
Guido van Rossum | a0fec16 | 1999-03-18 14:21:41 +0000 | [diff] [blame] | 14 | if url[:4] == '////': |
| 15 | # path is something like ////host/path/on/remote/host |
| 16 | # convert this to \\host\path\on\remote\host |
| 17 | # (notice halving of slashes at the start of the path) |
| 18 | url = url[2:] |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 19 | components = string.split(url, '/') |
| 20 | # make sure not to convert quoted slashes :-) |
| 21 | return urllib.unquote(string.join(components, '\\')) |
| 22 | comp = string.split(url, '|') |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 23 | if len(comp) != 2 or comp[0][-1] not in string.letters: |
| 24 | error = 'Bad URL: ' + url |
| 25 | raise IOError, error |
| 26 | drive = string.upper(comp[0][-1]) |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 27 | components = string.split(comp[1], '/') |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 28 | path = drive + ':' |
| 29 | for comp in components: |
| 30 | if comp: |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 31 | path = path + '\\' + urllib.unquote(comp) |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 32 | return path |
| 33 | |
| 34 | def pathname2url(p): |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 35 | """ Convert a DOS path name to a file url... |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 36 | C:\foo\bar\spam.foo |
| 37 | |
| 38 | becomes |
| 39 | |
| 40 | ///C|/foo/bar/spam.foo |
| 41 | """ |
| 42 | |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 43 | import string, urllib |
Guido van Rossum | d510b72 | 1997-08-12 14:47:24 +0000 | [diff] [blame] | 44 | if not ':' in p: |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 45 | # No drive specifier, just convert slashes and quote the name |
Guido van Rossum | a0fec16 | 1999-03-18 14:21:41 +0000 | [diff] [blame] | 46 | if p[:2] == '\\\\': |
| 47 | # path is something like \\host\path\on\remote\host |
| 48 | # convert this to ////host/path/on/remote/host |
| 49 | # (notice doubling of slashes at the start of the path) |
| 50 | p = '\\\\' + p |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 51 | components = string.split(p, '\\') |
| 52 | return urllib.quote(string.join(components, '/')) |
| 53 | comp = string.split(p, ':') |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 54 | if len(comp) != 2 or len(comp[0]) > 1: |
| 55 | error = 'Bad path: ' + p |
| 56 | raise IOError, error |
| 57 | |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 58 | drive = urllib.quote(string.upper(comp[0])) |
| 59 | components = string.split(comp[1], '\\') |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 60 | path = '///' + drive + '|' |
| 61 | for comp in components: |
| 62 | if comp: |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 63 | path = path + '/' + urllib.quote(comp) |
Guido van Rossum | 746ea35 | 1996-06-26 19:47:56 +0000 | [diff] [blame] | 64 | return path |