1 from __future__ import with_statement
3 from mako import template, lookup, filters
4 import util, form, session
6 # It seems Mako isn't thread-safe.
7 makolock = threading.Lock()
9 class liblookup(lookup.TemplateLookup):
10 def __init__(self, *args, **kwargs):
11 lookup.TemplateLookup.__init__(self, *args, **kwargs)
13 def adjust_uri(self, uri, relativeto):
17 homedir = os.getenv("HOME")
18 if homedir is not None:
19 usrdir = os.path.join(homedir, "wmako")
20 if os.path.exists(usrdir):
21 libdirs.append(usrdir)
22 libdirs.append(os.path.join(os.path.dirname(__file__), "makolib"))
23 cachedir = os.path.join("/tmp/", "mako-" + str(os.getuid()))
24 defargs = {"output_encoding": "utf-8",
25 "input_encoding": "utf-8",
26 "default_filters": ["decode.utf8"],
27 "module_directory": cachedir,
29 lib = liblookup(directories = libdirs, **defargs)
31 if not os.path.exists(cachedir):
33 def handle(req, filename, **kw):
35 tt = template.Template(filename = filename, lookup = lib, **defargs)
36 req.ohead["Content-Type"] = "text/html; charset=utf-8"
37 return [tt.render(request = req, **kw)]
41 return handle(req, req.filename,
42 form = form.formdata(req),
43 session = session.get(req))