Andy's insights

Opinions, thoughts, archivements

Sat, 18 Mar 2006

Using PyBlosxom

Searching for an extendable but simple weblog system, I finally found PyBlosxom.

This one is really simple and easy to extend. As a prove of concept I wrote the mod_python hander to integrate the blog in Apache 2. There is an old code snippled for this purpose somewhere out there. But this one won't work with recend software.

#!/usr/bin/python
"""
Pyblosxom mod_python handler

Based on ideas from 
 * Wari Wahab <wari at wari.per.sg>
   http://roughingit.subtlehints.net/pyblosxom/2003/Jun/02/
 * pyblosxom.cgi 1.3.2
"""

from mod_python import apache
import sys
sys.path.append('/etc/pyblosxom')
from config import py as cfg

if cfg.has_key("codebase"):
    sys.path.insert(0, cfg["codebase"])

from Pyblosxom.pyblosxom import PyBlosxom

__author__ = 'Andreas Stricker <rapidmax@gmx.net>'
__version__ = "1.3.2"
__copyright__ = "Copyright (c) 2006 Andreas Stricker"
__license__ = "Python"

def handler(req):
    req.content_type = 'text/html'
    req.add_common_vars()

    env = {}
    for k, v in req.subprocess_env.items():
        env[k] = v

    env['wsgi.input'] = req
    env['wsgi.errors'] = sys.stderr

    # setup url_scheme for static rendering
    if cfg.has_key('base_url'):
    	if cfg['base_url'].find('://') > 0:
            env['wsgi.url_scheme'] = cfg['base_url'][:cfg['base_url'].find("://")]
        else:
            env['wsgi.url_scheme'] = "http"
	    if cfg['base_url'].startswith('/'):
	    	cfg['base_url'] = "http://" + req.hostname + cfg['base_url']
    else:
        env['wsgi.url_scheme'] = "http"
        cfg['base_url'] = "/blog/blog.py"

    p = PyBlosxom(cfg, env)

    p.run()

    response = p.getResponse()

    # detect if we have to return "404 Not found" status
    # This case is handled as suggested in mod_python documenation
    if response.status.startswith('404'):
        req.status = apache.HTTP_NOT_FOUND

    # set headers
    for k, v in response.headers.items():
        if k.lower() == 'content-type':
            req.content_type = v
        req.headers_out[k] = v

    response.sendBody(req)

    return apache.OK

To make it work, you must register this script as handler for a specific directory:

DirectoryIndex blog.py
AddHandler mod_python .py
PythonHandler blog
#PythonDebug On

posted at: 14:58 | path: /python | permanent link to this entry