pyblosxom wsgi application

Steven Armstrong <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
Hi all

Here's the wsgi application launcher for Pyblosxom 1.2+.

cheers
Steven

ps: check the 'Dependencies' section in the file.
wsgi_app.py (text/x-python, 3.6 KB)
"""
WSGI application launcher function for Pyblosxom.


Dependencies:
    - Pyblosxom 1.2+
    - wsgiref library from http://cvs.eby-sarna.com/wsgiref/
    - if run with mod_python: mp_wsgi_handler.py (mod_python wsgi wrapper)
        from http://www.c-area.ch/code/
    - if run with twisted: twisted_wsgi.py (twisted wsgi wrapper)
        from http://svn.colorstudy.com/trunk/WSGIKit/wsgikit/


Configuration:
    - put this file in your weblog folder (where pyblosxom.cgi is)
    - mod_python:
        Note: If you have a folder in your document root that has the same name as the 
          Location used below, the SCRIPT_NAME and PATH_INFO variables will be broken.
          You should keep your blog's files outside your document root.
        <Location /weblog>
            PythonDebug On
            SetHandler python-program
            # set PythonPath to the folders containing the files.
            PythonPath "['/path/to/mp_wsgi_handler', '/path/to/wsgi_app']+sys.path"
            PythonHandler mp_wsgi_handler::wsgi_handler
            # this should be the same as Location
            PythonOption ApplicationPath /weblog
            PythonOption application wsgi_app::application
        </Location>
        

Todo:
    - more documentation
    - example configuration for twisted


Revisions:
    $Log: wsgi_app.py,v $
    Revision 1.8  2005/02/05 19:13:55  sar
    added exception handling

$Id: wsgi_app.py,v 1.8 2005/02/05 19:13:55 sar Exp $
"""
__author__ = "Steven Armstrong <sa at c-area dot ch>"
__version__ = "$Revision: 1.8 $ $Date: 2005/02/05 19:13:55 $"
__url__ = "http://www.c-area.ch/code/"
__description__ = "WSGI application launcher for Pyblosxom"
__license__ = "Pyblosxom"


# Python imports
import sys

# Pyblosxom imports
from Pyblosxom.pyblosxom import PyBlosxom
from Pyblosxom import tools
from config import py as cfg

_debug = False

def _getExcInfo():
    try: from cStringIO import StringIO
    except ImportError: from StringIO import StringIO
    import traceback
    (exc_type, exc_value, tb) = sys.exc_info()
    exc_file = StringIO()
    traceback.print_exception(exc_type, exc_value, tb, file=exc_file)
    exc_string = exc_file.getvalue()
    return exc_string

def application(env, start_response):

    try: # regular application code here

        # ensure that PATH_INFO exists. a few plugins break if this is missing.
        if not 'PATH_INFO' in env:
            env['PATH_INFO'] = ""
    
        p = PyBlosxom(cfg, env)
        p.run()
    
        response = p.getResponse()
    
        if _debug:
            log = tools.getLogger("pyblosxom")            
            log.setLevel(log.DEBUG)
            log.debug("status: %s", response.status)
            log.debug("headers: %s", response.headers)

        write = start_response(response.status, list(response.headers.items()))
        response.seek(0)
    
        # this would be the recommended way
        #return response
    
        # but this seems to be faster
        write(response.read())
        return []

    except:
        # XXX should trap runtime issues like MemoryError, KeyboardInterrupt
        #     in a separate handler before this bare 'except:'...
        
        # log the exception to /<logdir>/error.log
        tools.log_exception()
        status = "500 Oops"
        response_headers = [("content-type","text/plain")]
        start_response(status, response_headers)
        result = ["Pyblosxom WSGI application: A server error occurred.  Please contact the administrator."]
        if _debug:
            result.append("\n")
            result.append(_getExcInfo())
        return result
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.