blob: ae261bad6fa22f32521cf003ef8fed9cf747c29c [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):
4 """ Convert a URL to a DOS path...
Guido van Rossum746ea351996-06-26 19:47:56 +00005 ///C|/foo/bar/spam.foo
6
7 becomes
8
9 C:\foo\bar\spam.foo
10 """
Guido van Rossum367ac801999-03-12 14:31:10 +000011 import string, urllib
Guido van Rossumd510b721997-08-12 14:47:24 +000012 if not '|' in url:
13 # No drive specifier, just convert slashes
Guido van Rossuma0fec161999-03-18 14:21:41 +000014 if url[:4] == '////':
15 # path is something like ////host/path/on/remote/host
16 # convert this to \\host\path\on\remote\host
17 # (notice halving of slashes at the start of the path)
18 url = url[2:]
Guido van Rossum367ac801999-03-12 14:31:10 +000019 components = string.split(url, '/')
20 # make sure not to convert quoted slashes :-)
21 return urllib.unquote(string.join(components, '\\'))
22 comp = string.split(url, '|')
Guido van Rossum746ea351996-06-26 19:47:56 +000023 if len(comp) != 2 or comp[0][-1] not in string.letters:
24 error = 'Bad URL: ' + url
25 raise IOError, error
26 drive = string.upper(comp[0][-1])
Guido van Rossum367ac801999-03-12 14:31:10 +000027 components = string.split(comp[1], '/')
Guido van Rossum746ea351996-06-26 19:47:56 +000028 path = drive + ':'
29 for comp in components:
30 if comp:
Guido van Rossum367ac801999-03-12 14:31:10 +000031 path = path + '\\' + urllib.unquote(comp)
Guido van Rossum746ea351996-06-26 19:47:56 +000032 return path
33
34def pathname2url(p):
Guido van Rossum746ea351996-06-26 19:47:56 +000035 """ Convert a DOS path name to a file url...
Guido van Rossum746ea351996-06-26 19:47:56 +000036 C:\foo\bar\spam.foo
37
38 becomes
39
40 ///C|/foo/bar/spam.foo
41 """
42
Guido van Rossum367ac801999-03-12 14:31:10 +000043 import string, urllib
Guido van Rossumd510b721997-08-12 14:47:24 +000044 if not ':' in p:
Guido van Rossum367ac801999-03-12 14:31:10 +000045 # No drive specifier, just convert slashes and quote the name
Guido van Rossuma0fec161999-03-18 14:21:41 +000046 if p[:2] == '\\\\':
47 # path is something like \\host\path\on\remote\host
48 # convert this to ////host/path/on/remote/host
49 # (notice doubling of slashes at the start of the path)
50 p = '\\\\' + p
Guido van Rossum367ac801999-03-12 14:31:10 +000051 components = string.split(p, '\\')
52 return urllib.quote(string.join(components, '/'))
53 comp = string.split(p, ':')
Guido van Rossum746ea351996-06-26 19:47:56 +000054 if len(comp) != 2 or len(comp[0]) > 1:
55 error = 'Bad path: ' + p
56 raise IOError, error
57
Guido van Rossum367ac801999-03-12 14:31:10 +000058 drive = urllib.quote(string.upper(comp[0]))
59 components = string.split(comp[1], '\\')
Guido van Rossum746ea351996-06-26 19:47:56 +000060 path = '///' + drive + '|'
61 for comp in components:
62 if comp:
Guido van Rossum367ac801999-03-12 14:31:10 +000063 path = path + '/' + urllib.quote(comp)
Guido van Rossum746ea351996-06-26 19:47:56 +000064 return path