home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / spyceUtil.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  4.3 KB  |  158 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 20864 2009-06-02 06:16:47Z ceros7 $
  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 ThreadedWriter:
  55.   '''Thread-safe writer'''
  56.   def __init__(self, o=None):
  57.     try: import thread,threading
  58.     except: raise 'threading not supported!'
  59.     self.__dict__['_currentThread'] = threading.currentThread
  60.     self.__dict__['_o'] = o
  61.   def setObject(self, o=None):
  62.     self._currentThread().threadOut = o
  63.     self._currentThread().threadWrite = o.write
  64.   def getObject(self):
  65.     try: return self._currentThread().threadOut
  66.     except AttributeError: return self._o
  67.   def clearObject(self):
  68.     try: del self._currentThread().threadOut
  69.     except AttributeError: pass
  70.   def write(self, s):
  71.     try: self._currentThread().threadWrite(s)
  72.     except AttributeError: self._o.write(s)
  73.   def close(self):
  74.     self.getObject().close()
  75.   def flush(self):
  76.     self.getObject().flush()
  77.   def __getattr__(self, name):
  78.     if name=='softspace':  # performance
  79.       return self.getObject().softspace
  80.     return eval('self.getObject().%s'%name)
  81.   def __setattr__(self, name, value):
  82.     if name=='softspace': # performance
  83.       self.getObject().softspace = value
  84.     eval('self.getObject().%s=value'%name)
  85.   def __delattr__(self, name):
  86.     return eval('del self.getObject().%s'%name)
  87.  
  88. ##################################################
  89. # Output
  90. #
  91.  
  92. class BufferedOutput:
  93.   "Buffered output stream."
  94.   def __init__(self, out):
  95.     self.buf = StringIO()
  96.     self.writeBuf = self.buf.write
  97.     self.out = out
  98.     self.closed = 0
  99.   def write(self, s):
  100.     if self.closed:
  101.       raise 'output stream closed'
  102.     self.writeBuf(s)
  103.   def clear(self):
  104.     if not self.buf:
  105.       raise 'stream is not buffered'
  106.     self.buf = StringIO()
  107.     self.writeBuf = self.buf.write
  108.   def flush(self, stopFlag=0):
  109.     if stopFlag: return
  110.     if self.buf and self.buf.getvalue():
  111.       self.out.write(self.buf.getvalue())
  112.       self.out.flush()
  113.       self.clear()
  114.   def close(self):
  115.     if self.closed:
  116.       raise 'output stream closed'
  117.     self.closed = 1
  118.     self.flush()
  119.     self.out.close()
  120.   def unbuffer(self):
  121.     "Turn this into a pass-through."
  122.     if self.buf:
  123.       self.flush()
  124.       self.buf = None
  125.       self.writeBuf = self.out.write
  126.   def getOut(self):
  127.     "Return underlying output stream."
  128.     return self.out
  129.  
  130.  
  131. class NoCloseOut:
  132.   def __init__(self, out):
  133.     self.out = out
  134.     self.write = self.out.write
  135.     self.flush = self.out.flush
  136.   def close(self):
  137.     pass
  138.   def getOut(self):
  139.     return self.out
  140.  
  141. def panicOutput(response, s):
  142.   import cgi
  143.   # output to browser, if possible
  144.   try: response.clear()
  145.   except: pass
  146.   try:
  147.     response.write('<html><pre>\n')
  148.     response.write('Spyce Panic!<br>\n')
  149.     response.write(cgi.escape(s))
  150.     response.write('</pre></html>\n')
  151.     response.returncode = response.RETURN_OK
  152.     response.flush()
  153.   except: pass
  154.   # output to error log
  155.   sys.stderr.write(s)
  156.   sys.stderr.flush()
  157.   sys.exit(1)
  158.