[mod_python] mod_python.publisher and Cheetah

Graham Dumpleton grahamd at dscpl.com.au
Mon Mar 14 18:45:05 EST 2005


My obvious error should also be fixed. :-)

  # Assume that HTML is being generated.

  req.content_type = "text/html"
  req.send_http_header()

Browser was smart enough to realise it was HTML anyway when
I tested it.

Graham

Graham Dumpleton wrote ..
> Here is a better version of a handler which allows use of Cheetah
> templates with mod_python.
> 
> 
> 
> from mod_python import apache
> 
> import os
> 
> # It is assumed that Apache config or .htaccess file
> # blocks access to ".tmpl", ".py" and ".pyc" files.
> 
> def handler(req):
> 
>   # Assume REST style URLs. Ie., no extension is used
>   # for accessing Cheetah pages. On this basis, first
>   # perform a check that there is a ".py" file in
>   # existance. This is done because can't distinguish
>   # between a non existant module and a module which has
>   # a coding error in it when using the function
>   # "apache.import_module()". By returning DECLINED,
>   # Apache will then serve up any static files in the
>   # directory which may otherwise be matched.
> 
>   target = req.filename + ".py"
> 
>   if not os.path.exists(target):
>     return apache.DECLINED
> 
>   # Grab the module name to look for from the last part
>   # of the path. This means that pages can be spread
>   # across subdirectories as well.
> 
>   directory,module_name = os.path.split(req.filename)
> 
>   # Import the module. Any coding error in the module
>   # being imported is thrown back to the user. Error
>   # also results if by chance the target just vanished.
> 
>   module = apache.import_module(module_name,[directory])
> 
>   # Ensure that there is a class defined in the module
>   # of the appropriate name.
> 
>   if not hasattr(module,module_name):
>     return apache.DECLINED
> 
>   # Create instance of the class and setup request object.
> 
>   tmpl = getattr(module,module_name)()
>   tmpl.req = req
> 
>   # Assume that HTML is being generated.
> 
>   req.content_type = "text/plain"
>   req.send_http_header()
> 
>   # Now generate the actual content and return it.
> 
>   req.write(tmpl.respond())
> 
>   return apache.OK
> 
> 
> 
> Graham Dumpleton wrote ..
> > I somewhat suspect that you aren't going to get the code as by
> > default generated by Cheetah to play well with publisher. As to
> > your original custom handler, you could clean it up somewhat
> > by using something like:
> > 
> > start = "/python/"
> > 
> > from mod_python import apache
> > import string
> > 
> > def handler(req):
> > 
> >       req.content_type = "text/html"
> >       module_name = string.replace (req.uri, start, "")
> > 
> >       #     try:
> >       #              exec ("""import %s
> >       #tmpl = %s.%s()
> >       #tmpl.req = req""" % (module_name, module_name, module_name))
> >       #     except ImportError:
> >       #              return apache.HTTP_NOT_FOUND
> > 
> >       module = apache.import_module(module_name)
> >       if not hasattr(module,module_name):
> >             return apache.HTTP_NOT_FOUND
> > 
> >       tmpl = getattr(module,module_name)()
> >       tmpl.req = req
> > 
> >       req.send_http_header()
> >       req.write(tmpl.respond())
> > 
> >       return apache.OK
> > 
> > I still see some issues with this code in as much as I think it
> > requires everything to be in the one directory. Ie., can't have
> > subdirectories. Using "start" as a fixed value is not good, but
> > that can also be fixed. Also, you can't mix different file types
> > in the same directory.
> > 
> > When I get a chance, I'll write you a better version of this
> > which would fix these problems and provide new abilities as well.
> > I am surprised though that someone hasn't already written a
> > better handler for integrating Cheetah with mod_python.
> > 
> > BTW, I am not on the Cheetah mailing list, so ensure any responses
> > also go back to the mod_python list as well.
> > 
> > Graham
> > 
> > On Tuesday, March 15, 2005, at 02:17  AM, Stephane Bortzmeyer wrote:
> > 
> > > I always used Cheetah on Apache with a custom mod_python handler (see
> > > http://wiki.cheetahtemplate.org/cheetahrecipes.html). I would like
> to
> > > use mod_python's publisher
> > > http://www.modpython.org/live/current/doc-html/hand-pub.html, in order
> > > to limit my liability :-) specially for security reasons.
> > >
> > > It works fine but not if I use Cheetah's inheritance, as described
> in
> > > http://www.cheetahtemplate.org/docs/users_guide_html_multipage/ 
> > > inheritanceEtc.extends.html.
> > > My foobar.tmpl Cheetah file is compiled as foobar.py, which includes
> > a
> > > foobar class. I added a index() routine but Cheetah puts in in the
> > > foobar class, not directly in the foobar module. When
> > > mod_python.publisher calls index(), it finds nothing and returns
> > > NOT_FOUND.
> > >
> > >
> > >
> > > My SiteLogic.py:
> > >
> > >
> > > from Cheetah.Template import Template
> > >
> > > class SiteLogic(Template):
> > >         """ Pure Python class containing methods for the site """
> > >
> > >         # Never found because Cheetah puts it in a class
> > >         def index(req):
> > >             return "Dummy"
> > >
> > >
> > >
> > > My Site.tmpl:
> > >
> > >
> > >
> > > #from SiteLogic import SiteLogic
> > > #extends SiteLogic
> > > #implements respond
> > >
> > > #def title
> > > No title #slurp
> > > #end def
> > >
> > > #def body
> > > <P>Default body.
> > > #end def
> > >
> > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> > > <HTML>
> > > <HEAD>
> > > <TITLE>
> > > $title
> > > </TITLE>
> > > </HEAD>
> > > <BODY>
> > > <H1>$title</H1>
> > > $body
> > > <HR>
> > >
> > >
> > >
> > > A typical page:
> > >
> > >
> > > #from Site import Site
> > > #extends Site
> > > #implements body
> > > #def title
> > > Welcome
> > > #end def
> > >
> > > <P>Some content.
> > > _______________________________________________
> > > Mod_python mailing list
> > > Mod_python at modpython.org
> > > http://mailman.modpython.org/mailman/listinfo/mod_python
> > 
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> _______________________________________________
> 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