[mod_python] mod_python, best practices

Graham Dumpleton grahamd at dscpl.com.au
Wed Nov 24 07:04:11 EST 2004


On 24/11/2004, at 9:36 PM, Graham Dumpleton wrote:

>
> On Wednesday, November 24, 2004, at 08:35  PM, Rune Hansen wrote:
>
>> Hi,
>> I've written a rather long and winding document which I've called  
>> "mod_python, best practices".
>>
>> A better name would be "mod_python, assumptions made on shaky ground  
>> - kept in a light tone to mask my inherent fear of making a complete  
>> fool of my self." Obviously, that title is to long.
>>
>> Anyway, the document can be found here http://www.scanmine.com/mp/
>>
>> I'd really like thoughts, comments and corrections... keep your  
>> smirks and laughs to your self :)
>>
>> As you read the document it will become apparent that english is not  
>> my native tongue, feel free to comment on that too.
>
> ...

> On the issue of configuration files, again, look at Vampire. It
> provides a mechanism for configuration which also uses ConfigParser.
> It has though a smart search ability to find the config file. Ie.,
> rather than having to specify the exact location of the config file,
> you simply ask for config file of certain name, and using the req
> object will search back up the directory hierarchy until found,
> or will stop searching if go up as far as where the PythonHandler
> definition was found. Thus instead of writing:
>
> def handler(req):
>     global CONFIG,cfgfile
>     if not CONFIG:
>        CONFIG =  
> Cfgparser("/".join([os.path.dirname(__file__),cfgfile]),req.server)
>        CONFIG.getConfig()
>     req.CONFIG = CONFIG
>
> one can write something like:
>
> def handler_html(req):
>     config = vampire.loadConfig(req,"Globals.cfg")
>
> This means that you don't have to specify fixed relative pathnames in
> subdirectory content handlers to refer to a config somewhere above
> that directory, instead it will be automatically found. This allows
> one to freely move stuff around and not have to fix up any fixed
> location paths.
>
> The config object you get back also has some default values set which
> allow one to easily create URLs which reference back to the root of
> your application, again avoiding issues of having hardcoded relative
> paths. Unfortunately, haven't yet released a newer version of Vampire I
> have which takes this a bit further and makes it even more useful.
>
> Configs are cached automatically, but the cache will also reload them
> automatically when it detects that the actual config file has changed.

In a rush of madness, I have released Vampire 1.3 which has the changes
to the config support I alluded to above.

As an quick illustration of how the config mechanism supplied with
Vampire can work, the Vampire web site tree has a ".htaccess" and
".vampire" file in its root directory. You can see the content of
the .vampire file as:

   http://www.dscpl.com.au/projects/vampire/source/.vampire

The .htaccess file has various PythonOption settings:

   PythonOption VampireHandlersSection Handlers
   PythonOption VampireHandlersConfig .vampire
   PythonOption VisibleSourceExtensions .html .psp .py .rml .vampire
   PythonOption VampireDirectoryIndex index.html

In a subdirectory I have a content handler example which will display  
the
config as will be available when loaded. This example, will look for the
.vampire file starting in its directory and moving upwards. The source
code for this content handler is:

    
http://www.dscpl.com.au/projects/vampire/source/examples-01/vampire- 
config.py
    
http://www.dscpl.com.au/projects/vampire/source/examples-01/vampire- 
config.html

You can see what it produces by accessing:

    
http://www.dscpl.com.au/projects/vampire/examples-01/vampire- 
config.html
    
http://www.dscpl.com.au/projects/vampire/examples-01/vampire- 
config.html?mode=raw

Note this is actually an example designed to show what config is  
available,
you don't need to browse other like this example does, just get the  
variable
you need and your done. I just happen to use the HTMLTemplate package to
render pages, but you could be using mpservlets, psp or anything else  
for
that matter.

As you have used ConfigParser already you will understand the difference
between interpolated values and raw mode which shows values before being
interpolated.

As is standard with ConfigParser, interpolation will take values from  
the
same section in the config file. Vampire also sets things up though so  
that
values used for interpolation can be sourced from PythonOption settings  
in
the .htaccess file, although the example does use these as such in any  
of
the config file value strings.

Also, Vampire sets up some magic variables which can be used in string
interpolation. These are:

       __config_root__ --> Absolute path to directory config file is in.
       __config_file__ --> Absolute path to the actual config file.
       __config_mtime__ --> Modification time of the config file.
       __baseurl_abs__ --> Base URL of the directory where config file  
is in.
       __handler_root__ --> Where PythonHandler directive was defined.
       __baseurl_rel__  --> Base URL relative to the current request.

The base URL variables are useful as they relate to the URL used in a  
request.
One can thus put a config file at the top of your web application.  
Under that
you would have an "images" and "styles" directory. Your config can then  
have:

   images = %(__baseurl_rel__)s/images
   styles = %(__baseurl_rel__)s/styles

In content handlers anywhere in nested subdiretories, they can get the  
config
and grab those values and relative path will always be setup correctly  
to
reference the images and styles directory. This can be used to then  
fill out
content in HTML as appropriate. One could have used __baseurl_abs__ as  
well,
but then someone who scrapes your site into static files would have  
links
which reference a location which wouldn't exist if they hosted it in a  
different
location, thus relative paths preferable.

Hopefully you can see where I am going with this.

Bit more information in CHANGES file for Vampire 1.3.

   http://www.dscpl.com.au/projects/vampire/CHANGES

I really got to log off now. Not sure when I'll be able to next read  
email. :-(

--
Graham Dumpleton (grahamd at dscpl.com.au)



More information about the Mod_python mailing list