home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / macurl2path.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  2.2 KB  |  89 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  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.     if pathname[:3] == '///':
  15.         pathname = pathname[2:]
  16.     elif pathname[:2] == '//':
  17.         raise RuntimeError, 'Cannot convert non-local URL to pathname'
  18.     components = pathname.split('/')
  19.     i = 0
  20.     while i < len(components):
  21.         if components[i] == '.':
  22.             del components[i]
  23.             continue
  24.         if components[i] == '..' and i > 0 and components[i - 1] not in ('', '..'):
  25.             del components[i - 1:i + 1]
  26.             i = i - 1
  27.             continue
  28.         if components[i] == '' and i > 0 and components[i - 1] != '':
  29.             del components[i]
  30.             continue
  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.     components = pathname.split(':')
  47.     if components[0] == '':
  48.         del components[0]
  49.     if components[-1] == '':
  50.         del components[-1]
  51.     for i in range(len(components)):
  52.         if components[i] == '':
  53.             components[i] = '..'
  54.             continue
  55.     components = map(_pncomp2url, components)
  56.     if os.path.isabs(pathname):
  57.         return '/' + '/'.join(components)
  58.     return None.join(components)
  59.  
  60.  
  61. def _pncomp2url(component):
  62.     component = urllib.quote(component[:31], safe = '')
  63.     return component
  64.  
  65.  
  66. def test():
  67.     for url in [
  68.         'index.html',
  69.         'bar/index.html',
  70.         '/foo/bar/index.html',
  71.         '/foo/bar/',
  72.         '/']:
  73.         print '%r -> %r' % (url, url2pathname(url))
  74.     
  75.     for path in [
  76.         'drive:',
  77.         'drive:dir:',
  78.         'drive:dir:file',
  79.         'drive:file',
  80.         'file',
  81.         ':file',
  82.         ':dir:',
  83.         ':dir:file']:
  84.         print '%r -> %r' % (path, pathname2url(path))
  85.     
  86.  
  87. if __name__ == '__main__':
  88.     test()
  89.