home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2029 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.8 KB  |  132 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import urllib2
  5. from cStringIO import StringIO
  6. import _response
  7.  
  8. class GzipConsumer:
  9.     
  10.     def __init__(self, consumer):
  11.         self._GzipConsumer__consumer = consumer
  12.         self._GzipConsumer__decoder = None
  13.         self._GzipConsumer__data = ''
  14.  
  15.     
  16.     def __getattr__(self, key):
  17.         return getattr(self._GzipConsumer__consumer, key)
  18.  
  19.     
  20.     def feed(self, data):
  21.         if self._GzipConsumer__decoder is None:
  22.             data = self._GzipConsumer__data + data
  23.             
  24.             try:
  25.                 i = 10
  26.                 flag = ord(data[3])
  27.                 if flag & 4:
  28.                     x = ord(data[i]) + 256 * ord(data[i + 1])
  29.                     i = i + 2 + x
  30.                 
  31.                 if flag & 8:
  32.                     while ord(data[i]):
  33.                         i = i + 1
  34.                     i = i + 1
  35.                 
  36.                 if flag & 16:
  37.                     while ord(data[i]):
  38.                         i = i + 1
  39.                     i = i + 1
  40.                 
  41.                 if flag & 2:
  42.                     i = i + 2
  43.                 
  44.                 if len(data) < i:
  45.                     raise IndexError('not enough data')
  46.                 len(data) < i
  47.                 if data[:3] != '\x1f\x8b\x08':
  48.                     raise IOError('invalid gzip data')
  49.                 data[:3] != '\x1f\x8b\x08'
  50.                 data = data[i:]
  51.             except IndexError:
  52.                 self._GzipConsumer__data = data
  53.                 return None
  54.  
  55.             import zlib
  56.             self._GzipConsumer__data = ''
  57.             self._GzipConsumer__decoder = zlib.decompressobj(-(zlib.MAX_WBITS))
  58.         
  59.         data = self._GzipConsumer__decoder.decompress(data)
  60.         if data:
  61.             self._GzipConsumer__consumer.feed(data)
  62.         
  63.  
  64.     
  65.     def close(self):
  66.         if self._GzipConsumer__decoder:
  67.             data = self._GzipConsumer__decoder.flush()
  68.             if data:
  69.                 self._GzipConsumer__consumer.feed(data)
  70.             
  71.         
  72.         self._GzipConsumer__consumer.close()
  73.  
  74.  
  75.  
  76. class stupid_gzip_consumer:
  77.     
  78.     def __init__(self):
  79.         self.data = []
  80.  
  81.     
  82.     def feed(self, data):
  83.         self.data.append(data)
  84.  
  85.  
  86.  
  87. class stupid_gzip_wrapper(_response.closeable_response):
  88.     
  89.     def __init__(self, response):
  90.         self._response = response
  91.         c = stupid_gzip_consumer()
  92.         gzc = GzipConsumer(c)
  93.         gzc.feed(response.read())
  94.         self._stupid_gzip_wrapper__data = StringIO(''.join(c.data))
  95.  
  96.     
  97.     def read(self, size = -1):
  98.         return self._stupid_gzip_wrapper__data.read(size)
  99.  
  100.     
  101.     def readline(self, size = -1):
  102.         return self._stupid_gzip_wrapper__data.readline(size)
  103.  
  104.     
  105.     def readlines(self, sizehint = -1):
  106.         return self._stupid_gzip_wrapper__data.readlines(sizehint)
  107.  
  108.     
  109.     def __getattr__(self, name):
  110.         return getattr(self._response, name)
  111.  
  112.  
  113.  
  114. class HTTPGzipProcessor(urllib2.BaseHandler):
  115.     handler_order = 200
  116.     
  117.     def http_request(self, request):
  118.         request.add_header('Accept-Encoding', 'gzip')
  119.         return request
  120.  
  121.     
  122.     def http_response(self, request, response):
  123.         enc_hdrs = response.info().getheaders('Content-encoding')
  124.         for enc_hdr in enc_hdrs:
  125.             if 'gzip' in enc_hdr or 'compress' in enc_hdr:
  126.                 return stupid_gzip_wrapper(response)
  127.         
  128.         return response
  129.  
  130.     https_response = http_response
  131.  
  132.