home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / modules / compress.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  2.5 KB  |  85 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: compress.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. from spyceModule import spyceModule
  10. from cStringIO import StringIO
  11. import gzip, string, spyceUtil
  12.  
  13. __doc__ = '''Compress module provides dynamic compression.'''
  14.  
  15. OUTPUT_POSITION = 95
  16.  
  17. class compress(spyceModule):
  18.   def start(self):
  19.     # install compress filter into response module
  20.     self._filter = FilterCompress(self)
  21.     self._api.getModule('response').addFilter(OUTPUT_POSITION, self._filter)
  22.   def finish(self, theError=None):
  23.     if not theError:
  24.       self._filter.close()
  25.   def init(self, gzip=None, spaces=None):
  26.     if gzip: self.gzip()
  27.     if spaces: self.spaces()
  28.   def spaces(self, on=1):
  29.     self._filter.setSpaceMode(on)
  30.   def gzip(self, level=None):
  31.     self._filter.setGZIP(level)
  32.  
  33. class FilterCompress(Filter):
  34.   def __init__(self, module):
  35.     self._module = module
  36.     self._buf = StringIO()
  37.     self._flushed = 0
  38.     self._space = 0
  39.     self._gzip = None
  40.     self._gzipfile = None
  41.   def writeStatic(self, s):
  42.     self.write(s)
  43.   def writeExpr(self, s, **kwargs):
  44.     self.write(str(s))
  45.   def setSpaceMode(self, on):
  46.     self._space = on
  47.   def setGZIP(self, level):
  48.     if self._flushed:
  49.       raise 'output already flushed'
  50.     encodings = self._module._api.getModule('request').getHeader('Accept-Encoding')
  51.     if not encodings or string.find(encodings, 'gzip')<0:
  52.       return  # ensure the browser can cope
  53.     if level==0:
  54.       self._gzip = None
  55.       self._gzipfile = None
  56.     else:
  57.       self._gzipfile = StringIO()
  58.       if level:
  59.         self._gzip = gzip.GzipFile(mode='w', fileobj=self._gzipfile, compresslevel=level)
  60.       else:
  61.         self._gzip = gzip.GzipFile(mode='w', fileobj=self._gzipfile)
  62.   def write(self, s, *args, **kwargs):
  63.     self._buf.write(s)
  64.   def flushImpl(self, final=0):
  65.     self._flushed = 1
  66.     s = self._buf.getvalue()
  67.     self._buf = StringIO()
  68.     if self._space:
  69.       s = spyceUtil.spaceCompact(s)
  70.     if self._gzip:
  71.       self._gzip.write(s)
  72.       self._gzip.flush()
  73.       if final:
  74.         self._module._api.getModule('response').addHeader('Content-Encoding', 'gzip')
  75.         self._gzip.close()
  76.         self._gzip = None
  77.       s = self._gzipfile.getvalue()
  78.       self._gzipfile.truncate(0)
  79.     self.next.write(s)
  80.   def clearImpl(self):
  81.     self._buf = StringIO()
  82.   def close(self):
  83.     self.flushImpl(1)
  84.  
  85.