From 3d866605f1fc40fb8b9d8470a02bcba7adb890ad Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 31 Oct 2011 09:56:26 +0100 Subject: [PATCH] Added a simplifying mako dispatcher. --- wrw/makolib/xhtml11.mako | 18 ++++++++++++++++++ wrw/wmako.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 wrw/makolib/xhtml11.mako create mode 100644 wrw/wmako.py diff --git a/wrw/makolib/xhtml11.mako b/wrw/makolib/xhtml11.mako new file mode 100644 index 0000000..da219c6 --- /dev/null +++ b/wrw/makolib/xhtml11.mako @@ -0,0 +1,18 @@ + + + + +${self.htmlhead()} + + +${next.body()} + + + +<%def name="htmlhead()"> + ${self.title()} + + +<%def name="title()"> + Untitled Page + diff --git a/wrw/wmako.py b/wrw/wmako.py new file mode 100644 index 0000000..2fe7801 --- /dev/null +++ b/wrw/wmako.py @@ -0,0 +1,43 @@ +from __future__ import with_statement +import os, threading +from mako import template, lookup, filters +import util, form, session + +# It seems Mako isn't thread-safe. +makolock = threading.Lock() + +class liblookup(lookup.TemplateLookup): + def __init__(self, *args, **kwargs): + lookup.TemplateLookup.__init__(self, *args, **kwargs) + + def adjust_uri(self, uri, relativeto): + return uri + +libdirs = [] +homedir = os.getenv("HOME") +if homedir is not None: + usrdir = os.path.join(homedir, "wmako") + if os.path.exists(usrdir): + libdirs.append(usrdir) +libdirs.append(os.path.join(os.path.dirname(__file__), "makolib")) +cachedir = os.path.join("/tmp/", "mako-" + str(os.getuid())) +defargs = {"output_encoding": "utf-8", + "input_encoding": "utf-8", + "default_filters": ["decode.utf8"], + "module_directory": cachedir, + } +lib = liblookup(directories = libdirs, **defargs) + +if not os.path.exists(cachedir): + os.mkdir(cachedir) +def handle(req, filename, **kw): + with makolock: + tt = template.Template(filename = filename, lookup = lib, **defargs) + req.ohead["Content-Type"] = "text/html; charset=utf-8" + return [tt.render(request = req, **kw)] + +@util.wsgiwrap +def application(req): + return handle(req, req.filename, + form = form.formdata(req), + session = session.get(req)) -- 2.11.0