0001 """Convert a NT pathname to a file URL and vice versa.""" 0002 0003 def url2pathname(url): 0004 r"""Convert a URL to a DOS path. 0005 0006 ///C|/foo/bar/spam.foo 0007 0008 becomes 0009 0010 C:\foo\bar\spam.foo 0011 """ 0012 import string, urllib 0013 if not '|' in url: 0014 # No drive specifier, just convert slashes 0015 if url[:4] == '////': 0016 # path is something like ////host/path/on/remote/host 0017 # convert this to \\host\path\on\remote\host 0018 # (notice halving of slashes at the start of the path) 0019 url = url[2:] 0020 components = url.split('/') 0021 # make sure not to convert quoted slashes :-) 0022 return urllib.unquote('\\'.join(components)) 0023 comp = url.split('|') 0024 if len(comp) != 2 or comp[0][-1] not in string.ascii_letters: 0025 error = 'Bad URL: ' + url 0026 raise IOError, error 0027 drive = comp[0][-1].upper() 0028 components = comp[1].split('/') 0029 path = drive + ':' 0030 for comp in components: 0031 if comp: 0032 path = path + '\\' + urllib.unquote(comp) 0033 return path 0034 0035 def pathname2url(p): 0036 r"""Convert a DOS path name to a file url. 0037 0038 C:\foo\bar\spam.foo 0039 0040 becomes 0041 0042 ///C|/foo/bar/spam.foo 0043 """ 0044 0045 import urllib 0046 if not ':' in p: 0047 # No drive specifier, just convert slashes and quote the name 0048 if p[:2] == '\\\\': 0049 # path is something like \\host\path\on\remote\host 0050 # convert this to ////host/path/on/remote/host 0051 # (notice doubling of slashes at the start of the path) 0052 p = '\\\\' + p 0053 components = p.split('\\') 0054 return urllib.quote('/'.join(components)) 0055 comp = p.split(':') 0056 if len(comp) != 2 or len(comp[0]) > 1: 0057 error = 'Bad path: ' + p 0058 raise IOError, error 0059 0060 drive = urllib.quote(comp[0].upper()) 0061 components = comp[1].split('\\') 0062 path = '///' + drive + '|' 0063 for comp in components: 0064 if comp: 0065 path = path + '/' + urllib.quote(comp) 0066 return path 0067
Generated by PyXR 0.9.4