partying with pyblosxom part 3

I would like to have gone over installation of PyBlosxom in a mod_python environment, but I couldn't get it to work for the life of me. That and the lack of documentation forced me to abandon that idea. So with that, we'll move on to dissecting the pyblosxom.cgi file.

This file is essentially just a wrapper for the PyBlosxom modules. We'll take a look at what each part does and then create our own stripped down version. You can follow along by looking at the numbered code here.

Stepping Through pyblosxom.cgi
Let's start with Line 3. Here we're importing our config file, config.py, that's found in the same directory as pyblosxom.cgi. Though we treat config.py as a config file, it's just a python script that sets variables.

Lines 4 through 6 account for a PyBlosxom installation in a non-formal directory.

Line 7 starts the main action. First we import the standard os and sys modules. Next we create a dictionary called env and set some wsgi.* variables. The wsgi variables are for WSGI support which was added in the latest release of PyBlosxom.. Lines 15 through 21 look complicated, but they're really not. What PyBlosxom is doing is figuring out if your website is either http or https and if you set a base URL in the config file.

Next, the code block on lines 22 through 26 take a bunch of standard HTTP environment variables and toss them in our env dictionary. The env dictionary is reference many times throughout the life-cycle of PyBlosxom as well see later.

On line 27, we create our PyBlosxom instance passing it the config and env variables.

Again, lines 28 through 36 look jumbled and complicated, but they're not. What this block is doing is checking to see if this script was run from the command line, and if so, whether the user wants to just verify the installation or render some static files.

Finally, on line 38, PyBlosxom is run. The script gets the response and sends it to the client. What shows up on the page is your rendered blog.

Creating Our Own pyblosxom.cgi
Now that we understand what pyblosxom.cgi is doing, we can make our own. This is just for demonstration purposes and really doesn't have a real purpose since the original pyblosxom.cgi works fine.

Most of the work pyblosxom.cgi is doing is optional and I'm going to cut those out. I just want a script that will assume all the defaults are used and we just want a simple webpage printed to the screen. It looks something like this:

#!/usr/bin/env python from config import py as cfg import os,sys from Pyblosxom.pyblosxom import PyBlosxom env = {} env['wsgi.input'] = sys.stdin env['wsgi.errors'] = sys.stderr env['wsgi.url_scheme'] = 'http' for mem in [“HTTP_HOST”, “HTTP_USER_AGENT”, “HTTP_REFERER”, “PATH_INFO”, “QUERY_STRING”, “REMOTE_ADDR”, “REQUEST_METHOD”, “REQUEST_URI”, “SCRIPT_NAME”, “HTTP_IF_NONE_MATCH”, “HTTP_IF_MODIFIED_SINCE”, “HTTP_COOKIE”, “CONTENT_LENGTH”, “HTTP_ACCEPT”, “HTTP_ACCEPT_ENCODING”]: env[mem] = os.environ.get(mem, “”) p = PyBlosxom(cfg,env) p.run() response = p.getResponse() response.sendHeaders(sys.stdout) response.sendBody(sys.stdout)

And that's all there is to it. The bulk of the script was taken up by the HTTP environment variables, but there's really no way around that. This script will not do any static rendering, won't account for different installation directories, and will just hang if you invoke it via the command line. However, it will print out the rendered webpage to your web browser. Not so complicated now, is it?