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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/test_utils.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/utils.py
===================================================================
--- a/sitescripts/utils.py
+++ b/sitescripts/utils.py
@@ -141,19 +141,29 @@
except UnicodeEncodeError:
return '%s@%s' % (match.group(1).encode('ascii'),
match.group(2).encode('idna'))
_template_cache = {}
def get_template(template, autoescape=True, template_path=siteScriptsPath):
- """Parses and returns a Jinja2 template"""
+ """Load Jinja2 template.
+
+ If `template` is a relative path, it's looked up inside `template_path`.
+ If it's an absolute path, `template_path` is not used.
+
+ Note: Each template will only be loaded once (when first requested). After
+ that it will be cached and reused -- any changes on the filesystem will be
+ ignored.
+ """
+ if os.path.isabs(template):
+ template_path, template = os.path.split(template)
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
- if not key in _template_cache:
+ 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
if autoescape:
env = get_template_environment(template_path)
else:
env = get_unescaped_template_environment(template_path)
_template_cache[key] = env.get_template(template)
return _template_cache[key]
« 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