Clarify portability and main program.
[python/dscho.git] / Lib / nturl2path.py
bloba25dc2a17ae6b5c78dcbe6676cd67084b3bc2ae3
2 # nturl2path convert a NT pathname to a file URL and
3 # vice versa
5 def url2pathname(url):
6 """ Convert a URL to a DOS path...
7 ///C|/foo/bar/spam.foo
9 becomes
11 C:\foo\bar\spam.foo
12 """
13 import string
14 if not '|' in url:
15 # No drive specifier, just convert slashes
16 components = string.splitfields(url, '/')
17 return string.joinfields(components, '\\')
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
30 def pathname2url(p):
32 """ Convert a DOS path name to a file url...
33 C:\foo\bar\spam.foo
35 becomes
37 ///C|/foo/bar/spam.foo
38 """
40 import string
41 if not ':' in p:
42 # No drive specifier, just convert slashes
43 components = string.splitfields(p, '\\')
44 return string.joinfields(components, '/')
45 comp = string.splitfields(p, ':')
46 if len(comp) != 2 or len(comp[0]) > 1:
47 error = 'Bad path: ' + p
48 raise IOError, error
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