2 from mako import template, lookup, filters
3 import util, form, session, env
5 # It seems Mako isn't thread-safe.
6 makolock = threading.Lock()
8 class liblookup(lookup.TemplateLookup):
9 def __init__(self, *args, **kwargs):
10 lookup.TemplateLookup.__init__(self, *args, **kwargs)
12 def adjust_uri(self, uri, relativeto):
16 homedir = os.getenv("HOME")
17 if homedir is not None:
18 usrdir = os.path.join(homedir, "wmako")
19 if os.path.exists(usrdir):
20 libdirs.append(usrdir)
21 libdirs.append(os.path.join(os.path.dirname(__file__), "makolib"))
22 cachedir = os.path.join("/tmp/", "mako-" + str(os.getuid()))
23 defargs = {"output_encoding": "utf-8",
24 "input_encoding": "utf-8",
25 "default_filters": ["decode.utf8"],
26 "module_directory": cachedir,
29 def makelib(init=liblookup, directories=[], **kwargs):
32 return init(directories = libdirs + directories, **ad)
34 lib = env.var(makelib())
36 if not os.path.exists(cachedir):
38 def handle(req, filename, **kw):
40 tt = template.Template(filename = filename, lookup = lib.val, **defargs)
41 req.ohead["Content-Type"] = "text/html; charset=utf-8"
42 return [tt.render(request = req, **kw)]
46 return handle(req, req.filename,
47 form = form.formdata(req),
48 session = session.get(req))