[mod_python] import_module

Kamil Niechajewicz kamil at nvstudio.pl
Wed Apr 14 16:22:33 EST 2004


On Wed, 14 Apr 2004 09:00:50 -0400
Ron Alford <ronwalf at umd.edu> wrote:

> An alternative implementation would be to have a global in apache that 
> matched filenames to  a randomly generated module name, thus avoiding a 
> bunch of the code that I needed.  I think this method could be easily 
> integrated into the mod_python code-base.  Anyone see a problem with this?

Thanks, I will look into your code :)
Actually I did a fast solution not involving enabling reimport at every 
request.
It needed a change in Publisher. First I made a "path" variable that is
passed to import_module() a constant path leading to root directory of
my application (actually it was changing and depend on this:
   path, module_name = os.path.split(req.filename)

this returned module_name and a physical path to directory with this
module. That part is quite correct, but its correctness is overriden
soon by import_module, because we give it a single module_name, for
example "index" even if the path was "/", or "/admin", or "/whatsoever".

So my path now is equal to "/my_app_root" which contains the same 
structure as mentioned in previous mail - "index.py" that is default,
"admin/" dir which contains also "index.py".

Now I edited publisher.py in the place where it determines module_name.
I just get full path from url ("/", "/admin" etc.), convert it to 'dotted'
form. So for http://localhost/ I get standard module name "index", but
for http://localhost/admin I get "admin.index" instead of just "index".
Then it's passed to import_module and everything is fine, because it 
has path set to main directory from which it imports "index" or "admin.
index". Those are considered as different modules so noone is mistaken.
This works with every number of 'dotted' modules I guess, so I can have
directory "/admin/tree" and the module admin.tree.index will be imported.

Hope I explained it correctly :)

/Kamil


More information about the Mod_python mailing list