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... |
| 7 | Currently only works for absolute paths |
| 8 | |
| 9 | ///C|/foo/bar/spam.foo |
| 10 | |
| 11 | becomes |
| 12 | |
| 13 | C:\foo\bar\spam.foo |
| 14 | """ |
| 15 | import string |
| 16 | comp = string.splitfields(url, '|') |
| 17 | if len(comp) != 2 or comp[0][-1] not in string.letters: |
| 18 | error = 'Bad URL: ' + url |
| 19 | raise IOError, error |
| 20 | drive = string.upper(comp[0][-1]) |
| 21 | components = string.splitfields(comp[1], '/') |
| 22 | path = drive + ':' |
| 23 | for comp in components: |
| 24 | if comp: |
| 25 | path = path + '\\' + comp |
| 26 | return path |
| 27 | |
| 28 | def pathname2url(p): |
| 29 | |
| 30 | """ Convert a DOS path name to a file url... |
| 31 | Currently only works for absolute paths |
| 32 | |
| 33 | C:\foo\bar\spam.foo |
| 34 | |
| 35 | becomes |
| 36 | |
| 37 | ///C|/foo/bar/spam.foo |
| 38 | """ |
| 39 | |
| 40 | import string |
| 41 | comp = string.splitfields(p, ':') |
| 42 | if len(comp) != 2 or len(comp[0]) > 1: |
| 43 | error = 'Bad path: ' + p |
| 44 | raise IOError, error |
| 45 | |
| 46 | drive = string.upper(comp[0]) |
| 47 | components = string.splitfields(comp[1], '\\') |
| 48 | path = '///' + drive + '|' |
| 49 | for comp in components: |
| 50 | if comp: |
| 51 | path = path + '/' + comp |
| 52 | return path |