blob: 8c02049275a42fe279ff215369896d60d5662af8 [file] [log] [blame]
Guido van Rossum746ea351996-06-26 19:47:56 +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 Rossum746ea351996-06-26 19:47:56 +00007 ///C|/foo/bar/spam.foo
8
9 becomes
10
11 C:\foo\bar\spam.foo
12 """
Guido van Rossum367ac801999-03-12 14:31:10 +000013 import string, urllib
Guido van Rossumd510b721997-08-12 14:47:24 +000014 if not '|' in url:
15 # No drive specifier, just convert slashes
Guido van Rossuma0fec161999-03-18 14:21:41 +000016 if url[:4] == '////':
17 # path is something like ////host/path/on/remote/host
18 # convert this to \\host\path\on\remote\host
19 # (notice halving of slashes at the start of the path)
20 url = url[2:]
Guido van Rossum367ac801999-03-12 14:31:10 +000021 components = string.split(url, '/')
22 # make sure not to convert quoted slashes :-)
23 return urllib.unquote(string.join(components, '\\'))
24 comp = string.split(url, '|')
Guido van Rossum746ea351996-06-26 19:47:56 +000025 if len(comp) != 2 or comp[0][-1] not in string.letters:
26 error = 'Bad URL: ' + url
27 raise IOError, error
28 drive = string.upper(comp[0][-1])
Guido van Rossum367ac801999-03-12 14:31:10 +000029 components = string.split(comp[1], '/')
Guido van Rossum746ea351996-06-26 19:47:56 +000030 path = drive + ':'
31 for comp in components:
32 if comp:
Guido van Rossum367ac801999-03-12 14:31:10 +000033 path = path + '\\' + urllib.unquote(comp)
Guido van Rossum746ea351996-06-26 19:47:56 +000034 return path
35
36def pathname2url(p):
37
38 """ Convert a DOS path name to a file url...
Guido van Rossum746ea351996-06-26 19:47:56 +000039 C:\foo\bar\spam.foo
40
41 becomes
42
43 ///C|/foo/bar/spam.foo
44 """
45
Guido van Rossum367ac801999-03-12 14:31:10 +000046 import string, urllib
Guido van Rossumd510b721997-08-12 14:47:24 +000047 if not ':' in p:
Guido van Rossum367ac801999-03-12 14:31:10 +000048 # No drive specifier, just convert slashes and quote the name
Guido van Rossuma0fec161999-03-18 14:21:41 +000049 if p[:2] == '\\\\':
50 # path is something like \\host\path\on\remote\host
51 # convert this to ////host/path/on/remote/host
52 # (notice doubling of slashes at the start of the path)
53 p = '\\\\' + p
Guido van Rossum367ac801999-03-12 14:31:10 +000054 components = string.split(p, '\\')
55 return urllib.quote(string.join(components, '/'))
56 comp = string.split(p, ':')
Guido van Rossum746ea351996-06-26 19:47:56 +000057 if len(comp) != 2 or len(comp[0]) > 1:
58 error = 'Bad path: ' + p
59 raise IOError, error
60
Guido van Rossum367ac801999-03-12 14:31:10 +000061 drive = urllib.quote(string.upper(comp[0]))
62 components = string.split(comp[1], '\\')
Guido van Rossum746ea351996-06-26 19:47:56 +000063 path = '///' + drive + '|'
64 for comp in components:
65 if comp:
Guido van Rossum367ac801999-03-12 14:31:10 +000066 path = path + '/' + urllib.quote(comp)
Guido van Rossum746ea351996-06-26 19:47:56 +000067 return path