From a679378455d79ee34b3465323fcba438f0afac74 Mon Sep 17 00:00:00 2001 From: Mike Bayer <mike_mp@zzzcomputing.com> Date: Sat, 25 Oct 2008 18:21:18 +0000 Subject: [PATCH] - beaker bump - added "cache_enabled=True" flag to Template, TemplateLookup. Setting this to False causes cache operations to "pass through" and execute every time; this flag should be integrated in Pylons with its own cache_enabled configuration setting. --- CHANGES | 6 ++++++ lib/mako/lookup.py | 19 +++++++++++++++++-- lib/mako/runtime.py | 7 +++++++ lib/mako/template.py | 8 +++++--- setup.py | 2 +- test/cache.py | 16 ++++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index 2e2d893..584c752 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,12 @@ e.g. ${local.cache.get('somekey')} or template.cache.invalidate_body() +- added "cache_enabled=True" flag to Template, + TemplateLookup. Setting this to False causes cache + operations to "pass through" and execute every time; + this flag should be integrated in Pylons with its own + cache_enabled configuration setting. + - the Cache object now supports invalidate_def(name), invalidate_body(), invalidate_closure(name), invalidate(key), which will remove the given key diff --git a/lib/mako/lookup.py b/lib/mako/lookup.py index f720e4c..8926254 100644 --- a/lib/mako/lookup.py +++ b/lib/mako/lookup.py @@ -39,7 +39,7 @@ class TemplateCollection(object): class TemplateLookup(TemplateCollection): def __init__(self, directories=None, module_directory=None, filesystem_checks=True, collection_size=-1, format_exceptions=False, error_handler=None, disable_unicode=False, output_encoding=None, encoding_errors='strict', cache_type=None, cache_dir=None, cache_url=None, - modulename_callable=None, default_filters=None, buffer_filters=[], imports=None, input_encoding=None, preprocessor=None): + cache_enabled=True, modulename_callable=None, default_filters=None, buffer_filters=[], imports=None, input_encoding=None, preprocessor=None): if isinstance(directories, basestring): directories = [directories] self.directories = [posixpath.normpath(d) for d in directories or []] @@ -47,7 +47,22 @@ class TemplateLookup(TemplateCollection): self.modulename_callable = modulename_callable self.filesystem_checks = filesystem_checks self.collection_size = collection_size - self.template_args = {'format_exceptions':format_exceptions, 'error_handler':error_handler, 'disable_unicode':disable_unicode, 'output_encoding':output_encoding, 'encoding_errors':encoding_errors, 'input_encoding':input_encoding, 'module_directory':module_directory, 'cache_type':cache_type, 'cache_dir':cache_dir or module_directory, 'cache_url':cache_url, 'default_filters':default_filters, 'buffer_filters':buffer_filters, 'imports':imports, 'preprocessor':preprocessor} + self.template_args = { + 'format_exceptions':format_exceptions, + 'error_handler':error_handler, + 'disable_unicode':disable_unicode, + 'output_encoding':output_encoding, + 'encoding_errors':encoding_errors, + 'input_encoding':input_encoding, + 'module_directory':module_directory, + 'cache_type':cache_type, + 'cache_dir':cache_dir or module_directory, + 'cache_url':cache_url, + 'cache_enabled':cache_enabled, + 'default_filters':default_filters, + 'buffer_filters':buffer_filters, + 'imports':imports, + 'preprocessor':preprocessor} if collection_size == -1: self.__collection = {} self._uri_cache = {} diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py index a82ffb4..33ac999 100644 --- a/lib/mako/runtime.py +++ b/lib/mako/runtime.py @@ -196,6 +196,13 @@ class Namespace(object): def get_cached(self, key, **kwargs): if self.template: + if not self.template.cache_enabled: + createfunc = kwargs.get('createfunc', None) + if createfunc: + return createfunc() + else: + return None + if self.template.cache_dir: kwargs.setdefault('data_dir', self.template.cache_dir) if self.template.cache_type: diff --git a/lib/mako/template.py b/lib/mako/template.py index d50eb8a..13823ea 100644 --- a/lib/mako/template.py +++ b/lib/mako/template.py @@ -18,7 +18,7 @@ class Template(object): def __init__(self, text=None, filename=None, uri=None, format_exceptions=False, error_handler=None, lookup=None, output_encoding=None, encoding_errors='strict', module_directory=None, cache_type=None, cache_dir=None, cache_url=None, module_filename=None, input_encoding=None, disable_unicode=False, default_filters=None, - buffer_filters=[], imports=None, preprocessor=None): + buffer_filters=[], imports=None, preprocessor=None, cache_enabled=True): """construct a new Template instance using either literal template text, or a previously loaded template module text - textual template source, or None if a module is to be provided @@ -106,6 +106,7 @@ class Template(object): self.cache_type = cache_type self.cache_dir = cache_dir self.cache_url = cache_url + self.cache_enabled = cache_enabled def source(self): """return the template source code for this Template.""" @@ -174,7 +175,7 @@ class ModuleTemplate(Template): template=None, template_filename=None, module_source=None, template_source=None, output_encoding=None, encoding_errors='strict', disable_unicode=False, format_exceptions=False, - error_handler=None, lookup=None, cache_type=None, cache_dir=None, cache_url=None + error_handler=None, lookup=None, cache_type=None, cache_dir=None, cache_url=None, cache_enabled=True ): self.module_id = re.sub(r'\W', "_", module._template_uri) self.uri = module._template_uri @@ -193,7 +194,8 @@ class ModuleTemplate(Template): self.cache_type = cache_type self.cache_dir = cache_dir self.cache_url = cache_url - + self.cache_enabled = cache_enabled + class DefTemplate(Template): """a Template which represents a callable def in a parent template.""" def __init__(self, parent, callable_): diff --git a/setup.py b/setup.py index fc41632..df456fc 100644 --- a/setup.py +++ b/setup.py @@ -37,7 +37,7 @@ SVN version: scripts=['scripts/mako-render'], zip_safe=False, install_requires=[ - 'Beaker==dev,>=1.0.4dev', + 'Beaker==dev,>=1.1dev', ], entry_points=""" [python.templating.engines] diff --git a/test/cache.py b/test/cache.py index 80f6416..1049b30 100644 --- a/test/cache.py +++ b/test/cache.py @@ -49,6 +49,22 @@ class CacheTest(unittest.TestCase): ] assert m.kwargs == {} + def test_cache_enable(self): + t = Template(""" + <%! + callcount = [0] + %> + <%def name="foo()" cached="True"> + <% callcount[0] += 1 %> + </%def> + ${foo()} + ${foo()} + callcount: ${callcount} + """, cache_enabled=False) + m = self._install_mock_cache(t) + + assert t.render().strip() =="callcount: [2]" + def test_nested_def(self): t = Template(""" <%! -- GitLab