home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / macurl2path.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  2.5 KB  |  95 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import urllib
  5. import os
  6. __all__ = [
  7.     'url2pathname',
  8.     'pathname2url']
  9.  
  10. def url2pathname(pathname):
  11.     tp = urllib.splittype(pathname)[0]
  12.     if tp and tp != 'file':
  13.         raise RuntimeError, 'Cannot convert non-local URL to pathname'
  14.     
  15.     if pathname[:3] == '///':
  16.         pathname = pathname[2:]
  17.     elif pathname[:2] == '//':
  18.         raise RuntimeError, 'Cannot convert non-local URL to pathname'
  19.     
  20.     components = pathname.split('/')
  21.     i = 0
  22.     while i < len(components):
  23.         if components[i] == '.':
  24.             del components[i]
  25.         elif components[i] == '..' and i > 0 and components[i - 1] not in ('', '..'):
  26.             del components[i - 1:i + 1]
  27.             i = i - 1
  28.         elif components[i] == '' and i > 0 and components[i - 1] != '':
  29.             del components[i]
  30.         else:
  31.             i = i + 1
  32.     if not components[0]:
  33.         rv = ':'.join(components[1:])
  34.     else:
  35.         i = 0
  36.         while i < len(components) and components[i] == '..':
  37.             components[i] = ''
  38.             i = i + 1
  39.         rv = ':' + ':'.join(components)
  40.     return urllib.unquote(rv)
  41.  
  42.  
  43. def pathname2url(pathname):
  44.     if '/' in pathname:
  45.         raise RuntimeError, 'Cannot convert pathname containing slashes'
  46.     
  47.     components = pathname.split(':')
  48.     if components[0] == '':
  49.         del components[0]
  50.     
  51.     if components[-1] == '':
  52.         del components[-1]
  53.     
  54.     for i in range(len(components)):
  55.         if components[i] == '':
  56.             components[i] = '..'
  57.         
  58.     
  59.     components = map(_pncomp2url, components)
  60.     if os.path.isabs(pathname):
  61.         return '/' + '/'.join(components)
  62.     else:
  63.         return '/'.join(components)
  64.  
  65.  
  66. def _pncomp2url(component):
  67.     component = urllib.quote(component[:31], safe = '')
  68.     return component
  69.  
  70.  
  71. def test():
  72.     for url in [
  73.         'index.html',
  74.         'bar/index.html',
  75.         '/foo/bar/index.html',
  76.         '/foo/bar/',
  77.         '/']:
  78.         print `url`, '->', `url2pathname(url)`
  79.     
  80.     for path in [
  81.         'drive:',
  82.         'drive:dir:',
  83.         'drive:dir:file',
  84.         'drive:file',
  85.         'file',
  86.         ':file',
  87.         ':dir:',
  88.         ':dir:file']:
  89.         print `path`, '->', `pathname2url(path)`
  90.     
  91.  
  92. if __name__ == '__main__':
  93.     test()
  94.  
  95.