Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 1 | # |
| 2 | # nturl2path convert a NT pathname to a file URL and |
| 3 | # vice versa |
| 4 | |
| 5 | def url2pathname(url): |
| 6 | """ Convert a URL to a DOS path... |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 7 | ///C|/foo/bar/spam.foo |
| 8 | |
| 9 | becomes |
| 10 | |
| 11 | C:\foo\bar\spam.foo |
| 12 | """ |
| 13 | import string |
Guido van Rossum | ff712aa | 1997-08-15 00:45:26 +0000 | [diff] [blame] | 14 | if not '|' in url: |
| 15 | # No drive specifier, just convert slashes |
| 16 | components = string.splitfields(url, '/') |
| 17 | return string.joinfields(components, '\\') |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 18 | comp = string.splitfields(url, '|') |
| 19 | if len(comp) != 2 or comp[0][-1] not in string.letters: |
| 20 | error = 'Bad URL: ' + url |
| 21 | raise IOError, error |
| 22 | drive = string.upper(comp[0][-1]) |
| 23 | components = string.splitfields(comp[1], '/') |
| 24 | path = drive + ':' |
| 25 | for comp in components: |
| 26 | if comp: |
| 27 | path = path + '\\' + comp |
| 28 | return path |
| 29 | |
| 30 | def pathname2url(p): |
| 31 | |
| 32 | """ Convert a DOS path name to a file url... |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 33 | C:\foo\bar\spam.foo |
| 34 | |
| 35 | becomes |
| 36 | |
| 37 | ///C|/foo/bar/spam.foo |
| 38 | """ |
| 39 | |
| 40 | import string |
Guido van Rossum | ff712aa | 1997-08-15 00:45:26 +0000 | [diff] [blame] | 41 | if not ':' in p: |
| 42 | # No drive specifier, just convert slashes |
| 43 | components = string.splitfields(p, '\\') |
| 44 | return string.joinfields(components, '/') |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 45 | comp = string.splitfields(p, ':') |
| 46 | if len(comp) != 2 or len(comp[0]) > 1: |
| 47 | error = 'Bad path: ' + p |
| 48 | raise IOError, error |
| 49 | |
| 50 | drive = string.upper(comp[0]) |
| 51 | components = string.splitfields(comp[1], '\\') |
| 52 | path = '///' + drive + '|' |
| 53 | for comp in components: |
| 54 | if comp: |
| 55 | path = path + '/' + comp |
| 56 | return path |