cache_extension.py
This file
Path
docs/cache_extension.py
56 lines · 2.1 KiB · Python
Last changed by
Fixed a documenation bug in the cache extension.Armin Ronacherbefore adoption7850dc5
History of this fileTake it
First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
lines 1–4why, one step back1from jinja2 import nodes2from jinja2.ext import Extension34more updates on the extension APIArmin Ronacherbefore adoption762079c
lines 5–6why, one step back5class FragmentCacheExtension(Extension):6 # a set of names that trigger the extension.First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
lines 7–9why, one step back7 tags = set(['cache'])89 def __init__(self, environment):more updates on the extension APIArmin Ronacherbefore adoption762079c
line 10why, one step back10 super(FragmentCacheExtension, self).__init__(environment)First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
line 11why, one step back11more updates on the extension APIArmin Ronacherbefore adoption762079c
lines 12–16why, one step back12 # add the defaults to the environment13 environment.extend(14 fragment_cache_prefix='',15 fragment_cache=None16 )First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
lines 17–27why, one step back1718 def parse(self, parser):19 # the first token is the token that started the tag. In our case20 # we only listen to ``'cache'`` so this will be a name token with21 # `cache` as value. We get the line number so that we can give22 # that line number to the nodes we create by hand.23 lineno = parser.stream.next().lineno2425 # now we parse a single expression that is used as cache key.26 args = [parser.parse_expression()]27more updates on the extension APIArmin Ronacherbefore adoption762079c
lines 28–29why, one step back28 # if there is a comma, the user provided a timeout. If not use29 # None as second parameter.added parsing code for "for item in seq recursive" and improved parser interface a bitArmin Ronacherbefore adoptionfdf9530
line 30why, one step back30 if parser.stream.skip_if('comma'):First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
lines 31–40why, one step back31 args.append(parser.parse_expression())32 else:33 args.append(nodes.Const(None))3435 # now we parse the body of the cache block up to `endcache` and36 # drop the needle (which would always be `endcache` in that case)37 body = parser.parse_statements(['name:endcache'], drop_needle=True)3839 # now return a `CallBlock` node that calls our _cache_support40 # helper method on this extension.fixed a bug in extension handlingArmin Ronacherbefore adoption27069d7
lines 41–42why, one step back41 return nodes.CallBlock(self.call_method('_cache_support', args),42 [], [], body).set_lineno(lineno)First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
lines 43–45why, one step back4344 def _cache_support(self, name, timeout, caller):45 """Helper callback."""more updates on the extension APIArmin Ronacherbefore adoption762079c
line 46why, one step back46 key = self.environment.fragment_cache_prefix + nameFirst extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
line 47why, one step back47more updates on the extension APIArmin Ronacherbefore adoption762079c
line 48why, one step back48 # try to load the block from the cacheFirst extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
lines 49–50why, one step back49 # if there is no fragment in the cache, render it and store50 # it in the cache.more updates on the extension APIArmin Ronacherbefore adoption762079c
line 51why, one step back51 rv = self.environment.fragment_cache.get(key)Fixed a documenation bug in the cache extension.Armin Ronacherbefore adoption7850dc5
line 52why, one step back52 if rv is not None:more updates on the extension APIArmin Ronacherbefore adoption762079c
line 53why, one step back53 return rvPriority of `not` raised. It's now possible to write `not foo in bar`Armin Ronacherbefore adoptiond89f0f3
lines 54–55why, one step back54 rv = caller()55 self.environment.fragment_cache.add(key, rv, timeout)First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9
line 56why, one step back56 return rv