home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / macurl2path.py < prev    next >
Text File  |  1997-05-20  |  2KB  |  83 lines

  1. """Mac specific module for conversion between pathnames and URLs.
  2. Do not import directly, use urllib instead."""
  3.  
  4. import string
  5. import urllib
  6. import os
  7.  
  8. def url2pathname(pathname):
  9.     "Convert /-delimited pathname to mac pathname"
  10.     #
  11.     # XXXX The .. handling should be fixed...
  12.     #
  13.     tp = urllib.splittype(pathname)[0]
  14.     if tp and tp <> 'file':
  15.     raise RuntimeError, 'Cannot convert non-local URL to pathname'
  16.     components = string.split(pathname, '/')
  17.     # Remove . and embedded ..
  18.     i = 0
  19.     while i < len(components):
  20.     if components[i] == '.':
  21.         del components[i]
  22.     elif components[i] == '..' and i > 0 and \
  23.                   components[i-1] not in ('', '..'):
  24.         del components[i-1:i+1]
  25.         i = i-1
  26.     elif components[i] == '' and i > 0 and components[i-1] <> '':
  27.         del components[i]
  28.     else:
  29.         i = i+1
  30.     if not components[0]:
  31.     # Absolute unix path, don't start with colon
  32.     return string.join(components[1:], ':')
  33.     else:
  34.     # relative unix path, start with colon. First replace
  35.     # leading .. by empty strings (giving ::file)
  36.     i = 0
  37.     while i < len(components) and components[i] == '..':
  38.         components[i] = ''
  39.         i = i + 1
  40.     return ':' + string.join(components, ':')
  41.  
  42. def pathname2url(pathname):
  43.     "convert mac pathname to /-delimited pathname"
  44.     if '/' in pathname:
  45.     raise RuntimeError, "Cannot convert pathname containing slashes"
  46.     components = string.split(pathname, ':')
  47.     # Remove empty first and/or last component
  48.     if components[0] == '':
  49.         del components[0]
  50.     if components[-1] == '':
  51.         del components[-1]
  52.     # Replace empty string ('::') by .. (will result in '/../' later)
  53.     for i in range(len(components)):
  54.     if components[i] == '':
  55.         components[i] = '..'
  56.     # Truncate names longer than 31 bytes
  57.     components = map(lambda x: x[:31], components)
  58.  
  59.     if os.path.isabs(pathname):
  60.     return '/' + string.join(components, '/')
  61.     else:
  62.     return string.join(components, '/')
  63.  
  64. def test():
  65.     for url in ["index.html",
  66.         "bar/index.html",
  67.         "/foo/bar/index.html",
  68.         "/foo/bar/",
  69.         "/"]:
  70.     print `url`, '->', `url2pathname(url)`
  71.     for path in ["drive:",
  72.          "drive:dir:",
  73.          "drive:dir:file",
  74.          "drive:file",
  75.          "file",
  76.          ":file",
  77.          ":dir:",
  78.          ":dir:file"]:
  79.     print `path`, '->', `pathname2url(path)`
  80.  
  81. if __name__ == '__main__':
  82.     test()
  83.