home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / NTURL2PATH.PY < prev    next >
Encoding:
Text File  |  1999-03-19  |  1.9 KB  |  68 lines

  1. #
  2. # nturl2path convert a NT pathname to a file URL and 
  3. # vice versa  
  4.  
  5. def url2pathname(url):
  6.     """ Convert a URL to a DOS path...
  7.         ///C|/foo/bar/spam.foo
  8.  
  9.             becomes
  10.  
  11.         C:\foo\bar\spam.foo
  12.     """
  13.     import string, urllib
  14.     if not '|' in url:
  15.         # No drive specifier, just convert slashes
  16.         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:]
  21.         components = string.split(url, '/')
  22.         # make sure not to convert quoted slashes :-)
  23.         return urllib.unquote(string.join(components, '\\'))
  24.     comp = string.split(url, '|')
  25.     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])
  29.     components = string.split(comp[1], '/')
  30.     path = drive + ':'
  31.     for  comp in components:
  32.         if comp:
  33.             path = path + '\\' + urllib.unquote(comp)
  34.     return path
  35.  
  36. def pathname2url(p):
  37.  
  38.     """ Convert a DOS path name to a file url...
  39.         C:\foo\bar\spam.foo
  40.  
  41.             becomes
  42.  
  43.         ///C|/foo/bar/spam.foo
  44.     """
  45.  
  46.     import string, urllib
  47.     if not ':' in p:
  48.         # No drive specifier, just convert slashes and quote the name
  49.         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
  54.         components = string.split(p, '\\')
  55.         return urllib.quote(string.join(components, '/'))
  56.     comp = string.split(p, ':')
  57.     if len(comp) != 2 or len(comp[0]) > 1:
  58.         error = 'Bad path: ' + p
  59.         raise IOError, error
  60.  
  61.     drive = urllib.quote(string.upper(comp[0]))
  62.     components = string.split(comp[1], '\\')
  63.     path = '///' + drive + '|'
  64.     for comp in components:
  65.         if comp:
  66.             path = path + '/' + urllib.quote(comp)
  67.     return path
  68.