[mod_python] Flexible imports

Colin Fox cfox at cfconsulting.ca
Sat Mar 6 17:01:04 EST 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hi, all. I'm trying to do something with mod_python that is easy with
CGIs, but is turning out to be somewhat challenging.

I'd like to support python modules in a variety of subdirectories. So a
url like

http://localhost/top/something.py

and

http://localhost/top/next/somethingelse.py

would both work. However, the way it seems to be set up, if you install
a python handler (say in /top), then it gets executed for all .py files
regardless of where they are (so in this case, somethingelse.py would be
expected to be in /top, not /top/next). That can be useful at times, but
at other times it can be a stumbling block.

Here's a snippet of my handler:

# --------------------

def process_pyx(req):
~    path, filename = os.path.split(req.filename)
~    name, ext = string.split(filename, ".")

~    #pyx_mod = apache.import_module("%s/%s" % (path,name))
~    #pyx_mod = apache.import_module(name, path=path)
~    pyx_mod = apache.import_module("%s" % name)

~    pyx_mod.main(req)
~    return apache.OK

def handler(req):
~    if args['requestedfile'].endswith('.py'):
	return process_pyx(req)
~    elif args['requestedfile'].endswith('.xml'):
	return process_xml(req)
~    else:
	return apache.ERROR

# --------------------

This works fine for any .py file that is in the same directory as this
handler. However, it doesn't work for subdirectories. You can see some
attempts for adding a path for the import, but they don't work.

So - is there an elegant way to allow a specifically named .py file to
be executed from any directory under this handler's directory?

Thanks,
~   cf

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFASnRQoaQ1/feGlJoRAoqnAJ9RvjGNKHTFTAGJ0rqwreIj244ckgCfbPU6
X+g/D+w9b/97UwCY2BLd7Qs=
=OPj0
-----END PGP SIGNATURE-----


More information about the Mod_python mailing list