Issues

Issue #185 resolved

support context passed to cacheimpl

Mike Bayer
repo owner created an issue

possible patch:

  1. !diff diff -r 2f022a271212b220519305649e6a8490cf4399ff mako/cache.py --- a/mako/cache.py Tue Feb 21 09:49:40 2012 -0500 +++ b/mako/cache.py Wed Mar 07 15:24:22 2012 -0500 @@ -85,6 +85,17 @@ creation_function,
    • self._get_cache_kw(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, context)) + def set(self, key, value, kw): """Place a value in the cache.

@@ -151,18 +162,31 @@

self.invalidate(name, M_defname=name)

- def _get_cache_kw(self, kw): + def _get_cache_kw(self, kw, context=None): + if context: + ctx_args = self.impl.get_template_arguments(context) + else: + ctx_args = None defname = kw.pop('M_defname', None) if not defname: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) + if ctx_args: + tmpl_kw.update(ctx_args) return tmpl_kw elif defname in self._def_regions: - return self._def_regions[defname] + tmpl_kw = self._def_regions[defname] + if ctx_args: + tmpl_kw = tmpl_kw.copy() + tmpl_kw.update(ctx_args) + return tmpl_kw else: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) self._def_regions[defname] = tmpl_kw + if ctx_args: + tmpl_kw = tmpl_kw.copy() + tmpl_kw.update(ctx_args) return tmpl_kw

class CacheImpl(object): @@ -171,6 +195,9 @@ def init(self, cache): self.cache = cache

+ def get_template_arguments(self, context): + return {} + 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 -r 2f022a271212b220519305649e6a8490cf4399ff mako/codegen.py --- a/mako/codegen.py Tue Feb 21 09:49:40 2012 -0500 +++ b/mako/codegen.py Wed Mar 07 15:24:22 2012 -0500 @@ -633,7 +633,7 @@ ) if buffered: s = "context.get('local')."\ - "cache.get_or_create(%s, lambda:M_%s(%s), %sM_defname=%r)" % \ + "cache._ctx_get_or_create(%s, lambda:M_%s(%s), context, %sM_defname=%r)" % \ (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name @@ -644,7 +644,7 @@ else: self.printer.writelines( "M_writer(context.get('local')." - "cache.get_or_create(%s, lambda:M_%s(%s), %sM_defname=%r))" % + "cache._ctx_get_or_create(%s, lambda:M_%s(%s), context, %sM_defname=%r))" % (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name,

Comments (3)

  1. Mike Bayer reporter

    another version where the impl just specifies the keys it wants at the class level:

    1. !diff diff -r 2f022a271212b220519305649e6a8490cf4399ff mako/cache.py --- a/mako/cache.py Tue Feb 21 09:49:40 2012 -0500 +++ b/mako/cache.py Wed Mar 07 15:41:38 2012 -0500 @@ -85,6 +85,17 @@ creation_function,
      • self._get_cache_kw(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, context)) + def set(self, key, value, kw): """Place a value in the cache.

    @@ -151,18 +162,34 @@

    self.invalidate(name, M_defname=name)

    - def _get_cache_kw(self, kw): + def _get_cache_kw(self, kw, context=None): + if context: + ctx_args = dict( + (key, context.data.get(key)) + for key in self.impl.context_keys + ) + else: + ctx_args = None defname = kw.pop('M_defname', None) if not defname: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) + if ctx_args: + tmpl_kw.update(ctx_args) return tmpl_kw elif defname in self._def_regions: - return self._def_regions[defname] + tmpl_kw = self._def_regions[defname] + if ctx_args: + tmpl_kw = tmpl_kw.copy() + tmpl_kw.update(ctx_args) + return tmpl_kw else: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) self._def_regions[defname] = tmpl_kw + if ctx_args: + tmpl_kw = tmpl_kw.copy() + tmpl_kw.update(ctx_args) return tmpl_kw

    class CacheImpl(object): @@ -171,6 +198,8 @@ def init(self, cache): self.cache = cache

    + context_keys = () + 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 -r 2f022a271212b220519305649e6a8490cf4399ff mako/codegen.py --- a/mako/codegen.py Tue Feb 21 09:49:40 2012 -0500 +++ b/mako/codegen.py Wed Mar 07 15:41:38 2012 -0500 @@ -633,7 +633,7 @@ ) if buffered: s = "context.get('local')."\ - "cache.get_or_create(%s, lambda:M_%s(%s), %sM_defname=%r)" % \ + "cache._ctx_get_or_create(%s, lambda:M_%s(%s), context, %sM_defname=%r)" % \ (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name @@ -644,7 +644,7 @@ else: self.printer.writelines( "M_writer(context.get('local')." - "cache.get_or_create(%s, lambda:M_%s(%s), %sM_defname=%r))" % + "cache._ctx_get_or_create(%s, lambda:M_%s(%s), context, %sM_defname=%r))" % (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name, diff -r 2f022a271212b220519305649e6a8490cf4399ff mako/parsetree.py --- a/mako/parsetree.py Tue Feb 21 09:49:40 2012 -0500 +++ b/mako/parsetree.py Wed Mar 07 15:41:38 2012 -0500 @@ -331,7 +331,7 @@ super(IncludeTag, self).init( keyword, attributes, - ('file', 'import', 'args'), + ('file', 'import', 'args', 'full'), (), ('file',), kwargs) self.page_args = ast.PythonCode( "DUMMY(%s)" % attributes.get('args', ''), diff -r 2f022a271212b220519305649e6a8490cf4399ff mako/runtime.py --- a/mako/runtime.py Tue Feb 21 09:49:40 2012 -0500 +++ b/mako/runtime.py Wed Mar 07 15:41:38 2012 -0500 @@ -571,7 +571,8 @@ context._clean_inheritance_tokens(), template) callable_(ctx, _kwargs_for_include(callable_, context._data, kwargs)) - + + def _inherit_from(context, uri, calling_uri): """called by the _inherit method in template modules to set up the inheritance chain at the start of a template's

  2. Log in to comment