Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/utils.py

Issue 29398791: Issue 5044 - Support absolute paths for Jinja templates (Closed)
Patch Set: Created March 30, 2017, 5:13 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/test_utils.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2017 eyeo GmbH 2 # Copyright (C) 2006-2017 eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 try: 139 try:
140 return email.encode('ascii') 140 return email.encode('ascii')
141 except UnicodeEncodeError: 141 except UnicodeEncodeError:
142 return '%s@%s' % (match.group(1).encode('ascii'), 142 return '%s@%s' % (match.group(1).encode('ascii'),
143 match.group(2).encode('idna')) 143 match.group(2).encode('idna'))
144 144
145 _template_cache = {} 145 _template_cache = {}
146 146
147 147
148 def get_template(template, autoescape=True, template_path=siteScriptsPath): 148 def get_template(template, autoescape=True, template_path=siteScriptsPath):
149 """Parses and returns a Jinja2 template""" 149 """Load Jinja2 template.
150
151 If `template` is a relative path, it's looked up inside `template_path`.
152 If it's an absolute path, `template_path` is not used.
153
154 Note: Each template will only be loaded once (when first requested). After
155 that it will be cached and reused -- any changes on the filesystem will be
156 ignored.
157 """
158 if os.path.isabs(template):
159 template_path, template = os.path.split(template)
150 key = (template_path, template, autoescape) 160 key = (template_path, template, autoescape)
Wladimir Palant 2017/03/30 17:47:42 This caching key wasn't entirely correct before, a
Vasily Kuznetsov 2017/03/30 18:35:53 Done
151 if not key in _template_cache: 161 if key not in _template_cache:
Sebastian Noack 2017/03/30 17:41:24 Assuming this was the only occurrence of `not x in
Vasily Kuznetsov 2017/03/30 18:35:53 Done
152 if autoescape: 162 if autoescape:
153 env = get_template_environment(template_path) 163 env = get_template_environment(template_path)
154 else: 164 else:
155 env = get_unescaped_template_environment(template_path) 165 env = get_unescaped_template_environment(template_path)
156 _template_cache[key] = env.get_template(template) 166 _template_cache[key] = env.get_template(template)
157 return _template_cache[key] 167 return _template_cache[key]
158 168
159 169
160 @cached(float('inf')) 170 @cached(float('inf'))
161 def get_template_environment(template_path): 171 def get_template_environment(template_path):
(...skipping 26 matching lines...) Expand all
188 Returns a custom Jinja2 template environment with additional filters. 198 Returns a custom Jinja2 template environment with additional filters.
189 """ 199 """
190 from sitescripts.templateFilters import filters 200 from sitescripts.templateFilters import filters
191 import jinja2 201 import jinja2
192 if not loader: 202 if not loader:
193 loader = jinja2.FileSystemLoader(siteScriptsPath) 203 loader = jinja2.FileSystemLoader(siteScriptsPath)
194 env = jinja2.Environment(loader=loader, autoescape=True) 204 env = jinja2.Environment(loader=loader, autoescape=True)
195 env.filters.update(filters) 205 env.filters.update(filters)
196 env.filters.update(additional_filters) 206 env.filters.update(additional_filters)
197 return env 207 return env
OLDNEW
« no previous file with comments | « no previous file | tests/test_utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld