diff options
-rw-r--r-- | CHANGES | 6 | ||||
-rw-r--r-- | mako/cache.py | 28 | ||||
-rw-r--r-- | mako/codegen.py | 4 | ||||
-rw-r--r-- | test/test_cache.py | 16 |
4 files changed, 43 insertions, 11 deletions
@@ -9,6 +9,12 @@ i.e. "% with x as e:/ % endwith" support. Courtesy Ben Trofatter [ticket:147] +- [feature] Added class-level flag to CacheImpl + "pass_context"; when True, the keyword argument + 'context' will be passed to get_or_create() + containing the Mako Context object. + [ticket:185] + - [bug] Fixed some Py3K resource warnings due to filehandles being implicitly closed. [ticket:182] diff --git a/mako/cache.py b/mako/cache.py index 959640e..34736ce 100644 --- a/mako/cache.py +++ b/mako/cache.py @@ -78,12 +78,18 @@ class Cache(object): """Retrieve a value from the cache, using the given creation function to generate a new value.""" + return self._ctx_get_or_create(key, creation_function, None, **kw) + + def _ctx_get_or_create(self, key, creation_function, context, **kw): + """Retrieve a value from the cache, using the given creation function + to generate a new value.""" + if not self.template.cache_enabled: return creation_function() return self.impl.get_or_create(key, creation_function, - **self._get_cache_kw(kw)) + **self._get_cache_kw(kw, context)) def set(self, key, value, **kw): """Place a value in the cache. @@ -94,7 +100,7 @@ class Cache(object): """ - self.impl.set(key, value, **self._get_cache_kw(kw)) + self.impl.set(key, value, **self._get_cache_kw(kw, None)) put = set """A synonym for :meth:`.Cache.set`. @@ -113,7 +119,7 @@ class Cache(object): values will use that same backend. """ - return self.impl.get(key, **self._get_cache_kw(kw)) + return self.impl.get(key, **self._get_cache_kw(kw, None)) def invalidate(self, key, **kw): """Invalidate a value in the cache. @@ -125,7 +131,7 @@ class Cache(object): values will use that same backend. """ - self.impl.invalidate(key, **self._get_cache_kw(kw)) + self.impl.invalidate(key, **self._get_cache_kw(kw, None)) def invalidate_body(self): """Invalidate the cached content of the "body" method for this template. @@ -151,19 +157,21 @@ class Cache(object): self.invalidate(name, __M_defname=name) - def _get_cache_kw(self, kw): + def _get_cache_kw(self, kw, context): defname = kw.pop('__M_defname', None) if not defname: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) - return tmpl_kw elif defname in self._def_regions: - return self._def_regions[defname] + tmpl_kw = self._def_regions[defname] else: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) self._def_regions[defname] = tmpl_kw - return tmpl_kw + if context and self.impl.pass_context: + tmpl_kw = tmpl_kw.copy() + tmpl_kw.setdefault('context', context) + return tmpl_kw class CacheImpl(object): """Provide a cache implementation for use by :class:`.Cache`.""" @@ -171,6 +179,10 @@ class CacheImpl(object): def __init__(self, cache): self.cache = cache + pass_context = False + """If True, the Context will be passed to get_or_create + as the name 'context'.""" + def get_or_create(self, key, creation_function, **kw): """Retrieve a value from the cache, using the given creation function to generate a new value. diff --git a/mako/codegen.py b/mako/codegen.py index 704330c..51eb01e 100644 --- a/mako/codegen.py +++ b/mako/codegen.py @@ -635,7 +635,7 @@ class _GenerateRenderMethod(object): ) if buffered: s = "context.get('local')."\ - "cache.get_or_create(%s, lambda:__M_%s(%s), %s__M_defname=%r)" % \ + "cache._ctx_get_or_create(%s, lambda:__M_%s(%s), context, %s__M_defname=%r)" % \ (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name @@ -646,7 +646,7 @@ class _GenerateRenderMethod(object): else: self.printer.writelines( "__M_writer(context.get('local')." - "cache.get_or_create(%s, lambda:__M_%s(%s), %s__M_defname=%r))" % + "cache._ctx_get_or_create(%s, lambda:__M_%s(%s), context, %s__M_defname=%r))" % (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name, diff --git a/test/test_cache.py b/test/test_cache.py index 4ba5d1f..dc20b8f 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -536,4 +536,18 @@ class CacheTest(TemplateTest): """, cache_args={'use_beaker':False}) m = self._install_mock_cache(t) t.render() - eq_(m.kwargs, {'use_beaker':False, 'region':'myregion', 'timeout':50, 'foo':'foob'})
\ No newline at end of file + eq_(m.kwargs, {'use_beaker':False, 'region':'myregion', 'timeout':50, 'foo':'foob'}) + + def test_pass_context(self): + t = Template(""" + <%page cached="True"/> + """) + m = self._install_mock_cache(t) + t.render() + assert 'context' not in m.kwargs + + m.pass_context = True + t.render(x="bar") + assert 'context' in m.kwargs + assert m.kwargs['context'].get('x') == 'bar' + |