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 / response.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  6.3 KB  |  187 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: response.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. from spyceModule import spyceModule
  10. import string, time
  11.  
  12. __doc__ = '''Response module provides user control over the browser
  13. response.'''
  14.  
  15. class response(spyceModule):
  16.   def start(self):
  17.     self.clearFilters()
  18.     self._unbuffer = 0
  19.     self._ioerror = 0
  20.     self._api.registerResponseCallback(self.syncResponse)
  21.     self.syncResponse()
  22.   def syncResponse(self):
  23.     self._response = self._api.getResponse()
  24.   def finish(self, theError=None):
  25.     self._api.unregisterResponseCallback(self.syncResponse)
  26.     if not theError:
  27.       self._filter.flush(1)
  28.   def clearFilters(self):
  29.     self._filter = FilterUnify(self)
  30.     self._filterList = [(99, self._filter)]
  31.   def addFilter(self, level, filter):
  32.     'Inject filter functions into output stream at given level of precedence'
  33.     filterExists = None
  34.     for i in range(len(self._filterList)):
  35.       l, _ = self._filterList[i]
  36.       if l==level:
  37.         _, filterExists = self._filterList[i]
  38.         del self._filterList[i]
  39.         break
  40.     if filter:
  41.       self._filterList.append((level, filter))
  42.       self._filterList.sort()
  43.     for i in range(len(self._filterList)-1):
  44.       l1, f1 = self._filterList[i]
  45.       l2, f2 = self._filterList[i+1]
  46.       f1.setNext(f2)
  47.     _, self._filter = self._filterList[0]
  48.     return filterExists
  49.  
  50.   # user functions
  51.   def write(self, s):
  52.     "Write out a dynamic (code) string."
  53.     try:
  54.       self._filter.write(s)
  55.       if self._unbuffer: self.flush()
  56.     except IOError:
  57.       self._ioerror = 1
  58.   def writeln(self, s):
  59.     "Writeln out a dynamic (code) string."
  60.     self.write(s+'\n')
  61.   def writeStatic(self, s):
  62.     "Write out a static string."
  63.     try:
  64.       self._filter.writeStatic(s)
  65.       if self._unbuffer: self.flush()
  66.     except IOError:
  67.       self._ioerror = 1
  68.   def writeExpr(self, s, **kwargs):
  69.     "Write out an expression result."
  70.     try:
  71.       apply(self._filter.writeExpr, (s,), kwargs)
  72.       if self._unbuffer: self.flush()
  73.     except IOError:
  74.       self._ioerror = 1
  75.   def clear(self):
  76.     "Clear the output buffer. (must not be unbuffered)"
  77.     self._filter.clear()
  78.   def flush(self, stopFlag=0):
  79.     "Flush resident buffer."
  80.     try:
  81.       self._filter.flush(stopFlag)
  82.     except IOError:
  83.       self._ioerror = 1
  84.   def setContentType(self, ct):
  85.     "Set document content type. (must not be unbuffered)"
  86.     self._response.setContentType(ct)
  87.   def setReturnCode(self, code):
  88.     "Set HTTP return (status) code"
  89.     self._response.setReturnCode(int(code))
  90.   def isCancelled(self):
  91.     return self._ioerror
  92.   def addHeader(self, type, data, replace=0):
  93.     "Add an HTTP header. (must not be unbuffered)"
  94.     if string.find(type, ':') != -1:
  95.       raise 'HTTP header type should not contain ":" (colon).'
  96.     self._response.addHeader(type, data, replace)
  97.   def clearHeaders(self):
  98.     "Clear all HTTP headers (must not be unbuffered)"
  99.     self._response.clearHeaders()
  100.   def unbuffer(self):
  101.     "Turn off output stream buffering; flush immediately to browser."
  102.     self._unbuffer = 1
  103.     self.flush()
  104.   def timestamp(self, thetime=None):
  105.     "Timestamp response with a HTTP Date header"
  106.     self.addHeader('Date', _genTimestampString(thetime), 1)
  107.   def expires(self, thetime=None):
  108.     "Add HTTP expiration headers"
  109.     self.addHeader('Expires', _genTimestampString(thetime), 1)
  110.   def expiresRel(self, secs=0):
  111.     "Set response expiration (relative to now) with a HTTP Expires header"
  112.     self.expires(int(time.time())+secs)
  113.   def lastModified(self, thetime=-1):
  114.     "Set last modification time"
  115.     if thetime==-1:
  116.       filename = self._api.getFilename()
  117.       if not filename or not os.path.exists(filename):
  118.         raise 'request filename not found; can not determine last modification time'
  119.       thetime = os.stat(filename)[9] # file ctime
  120.     self.addHeader('Last-Modified', _genTimestampString(thetime), 1)
  121.     # ensure last modified before timestamp, at least when we're generating it
  122.     if thetime==None: self.timestamp()
  123.   def uncacheable(self):
  124.     "Ensure that compliant clients and proxies don't cache this response"
  125.     self.addHeader('Cache-Control', 'no-store, no-cache, must-revalidate')
  126.     self.addHeader('Pragma', 'no-cache')
  127.   def __repr__(self):
  128.     s = []
  129.     s.append('filters: %s' % len(self._filterList))
  130.     s.append('unbuffered: %s' % self._unbuffer)
  131.     return string.join(s, ', ')
  132.  
  133. class Filter:
  134.   def setNext(self, filter):
  135.     self.next = filter
  136.   def write(self, s):
  137.     s = self.dynamicImpl(s)
  138.     self.next.write(s)
  139.   def writeStatic(self, s):
  140.     s = self.staticImpl(s)
  141.     self.next.writeStatic(s)
  142.   def writeExpr(self, s, **kwargs):
  143.     s = apply(self.exprImpl, (s,), kwargs)
  144.     apply(self.next.writeExpr, (s,), kwargs)
  145.   def flush(self, stopFlag=0):
  146.     self.flushImpl()
  147.     self.next.flush(stopFlag)
  148.   def clear(self):
  149.     self.clearImpl()
  150.     self.next.clear()
  151.   def dynamicImpl(self, s, *args, **kwargs):
  152.     raise 'not implemented'
  153.   def staticImpl(self, s, *args, **kwargs):
  154.     raise 'not implemented'
  155.   def exprImpl(self, s, *args, **kwargs):
  156.     raise 'not implemented'
  157.   def flushImpl(self):
  158.     raise 'not implemented'
  159.   def clearImpl(self):
  160.     raise 'not implemented'
  161.  
  162. class FilterUnify(Filter):
  163.   def __init__(self, mod):
  164.     self.mod = mod
  165.     self.mod._api.registerResponseCallback(self.syncResponse)
  166.     self.syncResponse()
  167.   def syncResponse(self):
  168.     response = self.mod._api.getResponse()
  169.     self.write = response.write
  170.     self.writeStatic = response.write
  171.     self.flush = response.flush
  172.     self.clear = response.clear
  173.   def writeExpr(self, s, **kwargs):
  174.     self.write(str(s))
  175.   def setNext(self, filter):
  176.     pass # we are always at the end
  177.  
  178. def _genTimestampString(thetime=None):
  179.   "Generate timestamp string"
  180.   if thetime==None:
  181.     thetime = int(time.time())
  182.   if type(thetime)==type(0):
  183.     thetime = time.strftime('%a, %d %b %Y %H:%M:%S %Z', time.localtime(thetime))
  184.   if type(thetime)==type(''):
  185.     return thetime
  186.   raise 'thetime value should be None or string or integer (seconds)'
  187.