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