Skip to content
Myr

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 file

Take it

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

lines 1–4why, one step back
1from jinja2 import nodes
2from jinja2.ext import Extension34

more updates on the extension APIArmin Ronacherbefore adoption762079c

lines 5–6why, one step back
5class 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 back
7 tags = set(['cache'])
89 def __init__(self, environment):

more updates on the extension APIArmin Ronacherbefore adoption762079c

line 10why, one step back
10 super(FragmentCacheExtension, self).__init__(environment)

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

line 11why, one step back
11

more updates on the extension APIArmin Ronacherbefore adoption762079c

lines 12–16why, one step back
12 # add the defaults to the environment
13 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 back
17
18 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()]27

more updates on the extension APIArmin Ronacherbefore adoption762079c

lines 28–29why, one step back
28 # if there is a comma, the user provided a timeout. If not use
29 # 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 back
30 if parser.stream.skip_if('comma'):

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

lines 31–40why, one step back
31 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 back
41 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 back
43
44 def _cache_support(self, name, timeout, caller):45 """Helper callback."""

more updates on the extension APIArmin Ronacherbefore adoption762079c

line 46why, one step back
46 key = self.environment.fragment_cache_prefix + name

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

line 47why, one step back
47

more updates on the extension APIArmin Ronacherbefore adoption762079c

line 48why, one step back
48 # try to load the block from the cache

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

lines 49–50why, one step back
49 # if there is no fragment in the cache, render it and store
50 # it in the cache.

more updates on the extension APIArmin Ronacherbefore adoption762079c

line 51why, one step back
51 rv = self.environment.fragment_cache.get(key)

Fixed a documenation bug in the cache extension.Armin Ronacherbefore adoption7850dc5

line 52why, one step back
52 if rv is not None:

more updates on the extension APIArmin Ronacherbefore adoption762079c

line 53why, one step back
53 return rv

Priority of `not` raised. It's now possible to write `not foo in bar`Armin Ronacherbefore adoptiond89f0f3

lines 54–55why, one step back
54 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 back
56 return rv