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