blob: a25dc2a17ae6b5c78dcbe6676cd67084b3bc2ae3 [file] [log] [blame]
Guido van Rossum5c971671996-07-22 15:23:25 +00001#
2# nturl2path convert a NT pathname to a file URL and
3# vice versa
4
5def url2pathname(url):
6 """ Convert a URL to a DOS path...
Guido van Rossum5c971671996-07-22 15:23:25 +00007 ///C|/foo/bar/spam.foo
8
9 becomes
10
11 C:\foo\bar\spam.foo
12 """
13 import string
Guido van Rossumff712aa1997-08-15 00:45:26 +000014 if not '|' in url:
15 # No drive specifier, just convert slashes
16 components = string.splitfields(url, '/')
17 return string.joinfields(components, '\\')
Guido van Rossum5c971671996-07-22 15:23:25 +000018 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
30def pathname2url(p):
31
32 """ Convert a DOS path name to a file url...
Guido van Rossum5c971671996-07-22 15:23:25 +000033 C:\foo\bar\spam.foo
34
35 becomes
36
37 ///C|/foo/bar/spam.foo
38 """
39
40 import string
Guido van Rossumff712aa1997-08-15 00:45:26 +000041 if not ':' in p:
42 # No drive specifier, just convert slashes
43 components = string.splitfields(p, '\\')
44 return string.joinfields(components, '/')
Guido van Rossum5c971671996-07-22 15:23:25 +000045 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