home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / spyceModpy.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  4.1 KB  |  139 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: spyceModpy.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. import string, sys, os
  10. import spyce, spyceException, spyceUtil
  11.  
  12. __doc__ = '''Apache mod_python-based Spyce entry point.'''
  13.  
  14. ##################################################
  15. # Request / response handlers
  16. #
  17.  
  18. import _apache   # fails when not running under apache
  19. from mod_python import apache
  20.  
  21. class NoFlush:
  22.   "Elide flush calls"
  23.   def __init__(self, apacheRequest):
  24.     self.write = apacheRequest.write
  25.   def flush(self):
  26.     pass
  27.  
  28. # make sure that both sets of classes have identical functionality
  29. class spyceModpyRequest(spyce.spyceRequest):
  30.   "Apache Spyce request object. (see spyce.spyceRequest)"
  31.   def __init__(self, apacheRequest):
  32.     spyce.spyceRequest.__init__(self)
  33.     self._in = apacheRequest
  34.   def env(self, name=None):
  35.     return spyceUtil.extractValue(self._in.subprocess_env, name)
  36.   def getHeader(self, type=None):
  37.     if type:
  38.       if self._in.headers_in.has_key(type):
  39.         return self._in.headers_in[type]
  40.     else: return self._in.headers_in
  41.   def getServerID(self):
  42.     return os.getpid()
  43.  
  44. class spyceModpyResponse(spyce.spyceResponse):
  45.   "Apache Spyce response object. (see spyce.spyceResponse)"
  46.   def __init__(self, apacheRequest):
  47.     spyce.spyceResponse.__init__(self)
  48.     self.origout = apacheRequest
  49.     self.out = spyceUtil.BufferedOutput(NoFlush(apacheRequest))
  50.     self.isCTset = 0
  51.     self.headersSent = 0
  52.     self.returncode = self.origout.status = self.RETURN_OK
  53.     # functions (for performance)
  54.     self.write = self.out.write
  55.     self.writeErr = sys.stderr.write
  56.   def close(self):
  57.     self.flush()
  58.     #self.out.close()
  59.   def clear(self):
  60.     self.out.clear()
  61.   def sendHeaders(self):
  62.     if self.headersSent: 
  63.       return
  64.     if not self.isCTset:
  65.       self.setContentType("text/html")
  66.     self.origout.send_http_header()
  67.     self.headersSent = 1
  68.   def clearHeaders(self):
  69.     if self.headersSent:
  70.       raise 'headers already sent'
  71.     for header in self.origout.headers_out.keys():
  72.       del self.origout.headers_out[header]
  73.   def setContentType(self, content_type):
  74.     if self.headersSent:
  75.       raise 'headers already sent'
  76.     self.origout.content_type = content_type
  77.     self.isCTset = 1
  78.   def setReturnCode(self, code):
  79.     if self.headersSent:
  80.       raise 'headers already sent'
  81.     self.returncode = self.origout.status = code
  82.   def addHeader(self, type, data, replace=0):
  83.     if self.headersSent:
  84.       raise 'headers already sent'
  85.     if replace:
  86.       self.origout.headers_out[type] = data
  87.     else:
  88.       self.origout.headers_out.add(type, data)
  89.   def flush(self, stopFlag=0):
  90.     if stopFlag: return
  91.     self.sendHeaders()
  92.     self.out.flush()
  93.   def unbuffer(self):
  94.     self.flush()
  95.     self.out.unbuffer()
  96.  
  97. ##################################################
  98. # Apache config
  99. #
  100.  
  101. def getApacheConfig(apachereq, param, default=None):
  102.   "Return Apache mod_python configuration parameter."
  103.   try:
  104.     return apachereq.get_options()[param]
  105.   except:
  106.     return default
  107.  
  108. ##################################################
  109. # Apache entry point
  110. #
  111.  
  112. CONFIG_FILE = None
  113.  
  114. def spyceMain(apacheRequest):
  115.   "Apache entry point."
  116.   os.environ[spyce.SPYCE_ENTRY] = 'modpy'
  117.   apacheRequest.add_common_vars()
  118.   request = spyceModpyRequest(apacheRequest)
  119.   response = spyceModpyResponse(apacheRequest)
  120.   filename = apacheRequest.filename
  121.   global CONFIG_FILE
  122.   if CONFIG_FILE==None:
  123.     CONFIG_FILE = getApacheConfig(apacheRequest, 'SPYCE_CONFIG', None)
  124.   try:
  125.     result = spyce.spyceFileHandler(request, response, filename, config_file=CONFIG_FILE )
  126.   except (spyceException.spyceForbidden, spyceException.spyceNotFound), e:
  127.     response.clear()
  128.     response.setContentType('text/plain')
  129.     response.write(str(e)+'\n')
  130.   except:
  131.     response.clear()
  132.     response.setContentType('text/plain')
  133.     response.write(spyceUtil.exceptionString()+'\n')
  134.   try:
  135.     response.flush()
  136.   except: pass
  137.   return apache.OK
  138.  
  139.