home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / nturl2path.py < prev    next >
Text File  |  1997-08-12  |  1KB  |  57 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
  14.     if not '|' in url:
  15.         # No drive specifier, just convert slashes
  16.         components = string.splitfields(url, '/')
  17.         return string.joinfields(components, '\\')
  18.     comp = string.splitfields(url, '|')
  19.     if len(comp) != 2 or comp[0][-1] not in string.letters:
  20.         error = 'Bad URL: ' + url
  21.         raise IOError, error
  22.     drive = string.upper(comp[0][-1])
  23.     components = string.splitfields(comp[1], '/')
  24.     path = drive + ':'
  25.     for  comp in components:
  26.         if comp:
  27.             path = path + '\\' + comp
  28.     return path
  29.  
  30. def pathname2url(p):
  31.  
  32.     """ Convert a DOS path name to a file url...
  33.         C:\foo\bar\spam.foo
  34.  
  35.             becomes
  36.  
  37.         ///C|/foo/bar/spam.foo
  38.     """
  39.  
  40.     import string
  41.     if not ':' in p:
  42.         # No drive specifier, just convert slashes
  43.         components = string.splitfields(p, '\\')
  44.         return string.joinfields(components, '/')
  45.     comp = string.splitfields(p, ':')
  46.     if len(comp) != 2 or len(comp[0]) > 1:
  47.         error = 'Bad path: ' + p
  48.         raise IOError, error
  49.  
  50.     drive = string.upper(comp[0])
  51.     components = string.splitfields(comp[1], '\\')
  52.     path = '///' + drive + '|'
  53.     for comp in components:
  54.         if comp:
  55.             path = path + '/' + comp
  56.     return path
  57.