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

Side by Side Diff: sitescripts/utils.py

Issue 29328589: Issue 3122 - Add testpages.web.sitekey_frame URL handler (Closed)
Patch Set: Further improvements Created Oct. 8, 2015, noon
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 raise ValueError 127 raise ValueError
128 128
129 try: 129 try:
130 return email.encode('ascii') 130 return email.encode('ascii')
131 except UnicodeEncodeError: 131 except UnicodeEncodeError:
132 return '%s@%s' % (match.group(1).encode('ascii'), 132 return '%s@%s' % (match.group(1).encode('ascii'),
133 match.group(2).encode('idna')) 133 match.group(2).encode('idna'))
134 134
135 _template_cache = {} 135 _template_cache = {}
136 136
137 def get_template(template, autoescape=True): 137 def get_template(template, autoescape=True, template_path=siteScriptsPath):
138 """Parses and returns a Jinja2 template""" 138 """Parses and returns a Jinja2 template"""
139 key = (template, autoescape) 139 key = (template_path, template, autoescape)
140 if not key in _template_cache: 140 if not key in _template_cache:
141 if autoescape: 141 if autoescape:
142 env = get_template_environment() 142 env = get_template_environment(template_path)
143 else: 143 else:
144 env = get_unescaped_template_environment() 144 env = get_unescaped_template_environment(template_path)
145 _template_cache[key] = env.get_template(template) 145 _template_cache[key] = env.get_template(template)
146 return _template_cache[key] 146 return _template_cache[key]
147 147
148 @cached(float("inf")) 148 @cached(float("inf"))
149 def get_template_environment(): 149 def get_template_environment(template_path):
150 """ 150 """
151 Returns a Jinja2 template environment with autoescaping enabled. 151 Returns a Jinja2 template environment with autoescaping enabled.
152 """ 152 """
153 from sitescripts.templateFilters import filters 153 from sitescripts.templateFilters import filters
154 import jinja2 154 import jinja2
155 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath), auto escape=True) 155 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path),
156 autoescape=True)
156 env.filters.update(filters) 157 env.filters.update(filters)
157 return env 158 return env
158 159
159 @cached(float("inf")) 160 @cached(float("inf"))
160 def get_unescaped_template_environment(): 161 def get_unescaped_template_environment(template_path):
161 """ 162 """
162 Returns a Jinja2 template environment without autoescaping. Don't use this t o 163 Returns a Jinja2 template environment without autoescaping. Don't use this t o
163 generate HTML files! 164 generate HTML files!
164 """ 165 """
165 from sitescripts.templateFilters import filters 166 from sitescripts.templateFilters import filters
166 import jinja2 167 import jinja2
167 env = jinja2.Environment(loader=jinja2.FileSystemLoader(siteScriptsPath)) 168 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path))
168 env.filters.update(filters) 169 env.filters.update(filters)
169 return env 170 return env
170 171
171 def get_custom_template_environment(additional_filters, loader=None): 172 def get_custom_template_environment(additional_filters, loader=None):
172 """ 173 """
173 Returns a custom Jinja2 template environment with additional filters. 174 Returns a custom Jinja2 template environment with additional filters.
174 """ 175 """
175 from sitescripts.templateFilters import filters 176 from sitescripts.templateFilters import filters
176 import jinja2 177 import jinja2
177 if not loader: 178 if not loader:
178 loader = jinja2.FileSystemLoader(siteScriptsPath) 179 loader = jinja2.FileSystemLoader(siteScriptsPath)
179 env = jinja2.Environment(loader=loader, autoescape=True) 180 env = jinja2.Environment(loader=loader, autoescape=True)
180 env.filters.update(filters) 181 env.filters.update(filters)
181 env.filters.update(additional_filters) 182 env.filters.update(additional_filters)
182 return env 183 return env
OLDNEW

Powered by Google App Engine
This is Rietveld