[mod_python] Question about vampire module loading

Graham Dumpleton grahamd at dscpl.com.au
Mon Mar 28 16:38:03 EST 2005


First off, need to pass request object as third argument to 
importModule()
if in that sub module you are intending to do global level config search
and additional imports. If this isn't done and you use __req__ from the
sub module it will be None. See changes below.

Also suggest better way of using HTMLTemplate below.

On 28/03/2005, at 10:37 PM, Keerati Inochanon wrote:

> Hi,
>
> I have been experimenting with vampire a little, and having problems
> with module loading. I store handlers in $documentroot and some other
> handlers in $documentroot/modules.
>
> Here is my rough directory structure:
> $documentroot:
> index.html index.py login.html login.py modules register.html 
> register.py
>
> $documentroot/modules:
> SessionManagement.py Settings.py UserManagement.py
>
> In my login.py file, I import the module using vampire importModule 
> mechanism:
>
> config = vampire.loadConfig(__req__, ".vampire")
> directory = config.get("Modules", "common")
> UserManagement = vampire.importModule("UserManagement", directory)

Change this to:

   UserManagement = vampire.importModule("UserManagement", directory, 
__req__)

> SessionManagement = vampire.importModule("SessionManagement", 
> directory)
> --
>
> In my UserManagement.py, I also need some stuff from
> SessionManagement, so I also do:
>
> config = vampire.loadConfig(__req__, ".vampire")
> directory = config.get("Modules", "common")
> SessionManagement = vampire.importModule("SessionManagement", 
> directory)

Change this to:

   SessionManagement = vampire.importModule("SessionManagement", 
directory, __req__)

> Settings = vampire.importModule("Settings", directory)
> --
>
> My handler_html in login.py:
>
> def handler_html(req, error=''):
>   if not os.path.exists(req.filename):
>     return apache.DECLINED
>
>   # Immediately redirect to the main page if session exists
>   session = SessionManagement.checkSession(req)
>   if session['isAuthorized'] and session['username']:
>     util.redirect(req, 'main.html')
>
>   message = ''
>   if error == 'invalid':
>     message = 'invalid username/password'
>
>   def renderTemplate(template, error):
>     template.result.content = error
>
>   html = open(req.filename, 'r')
>   template = HTMLTemplate.Template(renderTemplate, html.read())
>   html.close()

You can replace these five lines with:

   template = vampire.loadTemplate(req.filename)
   template.result.content = error

Ie., use Vampire's internal loading and caching system for HTMLTemplate.
It will do automatic reloading of source HTML when changes are made to 
it.

BTW, where do you render the page and return it. Ie.,

   req.content_type = "text/html"
   req.send_http_header()
   req.write(template.render())

>
> def handler_html(req, error=''):
>   if not os.path.exists(req.filename):
>     return apache.DECLINED
>
>   # Immediately redirect to the main page if session exists
>   session = SessionManagement.checkSession(req)
>   if session['isAuthorized'] and session['username']:
>     util.redirect(req, 'main.html')
>
>   message = '';
>   if error == 'invalid':
>     message = 'invalid username/password'
>
>   def renderTemplate(template, error):
>     template.result.content = error
>
>   html = open(req.filename, 'r')
>   template = HTMLTemplate.Template(renderTemplate, html.read())
>   html.close()

Similarly:


   template = vampire.loadTemplate(req.filename)
   template.result.content = error

   req.content_type = "text/html"
   req.send_http_header()
   req.write(template.render())


> --
>
> My httpd.conf:
>
> <Directory "/home/www/testing">
>         PythonOption VampireDirectoryIndex index.html
>         SetHandler python-program
>         PythonHandler vampire
>         PythonPath 'sys.path'
> #       PythonOption VampireDefaultHandlers On
>         PythonDebug On
>         <Files ~ "^\.vampire">
>           Order allow,deny
>           deny from all
>         </Files>
> </Directory>
> --
>
> When accessing login.html, I get this error:
> Mod_python error: "PythonHandler vampire"
>
> Traceback (most recent call last):
>
>   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line
> 299, in HandlerDispatch
>     result = object(req)
>
>   File "/usr/lib/python2.3/site-packages/vampire/lookup.py", line 585,
> in _handler
>     module = _import(req,file)
>
>   File "/usr/lib/python2.3/site-packages/vampire/lookup.py", line 53, 
> in _import
>     module = _moduleCache.importModule(name,directory,req)
>
>   File "/usr/lib/python2.3/site-packages/vampire/cache.py", line 181,
> in importModule
>     execfile(file,module.__dict__)
>
>   File "/home/www/testing/login.py", line 9, in ?
>     UserManagement = vampire.importModule("UserManagement", directory)
>
>   File "/usr/lib/python2.3/site-packages/vampire/cache.py", line 306,
> in importModule
>     return _moduleCache.importModule(name,path,req)
>
>   File "/usr/lib/python2.3/site-packages/vampire/cache.py", line 181,
> in importModule
>     execfile(file,module.__dict__)
>
>   File "/home/www/testing/modules/UserManagement.py", line 8, in ?
>     config = vampire.loadConfig(__req__, ".vampire")
>
>   File "/usr/lib/python2.3/site-packages/vampire/config.py", line 263,
> in loadConfig
>     return _configCache.loadConfig(req,name)
>
>   File "/usr/lib/python2.3/site-packages/vampire/config.py", line 198,
> in loadConfig
>     req.vampire = { "config": {} }
>
> AttributeError: 'NoneType' object has no attribute 'vampire'
> --
>
> I was wondering whether what I am doing is allowed or not. What should
> I do to get around it? I am using vampire-1.5. Python, mod_python, and
> vampire are still pretty new to me. Please let me know if you need any
> further information. Thank you very much in advance.
>
> Best regards,
> Keerati
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list