Added a simplifying mako dispatcher.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 31 Oct 2011 08:56:26 +0000 (09:56 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 31 Oct 2011 08:56:26 +0000 (09:56 +0100)
wrw/makolib/xhtml11.mako [new file with mode: 0644]
wrw/wmako.py [new file with mode: 0644]

diff --git a/wrw/makolib/xhtml11.mako b/wrw/makolib/xhtml11.mako
new file mode 100644 (file)
index 0000000..da219c6
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
+<head>
+${self.htmlhead()}
+</head>
+<body>
+${next.body()}
+</body>
+</html>
+
+<%def name="htmlhead()">
+    <title>${self.title()}</title>
+</%def>
+
+<%def name="title()">
+    Untitled Page
+</%def>
diff --git a/wrw/wmako.py b/wrw/wmako.py
new file mode 100644 (file)
index 0000000..2fe7801
--- /dev/null
@@ -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))