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 / spyceUtil.py < prev    next >
Encoding:
Python Source  |  2008-11-03  |  5.2 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: spyceUtil.py 5659 2006-04-27 16:15:15Z jwnmulder $
  7. ##################################################
  8.  
  9. import sys, re, string
  10. from cStringIO import StringIO
  11.  
  12. __doc__ = '''Spyce utility functions'''
  13.  
  14. ##################################################
  15. # Current exception string
  16. #
  17.  
  18. def exceptionString():
  19.   "Generate string out of current exception."
  20.   import traceback, string
  21.   ex=sys.exc_info()
  22.   ex=traceback.format_exception(ex[0], ex[1], ex[2])
  23.   ex=string.join(ex, '')
  24.   return ex
  25.  
  26. ##################################################
  27. # Return hashtable value, or entire hashtable
  28. #
  29.  
  30. def extractValue(hash, key, default=None):
  31.   """Extract value from dictionary, if it exists. 
  32.   If key is none, return entire dictionary"""
  33.   if key==None: return hash
  34.   if hash.has_key(key): return hash[key]
  35.   return default
  36.  
  37. ##################################################
  38. # Return hashtable value, or entire hashtable
  39. #
  40.  
  41. RE_SPACE_REDUCE = re.compile('[ \t][ \t]+')
  42. RE_SPACE_NEWLINE_REDUCE = re.compile('\n\s+')
  43. def spaceCompact(text):
  44.   text = string.split(text, '\n')
  45.   text = map(lambda s: RE_SPACE_REDUCE.sub(' ', s), text)
  46.   text = string.join(text, '\n')
  47.   text = RE_SPACE_NEWLINE_REDUCE.sub('\n', text)
  48.   return text
  49.  
  50. ##################################################
  51. # Threading helpers
  52. #
  53.  
  54. class NonThreadedWriter:
  55.   '''Simple writer'''
  56.   def __init__(self, o=None):
  57.     self.setObject(o)
  58.   def setObject(self, o=None):
  59.     self.__dict__['_o'] = o
  60.     if o:
  61.       self.__dict__['write'] = o.write
  62.       self.__dict__['close'] = o.close
  63.       self.__dict__['flush'] = o.flush
  64.     else:
  65.       self.__dict__['write'] = None
  66.       self.__dict__['close'] = None
  67.       self.__dict__['flush'] = None
  68.   def getObject(self):
  69.     return self._o
  70.   def clearObject(self):
  71.     self.setObject()
  72.   def __getattr__(self, name):
  73.     if name=='softspace':  # performance
  74.       return self.getObject().softspace
  75.     return eval('self._o.%s'%name)
  76.   def __setattr__(self, name, value):
  77.     if name=='softspace': # performance
  78.       self.getObject().softspace = value
  79.     eval('self._o.%s=value'%name)
  80.   def __delattr__(self, name):
  81.     return eval('del self.getObject().%s'%name)
  82.  
  83. class ThreadedWriter:
  84.   '''Thread-safe writer'''
  85.   def __init__(self, o=None):
  86.     try: import thread,threading
  87.     except: raise 'threading not supported!'
  88.     self.__dict__['_currentThread'] = threading.currentThread
  89.     self.__dict__['_o'] = o
  90.   def setObject(self, o=None):
  91.     self._currentThread().threadOut = o
  92.     self._currentThread().threadWrite = o.write
  93.   def getObject(self):
  94.     try: return self._currentThread().threadOut
  95.     except AttributeError: return self._o
  96.   def clearObject(self):
  97.     try: del self._currentThread().threadOut
  98.     except AttributeError: pass
  99.   def write(self, s):
  100.     try: self._currentThread().threadWrite(s)
  101.     except AttributeError: self._o.write(s)
  102.   def close(self):
  103.     self.getObject().close()
  104.   def flush(self):
  105.     self.getObject().flush()
  106.   def __getattr__(self, name):
  107.     if name=='softspace':  # performance
  108.       return self.getObject().softspace
  109.     return eval('self.getObject().%s'%name)
  110.   def __setattr__(self, name, value):
  111.     if name=='softspace': # performance
  112.       self.getObject().softspace = value
  113.     eval('self.getObject().%s=value'%name)
  114.   def __delattr__(self, name):
  115.     return eval('del self.getObject().%s'%name)
  116.  
  117. ##################################################
  118. # Output
  119. #
  120.  
  121. class BufferedOutput:
  122.   "Buffered output stream."
  123.   def __init__(self, out):
  124.     self.buf = StringIO()
  125.     self.writeBuf = self.buf.write
  126.     self.out = out
  127.     self.closed = 0
  128.   def write(self, s):
  129.     if self.closed:
  130.       raise 'output stream closed'
  131.     self.writeBuf(s)
  132.   def clear(self):
  133.     if not self.buf:
  134.       raise 'stream is not buffered'
  135.     self.buf = StringIO()
  136.     self.writeBuf = self.buf.write
  137.   def flush(self, stopFlag=0):
  138.     if stopFlag: return
  139.     if self.buf and self.buf.getvalue():
  140.       self.out.write(self.buf.getvalue())
  141.       self.out.flush()
  142.       self.clear()
  143.   def close(self):
  144.     if self.closed:
  145.       raise 'output stream closed'
  146.     self.closed = 1
  147.     self.flush()
  148.     self.out.close()
  149.   def unbuffer(self):
  150.     "Turn this into a pass-through."
  151.     if self.buf:
  152.       self.flush()
  153.       self.buf = None
  154.       self.writeBuf = self.out.write
  155.   def getOut(self):
  156.     "Return underlying output stream."
  157.     return self.out
  158.  
  159.  
  160. class NoCloseOut:
  161.   def __init__(self, out):
  162.     self.out = out
  163.     self.write = self.out.write
  164.     self.flush = self.out.flush
  165.   def close(self):
  166.     pass
  167.   def getOut(self):
  168.     return self.out
  169.  
  170. def panicOutput(response, s):
  171.   import cgi
  172.   # output to browser, if possible
  173.   try: response.clear()
  174.   except: pass
  175.   try:
  176.     response.write('<html><pre>\n')
  177.     response.write('Spyce Panic!<br>\n')
  178.     response.write(cgi.escape(s))
  179.     response.write('</pre></html>\n')
  180.     response.returncode = response.RETURN_OK
  181.     response.flush()
  182.   except: pass
  183.   # output to error log
  184.   sys.stderr.write(s)
  185.   sys.stderr.flush()
  186.   sys.exit(1)
  187.