43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from mod_python import apache, Session
|
|
from genshi.template import TemplateLoader, TemplateNotFound
|
|
from genshi import ParseError
|
|
|
|
def findtemplate(uri):
|
|
templates = {"/" : "index.xml"}
|
|
if uri in templates:
|
|
return templates[uri]
|
|
return None
|
|
|
|
def handler(req):
|
|
session = Session.Session(req)
|
|
try:
|
|
session['hits'] += 1
|
|
except:
|
|
session['hits'] = 1
|
|
|
|
session.save()
|
|
|
|
template = findtemplate(req.uri)
|
|
if template:
|
|
|
|
loader = TemplateLoader([req.document_root()])
|
|
try:
|
|
req.content_type = "text/html; charset=UTF-8"
|
|
tmpl = loader.load(template)
|
|
stream = tmpl.generate(title='Hello World: Reloaded',
|
|
hits=session['hits'])
|
|
pagebuffer = stream.render('xhtml')
|
|
except TemplateNotFound, tnf:
|
|
req.content_type = "text/plain; charset=UTF-8"
|
|
pagebuffer = str(tnf)
|
|
except ParseError, pe:
|
|
req.content_type = "text/plain; charset=UTF-8"
|
|
pagebuffer = str(pe)
|
|
|
|
#pagebuffer = "Hits: %d\n" % session['hits']
|
|
#pagebuffer += "Yippieh: I found %s -> %s!" % (req.uri, template)
|
|
|
|
req.write(pagebuffer)
|
|
return (apache.OK)
|
|
return (apache.HTTP_NOT_FOUND)
|