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

Side by Side Diff: cms/converters.py

Issue 29327966: Issue 3084 - [cms] Show full tracebacks for exceptions passing template code (Closed)
Patch Set: Addressed comments Created Sept. 16, 2015, 7:03 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 | « cms/bin/generate_static_pages.py ('k') | cms/sources.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 # 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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 missing_translations = 0 113 missing_translations = 0
114 total_translations = 0 114 total_translations = 0
115 115
116 def __init__(self, params, key="pagedata"): 116 def __init__(self, params, key="pagedata"):
117 self._params = params 117 self._params = params
118 self._key = key 118 self._key = key
119 self._attribute_parser = AttributeParser(self.whitelist) 119 self._attribute_parser = AttributeParser(self.whitelist)
120 self._seen_defaults = {} 120 self._seen_defaults = {}
121 121
122 # Read in any parameters specified at the beginning of the file 122 # Read in any parameters specified at the beginning of the file
123 lines = params[key].splitlines(True) 123 data, filename = params[key]
124 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): 124 lines = data.splitlines(True)
125 name, value = lines.pop(0).split("=", 1) 125 for i, line in enumerate(lines):
126 if not re.search(r"^\s*[\w\-]+\s*=", line):
127 break
128 name, value = line.split("=", 1)
126 params[name.strip()] = value.strip() 129 params[name.strip()] = value.strip()
127 params[key] = "".join(lines) 130 lines[i] = "\n"
131 params[key] = ("".join(lines), filename)
128 132
129 def localize_string(self, page, name, default, comment, localedata, escapes): 133 def localize_string(self, page, name, default, comment, localedata, escapes):
130 def escape(s): 134 def escape(s):
131 return re.sub(r".", 135 return re.sub(r".",
132 lambda match: escapes.get(match.group(0), match.group(0)), 136 lambda match: escapes.get(match.group(0), match.group(0)),
133 s, flags=re.S) 137 s, flags=re.S)
134 def re_escape(s): 138 def re_escape(s):
135 return re.escape(escape(s)) 139 return re.escape(escape(s))
136 140
137 # Handle duplicated strings 141 # Handle duplicated strings
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 head = [] 274 head = []
271 def add_to_head(match): 275 def add_to_head(match):
272 head.append(match.group(1)) 276 head.append(match.group(1))
273 return "" 277 return ""
274 body = re.sub(r"<head>(.*?)</head>", add_to_head, result, flags=re.S) 278 body = re.sub(r"<head>(.*?)</head>", add_to_head, result, flags=re.S)
275 return "".join(head), body 279 return "".join(head), body
276 else: 280 else:
277 return result 281 return result
278 282
279 class RawConverter(Converter): 283 class RawConverter(Converter):
280 def get_html(self, source): 284 def get_html(self, (source, filename)):
kzar 2015/09/17 08:41:17 Any reason we don't just have source and filename
Sebastian Noack 2015/09/17 08:49:59 Indeed, we could do the unpacking in the calling c
Sebastian Noack 2015/09/17 10:01:23 This bothered me. So I uploaded another patch set.
281 result = self.insert_localized_strings(source, html_escapes) 285 result = self.insert_localized_strings(source, html_escapes)
282 result = self.process_links(result) 286 result = self.process_links(result)
283 return result 287 return result
284 288
285 class MarkdownConverter(Converter): 289 class MarkdownConverter(Converter):
286 include_start_regex = r'(?:%s|%s)' % ( 290 include_start_regex = r'(?:%s|%s)' % (
287 Converter.include_start_regex, 291 Converter.include_start_regex,
288 re.escape(jinja2.escape(Converter.include_start_regex)) 292 re.escape(jinja2.escape(Converter.include_start_regex))
289 ) 293 )
290 include_end_regex = r'(?:%s|%s)' % ( 294 include_end_regex = r'(?:%s|%s)' % (
291 Converter.include_end_regex, 295 Converter.include_end_regex,
292 re.escape(jinja2.escape(Converter.include_end_regex)) 296 re.escape(jinja2.escape(Converter.include_end_regex))
293 ) 297 )
294 298
295 def get_html(self, source): 299 def get_html(self, (source, filename)):
296 def remove_unnecessary_entities(match): 300 def remove_unnecessary_entities(match):
297 char = unichr(int(match.group(1))) 301 char = unichr(int(match.group(1)))
298 if char in html_escapes: 302 if char in html_escapes:
299 return match.group(0) 303 return match.group(0)
300 else: 304 else:
301 return char 305 return char
302 306
303 escapes = {} 307 escapes = {}
304 for char in markdown.Markdown.ESCAPED_CHARS: 308 for char in markdown.Markdown.ESCAPED_CHARS:
305 escapes[char] = "&#" + str(ord(char)) + ";" 309 escapes[char] = "&#" + str(ord(char)) + ";"
306 for key, value in html_escapes.iteritems(): 310 for key, value in html_escapes.iteritems():
307 escapes[key] = value 311 escapes[key] = value
308 312
309 md = markdown.Markdown(output="html5", extensions=["attr_list"]) 313 md = markdown.Markdown(output="html5", extensions=["attr_list"])
310 md.preprocessors["html_block"].markdown_in_raw = True 314 md.preprocessors["html_block"].markdown_in_raw = True
311 315
312 def to_html(s): 316 def to_html(s):
313 return re.sub(r'</?p>', '', md.convert(s)) 317 return re.sub(r'</?p>', '', md.convert(s))
314 318
315 result = self.insert_localized_strings(source, escapes, to_html) 319 result = self.insert_localized_strings(source, escapes, to_html)
316 result = md.convert(result) 320 result = md.convert(result)
317 result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result) 321 result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result)
318 result = self.process_links(result) 322 result = self.process_links(result)
319 return result 323 return result
320 324
325 class SourceTemplateLoader(jinja2.BaseLoader):
326 def __init__(self, source):
327 self.source = source
328
329 def get_source(self, environment, template):
330 try:
331 result = self.source.read_file(template + ".tmpl")
332 except Exception:
333 raise jinja2.TemplateNotFound(template)
334 return result + (None,)
335
321 class TemplateConverter(Converter): 336 class TemplateConverter(Converter):
322 class _SourceLoader(jinja2.BaseLoader):
323 def __init__(self, source):
324 self.source = source
325
326 def get_source(self, environment, template):
327 try:
328 return self.source.read_file(template + ".tmpl"), None, None
329 except Exception:
330 raise jinja2.TemplateNotFound(template)
331
332 def __init__(self, *args, **kwargs): 337 def __init__(self, *args, **kwargs):
333 Converter.__init__(self, *args, **kwargs) 338 Converter.__init__(self, *args, **kwargs)
334 339
335 filters = { 340 filters = {
336 "translate": self.translate, 341 "translate": self.translate,
337 "linkify": self.linkify, 342 "linkify": self.linkify,
338 "toclist": self.toclist, 343 "toclist": self.toclist,
339 } 344 }
340 345
341 globals = { 346 globals = {
342 "get_string": self.get_string, 347 "get_string": self.get_string,
343 "get_page_content": self.get_page_content, 348 "get_page_content": self.get_page_content,
344 } 349 }
345 350
346 for dirname, dictionary in [("filters", filters), ("globals", globals)]: 351 for dirname, dictionary in [("filters", filters), ("globals", globals)]:
347 for filename in self._params["source"].list_files(dirname): 352 for filename in self._params["source"].list_files(dirname):
348 root, ext = os.path.splitext(filename) 353 root, ext = os.path.splitext(filename)
349 if ext.lower() != ".py": 354 if ext.lower() != ".py":
350 continue 355 continue
351 356
352 path = "%s/%s" % (dirname, filename) 357 path = "%s/%s" % (dirname, filename)
358 namespace = self._params["source"].exec_file(path)
359
353 name = os.path.basename(root) 360 name = os.path.basename(root)
354 dictionary[name] = self._params["source"].import_symbol(path, name) 361 try:
362 dictionary[name] = namespace[name]
363 except KeyError:
364 raise Exception("Expected symbol %r not found in %r" % (name, path))
355 365
356 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) 366 self._env = jinja2.Environment(loader=SourceTemplateLoader(self._params["sou rce"]), autoescape=True)
357 self._env.filters.update(filters) 367 self._env.filters.update(filters)
358 self._env.globals.update(globals) 368 self._env.globals.update(globals)
359 369
360 def get_html(self, source): 370 def get_html(self, (source, filename)):
361 template = self._env.from_string(source) 371 env = self._env
362 module = template.make_module(self._params) 372 code = env.compile(source, None, filename)
373 template = jinja2.Template.from_code(env, code, env.globals)
kzar 2015/09/17 08:41:17 I was trying to look up jinja2.Template.from_code
Sebastian Noack 2015/09/17 08:49:59 Indeed, it's not documented, at least not in the o
Wladimir Palant 2015/09/17 21:30:14 It's documented under http://code.nabla.net/doc/ji
374
375 try:
376 module = template.make_module(self._params)
377 except Exception:
378 env.handle_exception()
379
363 for key, value in module.__dict__.iteritems(): 380 for key, value in module.__dict__.iteritems():
364 if not key.startswith("_"): 381 if not key.startswith("_"):
365 self._params[key] = value 382 self._params[key] = value
366 383
367 result = unicode(module) 384 result = unicode(module)
368 result = self.process_links(result) 385 result = self.process_links(result)
369 return result 386 return result
370 387
371 def translate(self, default, name, comment=None): 388 def translate(self, default, name, comment=None):
372 return jinja2.Markup(self.localize_string( 389 return jinja2.Markup(self.localize_string(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 stack.pop() 437 stack.pop()
421 stack[-1]["subitems"].append(item) 438 stack[-1]["subitems"].append(item)
422 stack.append(item) 439 stack.append(item)
423 return structured 440 return structured
424 441
425 converters = { 442 converters = {
426 "html": RawConverter, 443 "html": RawConverter,
427 "md": MarkdownConverter, 444 "md": MarkdownConverter,
428 "tmpl": TemplateConverter, 445 "tmpl": TemplateConverter,
429 } 446 }
OLDNEW
« no previous file with comments | « cms/bin/generate_static_pages.py ('k') | cms/sources.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld