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

Unified Diff: cms/converters.py

Issue 29327966: Issue 3084 - [cms] Show full tracebacks for exceptions passing template code (Closed)
Patch Set: Created Sept. 15, 2015, 5:37 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 | cms/sources.py » ('j') | cms/sources.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/converters.py
===================================================================
--- a/cms/converters.py
+++ b/cms/converters.py
@@ -112,6 +112,7 @@
whitelist = {"a", "em", "strong", "code", "span"}
missing_translations = 0
total_translations = 0
+ removed_line = ''
def __init__(self, params, key="pagedata"):
self._params = params
@@ -121,9 +122,12 @@
# Read in any parameters specified at the beginning of the file
lines = params[key].splitlines(True)
- while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]):
- name, value = lines.pop(0).split("=", 1)
+ for i, line in enumerate(lines):
Sebastian Noack 2015/09/15 17:48:36 In order to prevent line numbers from being off we
+ if not re.search(r"^\s*[\w\-]+\s*=", line):
+ break
+ name, value = line.split("=", 1)
params[name.strip()] = value.strip()
+ lines[i] = self.removed_line
params[key] = "".join(lines)
def localize_string(self, page, name, default, comment, localedata, escapes):
@@ -246,6 +250,7 @@
name = match.group(1)
for format, converter_class in converters.iteritems():
if self._params["source"].has_include(name, format):
+ self._params["include"] = name
Sebastian Noack 2015/09/15 17:48:36 There seems to be no way to get the filename for i
self._params["includedata"] = self._params["source"].read_include(name, format)
converter = converter_class(self._params, key="includedata")
result = converter()
@@ -319,15 +324,7 @@
return result
class TemplateConverter(Converter):
- class _SourceLoader(jinja2.BaseLoader):
- def __init__(self, source):
- self.source = source
-
- def get_source(self, environment, template):
- try:
- return self.source.read_file(template + ".tmpl"), None, None
- except Exception:
- raise jinja2.TemplateNotFound(template)
+ removed_line = "{#\n#}"
def __init__(self, *args, **kwargs):
Converter.__init__(self, *args, **kwargs)
@@ -353,13 +350,30 @@
name = os.path.basename(root)
dictionary[name] = self._params["source"].import_symbol(path, name)
- self._env = jinja2.Environment(loader=self._SourceLoader(self._params["source"]), autoescape=True)
+ self._env = jinja2.Environment(loader=self._params["source"].get_template_loader(), autoescape=True)
self._env.filters.update(filters)
self._env.globals.update(globals)
+ def get_template_filename(self):
+ source = self._params["source"]
Sebastian Noack 2015/09/15 17:48:36 And here is where the problematic part starts. I d
+ if hasattr(source, "get_path"):
+ if self._key == "pagedata":
+ return source.get_path(source.page_filename(self._params["page"], "tmpl"))
+ if self._key == "includedata":
+ return source.get_path(source.include_filename(self._params["include"], "tmpl"))
+ if self._key == "templatedata":
+ return source.get_path(source.template_filename(self._params["template"]))
+
def get_html(self, source):
- template = self._env.from_string(source)
- module = template.make_module(self._params)
+ env = self._env
+ code = env.compile(source, None, self.get_template_filename())
Sebastian Noack 2015/09/15 17:48:36 There are two ways to make the template aware of t
+ template = env.template_class.from_code(env, code, env.globals)
+
+ try:
+ module = template.make_module(self._params)
+ except Exception:
+ env.handle_exception()
Sebastian Noack 2015/09/15 17:48:36 Note that this is necessary because as opposed to
+
for key, value in module.__dict__.iteritems():
if not key.startswith("_"):
self._params[key] = value
« no previous file with comments | « no previous file | cms/sources.py » ('j') | cms/sources.py » ('J')

Powered by Google App Engine
This is Rietveld