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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import struct
  5. import time
  6. import cherrypy
  7.  
  8. def decode(encoding = None, default_encoding = 'utf-8'):
  9.     if not encoding:
  10.         ct = cherrypy.request.headers.elements('Content-Type')
  11.         if ct:
  12.             ct = ct[0]
  13.             encoding = ct.params.get('charset', None)
  14.             if not encoding and ct.value.lower().startswith('text/'):
  15.                 encoding = 'ISO-8859-1'
  16.             
  17.         
  18.         if not encoding:
  19.             encoding = default_encoding
  20.         
  21.     
  22.     
  23.     try:
  24.         decode_params(encoding)
  25.     except UnicodeDecodeError:
  26.         decode_params('ISO-8859-1')
  27.  
  28.  
  29.  
  30. def decode_params(encoding):
  31.     decoded_params = { }
  32.     for key, value in cherrypy.request.params.items():
  33.         if not hasattr(value, 'file'):
  34.             if isinstance(value, list):
  35.                 value = [ v.decode(encoding) for v in value ]
  36.             elif isinstance(value, str):
  37.                 value = value.decode(encoding)
  38.             
  39.         
  40.         decoded_params[key] = value
  41.     
  42.     cherrypy.request.params = decoded_params
  43.  
  44.  
  45. def encode(encoding = None, errors = 'strict', text_only = True, add_charset = True):
  46.     if getattr(cherrypy.request, '_encoding_attempted', False):
  47.         return None
  48.     cherrypy.request._encoding_attempted = True
  49.     ct = cherrypy.response.headers.elements('Content-Type')
  50.     if ct:
  51.         ct = ct[0]
  52.         if not text_only or ct.value.lower().startswith('text/'):
  53.             ct.params['charset'] = find_acceptable_charset(encoding, errors = errors)
  54.             if add_charset:
  55.                 cherrypy.response.headers['Content-Type'] = str(ct)
  56.             
  57.         
  58.     
  59.  
  60.  
  61. def encode_stream(encoding, errors = 'strict'):
  62.     
  63.     def encoder(body):
  64.         for chunk in body:
  65.             if isinstance(chunk, unicode):
  66.                 chunk = chunk.encode(encoding, errors)
  67.             
  68.             yield chunk
  69.         
  70.  
  71.     cherrypy.response.body = encoder(cherrypy.response.body)
  72.     return True
  73.  
  74.  
  75. def encode_string(encoding, errors = 'strict'):
  76.     
  77.     try:
  78.         body = []
  79.         for chunk in cherrypy.response.body:
  80.             if isinstance(chunk, unicode):
  81.                 chunk = chunk.encode(encoding, errors)
  82.             
  83.             body.append(chunk)
  84.         
  85.         cherrypy.response.body = body
  86.     except (LookupError, UnicodeError):
  87.         return False
  88.  
  89.     return True
  90.  
  91.  
  92. def find_acceptable_charset(encoding = None, default_encoding = 'utf-8', errors = 'strict'):
  93.     response = cherrypy.response
  94.     if cherrypy.response.stream:
  95.         encoder = encode_stream
  96.     else:
  97.         response.collapse_body()
  98.         encoder = encode_string
  99.         if response.headers.has_key('Content-Length'):
  100.             del response.headers['Content-Length']
  101.         
  102.     encs = cherrypy.request.headers.elements('Accept-Charset')
  103.     charsets = [ enc.value.lower() for enc in encs ]
  104.     attempted_charsets = []
  105.     if encoding is not None:
  106.         encoding = encoding.lower()
  107.         if not charsets and '*' in charsets or encoding in charsets:
  108.             if encoder(encoding, errors):
  109.                 return encoding
  110.         
  111.     elif not encs:
  112.         if encoder(default_encoding, errors):
  113.             return default_encoding
  114.         raise cherrypy.HTTPError(500, failmsg % default_encoding)
  115.     elif '*' not in charsets:
  116.         iso = 'iso-8859-1'
  117.         if iso not in charsets:
  118.             attempted_charsets.append(iso)
  119.             if encoder(iso, errors):
  120.                 return iso
  121.         
  122.     
  123.     for element in encs:
  124.         if element.qvalue > 0:
  125.             pass
  126.         None if element.value == '*' else encoding not in attempted_charsets
  127.     
  128.     ac = cherrypy.request.headers.get('Accept-Charset')
  129.     if ac is None:
  130.         msg = 'Your client did not send an Accept-Charset header.'
  131.     else:
  132.         msg = 'Your client sent this Accept-Charset header: %s.' % ac
  133.     msg += ' We tried these charsets: %s.' % ', '.join(attempted_charsets)
  134.     raise cherrypy.HTTPError(406, msg)
  135.  
  136.  
  137. def compress(body, compress_level):
  138.     import zlib
  139.     yield '\x1f\x8b'
  140.     yield '\x08'
  141.     yield '\x00'
  142.     yield struct.pack('<L', long(time.time()))
  143.     yield '\x02'
  144.     yield '\xff'
  145.     crc = zlib.crc32('')
  146.     size = 0
  147.     zobj = zlib.compressobj(compress_level, zlib.DEFLATED, -(zlib.MAX_WBITS), zlib.DEF_MEM_LEVEL, 0)
  148.     for line in body:
  149.         size += len(line)
  150.         crc = zlib.crc32(line, crc)
  151.         yield zobj.compress(line)
  152.     
  153.     yield zobj.flush()
  154.     yield struct.pack('<l', crc)
  155.     yield struct.pack('<L', size & 0xFFFFFFFFL)
  156.  
  157.  
  158. def decompress(body):
  159.     import gzip
  160.     import StringIO
  161.     zbuf = StringIO.StringIO()
  162.     zbuf.write(body)
  163.     zbuf.seek(0)
  164.     zfile = gzip.GzipFile(mode = 'rb', fileobj = zbuf)
  165.     data = zfile.read()
  166.     zfile.close()
  167.     return data
  168.  
  169.  
  170. def gzip(compress_level = 9, mime_types = [
  171.     'text/html',
  172.     'text/plain']):
  173.     response = cherrypy.response
  174.     if not response.body:
  175.         return None
  176.     if getattr(cherrypy.request, 'cached', False):
  177.         return None
  178.     acceptable = cherrypy.request.headers.elements('Accept-Encoding')
  179.     if not acceptable:
  180.         return None
  181.     ct = response.headers.get('Content-Type', '').split(';')[0]
  182.     for coding in acceptable:
  183.         if coding.value == 'identity' and coding.qvalue != 0:
  184.             return None
  185.         if coding.value in ('gzip', 'x-gzip'):
  186.             if coding.qvalue == 0:
  187.                 return None
  188.             return None
  189.     
  190.     cherrypy.HTTPError(406, 'identity, gzip').set_response()
  191.  
  192.