Permalink
Browse files

Template caching.

  • Loading branch information...
ralphbean committed Apr 28, 2012
1 parent 283367b commit 4d16358e0a58b9d83e8e0abd8a4f364fda8ca2fe
Showing with 39 additions and 4 deletions.
  1. +4 −2 tests/test_template.py
  2. +8 −2 tw2/core/templating.py
  3. +27 −0 tw2/core/util.py
@@ -1,4 +1,5 @@
import tw2.core as twc, testapi
import tw2.core as twc
import testapi
import itertools
import os
import webob as wo
@@ -20,10 +21,11 @@ def _check_render(self, template, data, expected, engine=None):
if engine:
mw = twc.make_middleware(None, preferred_rendering_engines=[engine])
testapi.request(1, mw)
twc.util.flush_memoization()
out = twc.templating.render(template, 'string', data)
assert(isinstance(out, unicode))
assert out == expected, out
eq_(out, expected)
def test_get_source_inline(self):
template_in = "awesome"
@@ -1,6 +1,8 @@
import os
import core
from util import memoize
from webhelpers.html import literal
# Just shorthand
@@ -11,10 +13,11 @@
rendering_extension_lookup = {
'mako': ['mak', 'mako'],
'genshi': ['html'],
'genshi_abs': ['html'], #just for backwards compatibility with tw2 2.0.0
'genshi_abs': ['html'], # just for backwards compatibility with tw2 2.0.0
}
@memoize
def get_engine_name(template_name, mw=None):
global engine_name_cache
@@ -60,6 +63,7 @@ def get_engine_name(template_name, mw=None):
raise ValueError("Could not find engine name for %s" % template_name)
@memoize
def _get_dotted_filename(engine_name, template):
from_loc, location, filename = template.rsplit('.', 2)
module = __import__(location, globals(), locals(), [from_loc])
@@ -73,6 +77,7 @@ def _get_dotted_filename(engine_name, template):
raise IOError("Couldn't find source for %r" % template)
@memoize
def get_source(engine_name, template, inline=False):
if inline:
return template
@@ -90,6 +95,7 @@ def get_source(engine_name, template, inline=False):
return f.read()
@memoize
def get_render_callable(engine_name, displays_on, src):
""" Returns a function that takes a template source and kwargs. """
@@ -106,6 +112,7 @@ def get_render_callable(engine_name, displays_on, src):
raise NotImplementedError("Unhandled engine")
def render(template_name, displays_on, kwargs, inline=False, mw=None):
""" Highest level function, here for convenience.
@@ -116,4 +123,3 @@ def render(template_name, displays_on, kwargs, inline=False, mw=None):
source = get_source(engine_name, template_name, inline)
callback = get_render_callable(engine_name, displays_on, source)
return callback(kwargs)
@@ -93,3 +93,30 @@ def __call__(self, string, *args, **kw):
def abort(req, status):
return webob.Response(request=req, status=status, content_type="text/html")
_memoization_flush_callbacks = []
class memoize(object):
def __init__(self, f):
global _memoization_flush_callbacks
self.f = f
self.mem = {}
_memoization_flush_callbacks.append(self._flush)
def _flush(self):
self.mem = {}
def __call__(self, *args, **kwargs):
if (args, str(kwargs)) in self.mem:
return self.mem[args, str(kwargs)]
else:
tmp = self.f(*args, **kwargs)
self.mem[args, str(kwargs)] = tmp
return tmp
def flush_memoization():
for cb in _memoization_flush_callbacks:
cb()

0 comments on commit 4d16358

Please sign in to comment.