New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support context passed to cacheimpl #185

Closed
sqlalchemy-bot opened this Issue Mar 7, 2012 · 3 comments

Comments

Projects
None yet
1 participant
@sqlalchemy-bot

sqlalchemy-bot commented Mar 7, 2012

Migrated issue, originally created by Michael Bayer (@zzzeek)

possible patch:

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),  %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
@@ -644,7 +644,7 @@
         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, 
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Mar 7, 2012

Michael Bayer (@zzzeek) wrote:

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

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),  %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
@@ -644,7 +644,7 @@
         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 -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
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Mar 24, 2012

Michael Bayer (@zzzeek) wrote:

went with a simple boolean pass_context, 6fe1a4b

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Mar 24, 2012

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment