blob: 0445b8afb0fe94e8dc930405f38ff8595a0a0ea4 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Convert a NT pathname to a file URL and vice versa."""
Guido van Rossum746ea351996-06-26 19:47:56 +00002
3def url2pathname(url):
Guido van Rossum7f91cf92000-05-30 13:25:35 +00004 r"""Convert a URL to a DOS path.
5
Guido van Rossum746ea351996-06-26 19:47:56 +00006 ///C|/foo/bar/spam.foo
7
8 becomes
9
10 C:\foo\bar\spam.foo
11 """
Guido van Rossum367ac801999-03-12 14:31:10 +000012 import string, urllib
Guido van Rossumd510b721997-08-12 14:47:24 +000013 if not '|' in url:
14 # No drive specifier, just convert slashes
Guido van Rossuma0fec161999-03-18 14:21:41 +000015 if url[:4] == '////':
16 # path is something like ////host/path/on/remote/host
17 # convert this to \\host\path\on\remote\host
18 # (notice halving of slashes at the start of the path)
19 url = url[2:]
Guido van Rossum367ac801999-03-12 14:31:10 +000020 components = string.split(url, '/')
21 # make sure not to convert quoted slashes :-)
22 return urllib.unquote(string.join(components, '\\'))
23 comp = string.split(url, '|')
Guido van Rossum746ea351996-06-26 19:47:56 +000024 if len(comp) != 2 or comp[0][-1] not in string.letters:
25 error = 'Bad URL: ' + url
26 raise IOError, error
27 drive = string.upper(comp[0][-1])
Guido van Rossum367ac801999-03-12 14:31:10 +000028 components = string.split(comp[1], '/')
Guido van Rossum746ea351996-06-26 19:47:56 +000029 path = drive + ':'
30 for comp in components:
31 if comp:
Guido van Rossum367ac801999-03-12 14:31:10 +000032 path = path + '\\' + urllib.unquote(comp)
Guido van Rossum746ea351996-06-26 19:47:56 +000033 return path
34
35def pathname2url(p):
Guido van Rossum7f91cf92000-05-30 13:25:35 +000036 r"""Convert a DOS path name to a file url.
37
Guido van Rossum746ea351996-06-26 19:47:56 +000038 C:\foo\bar\spam.foo
39
40 becomes
41
42 ///C|/foo/bar/spam.foo
43 """
44
Guido van Rossum367ac801999-03-12 14:31:10 +000045 import string, urllib
Guido van Rossumd510b721997-08-12 14:47:24 +000046 if not ':' in p:
Guido van Rossum367ac801999-03-12 14:31:10 +000047 # No drive specifier, just convert slashes and quote the name
Guido van Rossuma0fec161999-03-18 14:21:41 +000048 if p[:2] == '\\\\':
49 # path is something like \\host\path\on\remote\host
50 # convert this to ////host/path/on/remote/host
51 # (notice doubling of slashes at the start of the path)
52 p = '\\\\' + p
Guido van Rossum367ac801999-03-12 14:31:10 +000053 components = string.split(p, '\\')
54 return urllib.quote(string.join(components, '/'))
55 comp = string.split(p, ':')
Guido van Rossum746ea351996-06-26 19:47:56 +000056 if len(comp) != 2 or len(comp[0]) > 1:
57 error = 'Bad path: ' + p
58 raise IOError, error
59
Guido van Rossum367ac801999-03-12 14:31:10 +000060 drive = urllib.quote(string.upper(comp[0]))
61 components = string.split(comp[1], '\\')
Guido van Rossum746ea351996-06-26 19:47:56 +000062 path = '///' + drive + '|'
63 for comp in components:
64 if comp:
Guido van Rossum367ac801999-03-12 14:31:10 +000065 path = path + '/' + urllib.quote(comp)
Guido van Rossum746ea351996-06-26 19:47:56 +000066 return path