[mod_python] Use of Vampire features outside of Vampire handlers.

Graham Dumpleton grahamd at dscpl.com.au
Wed Jun 1 21:35:45 EDT 2005

CC'd back to the list as will possibly be of interest to others.

Jamieson Becker wrote ..
> i've virtualized all my uris, so (for example) requests to /media
> directory ONLY get redirected (via mod_rewrite) to the actual media
> directory, whereas all other requests get handled by my handler. that's
> cool and works for me, but i noticed that you had a lot of other cool
> stuff in vampire -- like form handling stuff etc. is any of that usable
> outside of the normal vampire framework?

The intention is that as much as possible is usable outside of handlers
dispatched by Vampire. My history is that I have done a lot of work on
writing reusable C++ class libraries. My mindset therefore, even when
using Python, is to write as much as possible as being reusable within
its own right. If something doesn't and it appears there is no reason it
shouldn't, then I probably didn't test it outside of a handler dispatched
by Vampire and simply overlooked something obvious.

You mention forms, so will tackle that first. Note though that some of
the documentation on the web site is more up to date than latest
official release and describes features actually only in Vampire 1.7. This
release has been delayed for a little while I work out a better way of
doing something in the module loading mechanism. :-)

Obviously, if you use your own top level handler it will not be able to
accept form parameters direct as arguments in style of publisher as
done in Vampire. Your top level handler though, if dispatching to other
functions, can use vampire.executeHandler (1.7 feature), to execute
the function and thus get automatic form parameter unpacking and
interpretation of structured form values. Eg.

  from mod_python import apache

  import vampire

  def handler_args(req,arg1="",arg2=""):
    req.content_type = "text/plain"
    print >> req, arg1
    print >> req, arg2
    return apache.OK

  def handler(req):
    return vampire.executeHandler(req,handler_args)

A request of form:


Will thus yield:

  ['2', '3']

Note that you can do something similar to this with standard
mod_python by using util.apply_fs_data(). In Vampire though
you get the structured form data as well as lazy evaluation of
the form, ie., only actually process the form data when required,
which is important in some circumstances where third party
templating system expects to do its own form processing.

Functions like vampire.handlerArguments() also work fine outside
of handler dispatched by Vampire.

Other things that will also work are the configuration mechanism.
Is used exactly the same as if it was used in handler dispatched
by Vampire.

  def handler(req):
    config = vampire.loadConfig(req,"settings.cf")
    req.content_type = "text/plain"
    print >> req, config.get("Settings","key1")
    print >> req, config.get("Settings","key2")
    return apache.OK

You wouldn't be able to load config at global scope though because
of need to use "req" object. When Vampire dispatch mechanism is used
the "req" object is temporarily made available as "__req__" when loading
a module and that would be used if config lookup needed at global
scope. If your own top level handler made use of Vampire's module
importing mechanism however, you could do that also. Ie., should be
able to use vampire.importModule() as well with no problems outside
of handler dispatched by Vampire.

Some of the other things that should work are the vampire.Publisher(),
vampire.Handler() and vampire.Instance() (1.7 feature) wrapper objects.

  class _Object:

    def __init__(self):
      self.method2 = vampire.PathInfo(self._method2)

    def method1(self):
      return "method1"

    def _method2(self,path):
      return "method2",path
  handler = vampire.Publisher(_Object())

At least that it is supposed to work. Looks like I broke this at one point
as not working now when I know it used to. Will have to fix it for 1.7.

Anyway, you should get the idea. If there is a specific feature you were
looking at, let me know and can tell you if it will work and any possible
limitations if any.


More information about the Mod_python mailing list