comparison piecrust/templating/jinja/extensions.py @ 908:cedefb806bfd

jinja: Use the extensions performance timer.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 23 Jul 2017 08:31:05 -0700
parents 58e28ba02fb7
children 8adc27285d93
comparison
equal deleted inserted replaced
907:3e69f18912f5 908:cedefb806bfd
13 13
14 def parse(self, parser): 14 def parse(self, parser):
15 lineno = next(parser.stream).lineno 15 lineno = next(parser.stream).lineno
16 args = [parser.parse_expression()] 16 args = [parser.parse_expression()]
17 body = parser.parse_statements(['name:endpcformat'], drop_needle=True) 17 body = parser.parse_statements(['name:endpcformat'], drop_needle=True)
18 return CallBlock(self.call_method('_format', args), 18 return CallBlock(self.call_method('_formatTimed', args),
19 [], [], body).set_lineno(lineno) 19 [], [], body).set_lineno(lineno)
20
21 def _formatTimed(self, format_name, caller=None):
22 with self.environment.app.env.stats.timerScope(
23 'JinjaTemplateEngine_extensions'):
24 return self._format(format_name, caller)
20 25
21 def _format(self, format_name, caller=None): 26 def _format(self, format_name, caller=None):
22 body = caller() 27 body = caller()
23 text = format_text(self.environment.app, 28 text = format_text(self.environment.app,
24 format_name, 29 format_name,
55 60
56 # body of the block 61 # body of the block
57 body = parser.parse_statements(['name:endhighlight', 'name:endgeshi'], 62 body = parser.parse_statements(['name:endhighlight', 'name:endgeshi'],
58 drop_needle=True) 63 drop_needle=True)
59 64
60 return CallBlock(self.call_method('_highlight', args, kwargs), 65 return CallBlock(self.call_method('_highlightTimed', args, kwargs),
61 [], [], body).set_lineno(lineno) 66 [], [], body).set_lineno(lineno)
67
68 def _highlightTimed(self, lang, line_numbers=False, use_classes=False,
69 css_class=None, css_id=None, caller=None):
70 with self.environment.app.env.stats.timerScope(
71 'JinjaTemplateEngine_extensions'):
72 return self._highlight(lang, line_numbers, use_classes,
73 css_class, css_id, caller)
62 74
63 def _highlight(self, lang, line_numbers=False, use_classes=False, 75 def _highlight(self, lang, line_numbers=False, use_classes=False,
64 css_class=None, css_id=None, caller=None): 76 css_class=None, css_id=None, caller=None):
65 from pygments import highlight 77 from pygments import highlight
66 from pygments.formatters import HtmlFormatter 78 from pygments.formatters import HtmlFormatter
118 # now we parse the body of the cache block up to `endpccache` and 130 # now we parse the body of the cache block up to `endpccache` and
119 # drop the needle (which would always be `endpccache` in that case) 131 # drop the needle (which would always be `endpccache` in that case)
120 body = parser.parse_statements(['name:endpccache', 'name:endcache'], 132 body = parser.parse_statements(['name:endpccache', 'name:endcache'],
121 drop_needle=True) 133 drop_needle=True)
122 134
123 # now return a `CallBlock` node that calls our _cache_support 135 # now return a `CallBlock` node that calls our _renderCache
124 # helper method on this extension. 136 # helper method on this extension.
125 return CallBlock(self.call_method('_cache_support', args), 137 return CallBlock(self.call_method('_renderCacheTimed', args),
126 [], [], body).set_lineno(lineno) 138 [], [], body).set_lineno(lineno)
127 139
128 def _cache_support(self, name, caller): 140 def _renderCacheTimed(self, name, caller):
141 with self.environment.app.env.stats.timerScope(
142 'JinjaTemplateEngine_extensions'):
143 return self._renderCache(name, caller)
144
145 def _renderCache(self, name, caller):
129 key = self.environment.piecrust_cache_prefix + name 146 key = self.environment.piecrust_cache_prefix + name
130 147
131 rcs = self.environment.app.env.render_ctx_stack 148 rcs = self.environment.app.env.render_ctx_stack
132 rdr_pass = rcs.current_ctx.current_pass_info 149 rdr_pass = rcs.current_ctx.current_pass_info
133 150
134 # try to load the block from the cache 151 # try to load the block from the cache
135 # if there is no fragment in the cache, render it and store 152 # if there is no fragment in the cache, render it and store
136 # it in the cache. 153 # it in the cache.
137 pair = self.environment.piecrust_cache.get(key)
138 if pair is not None:
139 rdr_pass.used_source_names.update(pair[1])
140 return pair[0]
141
142 pair = self.environment.piecrust_cache.get(key) 154 pair = self.environment.piecrust_cache.get(key)
143 if pair is not None: 155 if pair is not None:
144 rdr_pass.used_source_names.update(pair[1]) 156 rdr_pass.used_source_names.update(pair[1])
145 return pair[0] 157 return pair[0]
146 158