spyceModpy.py :  » Web-Frameworks » Spyce » spyce-2.1 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Spyce 
Spyce » spyce 2.1 » spyceModpy.py
##################################################
# SPYCE - Python-based HTML Scripting
# Copyright (c) 2002 Rimon Barr.
#
# Refer to spyce.py
# CVS: $Id: spyceModpy.py 726 2005-05-13 16:29:50Z jbe $
##################################################

import string, sys, os
import spyce, spyceException, spyceUtil, spycePreload

__doc__ = '''Apache mod_python-based Spyce entry point.'''

##################################################
# Request / response handlers
#

import _apache   # fails when not running under apache
from mod_python import apache

class NoFlush:
  "Elide flush calls"
  def __init__(self, apacheRequest):
    self.write = apacheRequest.write
  def flush(self):
    pass

# make sure that both sets of classes have identical functionality
class spyceModpyRequest(spyce.spyceRequest):
  "Apache Spyce request object. (see spyce.spyceRequest)"
  def __init__(self, apacheRequest):
    spyce.spyceRequest.__init__(self)
    self._in = apacheRequest
  def env(self, name=None):
    return spyceUtil.extractValue(self._in.subprocess_env, name)
  def getHeader(self, type=None):
    if type:
      if self._in.headers_in.has_key(type):
        return self._in.headers_in[type]
    else: return self._in.headers_in
  def getServerID(self):
    return os.getpid()

class spyceModpyResponse(spyce.spyceResponse):
  "Apache Spyce response object. (see spyce.spyceResponse)"
  def __init__(self, apacheRequest):
    spyce.spyceResponse.__init__(self)
    self.origout = apacheRequest
    self.out = spyceUtil.BufferedOutput(NoFlush(apacheRequest))
    self.isCTset = 0
    self.headersSent = 0
    self.returncode = self.origout.status = self.RETURN_OK
    # functions (for performance)
    self.write = self.out.write
    self.writeErr = sys.stderr.write
  def close(self):
    self.flush()
    #self.out.close()
  def clear(self):
    self.out.clear()
  def sendHeaders(self):
    if self.headersSent: 
      return
    if not self.isCTset:
      self.setContentType("text/html")
    self.origout.send_http_header()
    self.headersSent = 1
  def clearHeaders(self):
    if self.headersSent:
      raise 'headers already sent'
    for header in self.origout.headers_out.keys():
      del self.origout.headers_out[header]
  def setContentType(self, content_type):
    if self.headersSent:
      raise 'headers already sent'
    self.origout.content_type = content_type
    self.isCTset = 1
  def setReturnCode(self, code):
    if self.headersSent:
      raise 'headers already sent'
    self.returncode = self.origout.status = code
  def addHeader(self, type, data, replace=0):
    if self.headersSent:
      raise 'headers already sent'
    if replace:
      self.origout.headers_out[type] = data
    else:
      self.origout.headers_out.add(type, data)
  def flush(self, stopFlag=0):
    if stopFlag: return
    self.sendHeaders()
    self.out.flush()
  def unbuffer(self):
    self.flush()
    self.out.unbuffer()

##################################################
# Apache config
#

def getApacheConfig(apachereq, param, default=None):
  "Return Apache mod_python configuration parameter."
  try:
    return apachereq.get_options()[param]
  except:
    return default

##################################################
# Apache entry point
#

configmod = None

def spyceMain(apacheRequest):
  "Apache entry point."
  os.environ[spyce.SPYCE_ENTRY] = 'modpy'
  apacheRequest.add_common_vars()
  request = spyceModpyRequest(apacheRequest)
  response = spyceModpyResponse(apacheRequest)
  filename = apacheRequest.filename
  global configmod
  if configmod == None:
    configFile = getApacheConfig(apacheRequest, 'SPYCE_CONFIG')
    configmod = spycePreload.getConfigModule(configFile)
  try:
    result = spyce.spyceFileHandler(request, response, filename, config=configmod)
  except (spyceException.spyceForbidden, spyceException.spyceNotFound), e:
    response.clear()
    response.setContentType('text/plain')
    response.write(str(e)+'\n')
  except:
    response.clear()
    response.setContentType('text/plain')
    response.write(spyceUtil.exceptionString()+'\n')
  try:
    response.flush()
  except: pass
  return apache.OK

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.