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 / stdout.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  3.6 KB  |  105 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: stdout.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. from spyceModule import spyceModule
  10. from spyceUtil import NoCloseOut
  11. from cStringIO import StringIO
  12.  
  13. __doc__ = '''Sets the thread-safe server stdout to the response object for
  14. convenience of using print statements, and supports output redirection.'''
  15.  
  16. class stdout(spyceModule):
  17.   def start(self):
  18.     # thread-safe stdout swap
  19.     self.stdout = self._api.getStdout()
  20.     self._api.setStdout(myResponseWrapper(self))
  21.     # output redirection stack
  22.     self.outputStack = []
  23.     # memoize storage
  24.     try: self.memoizeCache = self._api.getServerObject().memoized
  25.     except AttributeError:
  26.       self.memoizeCache = self._api.getServerObject().memoized = {}
  27.   def finish(self, theError=None):
  28.     # close all redirects
  29.     while self.outputStack:
  30.       self.pop()
  31.     # thread-safe stdout swap back
  32.     self._api.setStdout(self.stdout)
  33.   def push(self, file=None):
  34.     'Redirect stdout to buffer'
  35.     old_response = self._api.getResponse()
  36.     old_response_mod = self._api.getModule('response')
  37.     new_response = spyceCaptureResponse(old_response)
  38.     self._api.setResponse(new_response)
  39.     new_response_mod = self._api.spyceModule('response', 'response.py')(self._api)
  40.     self._api.setModule('response', new_response_mod)
  41.     new_response_mod.start()
  42.     self.outputStack.append( (file, old_response, old_response_mod) )
  43.   def pop(self):
  44.     'Return buffer value, and possible write to file'
  45.     self._api.getModule('response').finish()
  46.     buffer = self._api.getResponse().getCapturedOutput()
  47.     file, old_response, old_response_mod = self.outputStack.pop()
  48.     self._api.setModule('response', old_response_mod)
  49.     self._api.setResponse(old_response)
  50.     if file:
  51.       file = os.path.join(os.path.dirname(self._api.getFilename()), file)
  52.       out = None
  53.       try:
  54.         out = open(file, 'w')
  55.         out.write(buffer)
  56.       finally:
  57.         if out: out.close()
  58.     return buffer
  59.   def capture(self, _spyceReserved, *args, **kwargs):
  60.     'Capture the output side-effects of a function'
  61.     f = _spyceReserved # placeholder not to collide with kwargs
  62.     self.push()
  63.     r = apply(f, args, kwargs)
  64.     s = self.pop()
  65.     return r, s
  66.   def __repr__(self):
  67.     return 'depth: %s' % len(self.outputStack)
  68.  
  69. class myResponseWrapper:
  70.   def __init__(self, mod):
  71.     self._mod = mod
  72.     mod._api.registerModuleCallback(self.syncResponse)
  73.     self.syncResponse()
  74.   def syncResponse(self):
  75.     response = self._mod._api.getModule('response')
  76.     # functions (for performance)
  77.     self.write = response.write
  78.     self.writeln = response.writeln
  79.     self.flush = response.flush
  80.   def close(self):
  81.     raise 'method not allowed'
  82.  
  83. class spyceCaptureResponse:
  84.   "Capture output, and let everything else through."
  85.   def __init__(self, old_response):
  86.     self._old_response = old_response
  87.     self._buf = StringIO()
  88.   def write(self, s):
  89.     self._buf.write(s)
  90.   def close(self):
  91.     raise 'cannot close output while capturing output'
  92.   def clear(self):
  93.     self._buf = StringIO()
  94.   def sendHeaders(self):
  95.     raise 'cannot sendHeaders while capturing output!'
  96.   def flush(self, stopFlag=0):
  97.     pass
  98.   def unbuffer(self):
  99.     raise 'cannot unbuffer output while capturing output!'
  100.   def __getattr__(self, name):
  101.     return eval('self._old_response.%s'%name)
  102.   def getCapturedOutput(self):
  103.     return self._buf.getvalue()
  104.  
  105.