Debian Codegraph @

third_party/python/Mako/mako/codegen.py

raw
1 # mako/codegen.py
2 # Copyright 2006-2022 the Mako authors and contributors <see AUTHORS file>
3 #
4 # This module is part of Mako and is released under
5 # the MIT License: http://www.opensource.org/licenses/mit-license.php
6
7 """provides functionality for rendering a parsetree constructing into module
8 source code."""
9
10 import json
11 import re
12 import time
13
14 from mako import ast
15 from mako import exceptions
16 from mako import filters
17 from mako import parsetree
18 from mako import util
19 from mako.pygen import PythonPrinter
20
21
22 MAGIC_NUMBER = 10
23
24 # names which are hardwired into the
25 # template and are not accessed via the
26 # context itself
27 TOPLEVEL_DECLARED = {"UNDEFINED", "STOP_RENDERING"}
28 RESERVED_NAMES = {"context", "loop"}.union(TOPLEVEL_DECLARED)
29
30
31 def compile( # noqa
32 node,
33 uri,
34 filename=None,
35 default_filters=None,
36 buffer_filters=None,
37 imports=None,
38 future_imports=None,
39 source_encoding=None,
40 generate_magic_comment=True,
41 strict_undefined=False,
42 enable_loop=True,
43 reserved_names=frozenset(),
44 ):
45 """Generate module source code given a parsetree node,
46 uri, and optional source filename"""
47
48 buf = util.FastEncodingBuffer()
49
50 printer = PythonPrinter(buf)
51 _GenerateRenderMethod(
52 printer,
53 _CompileContext(
54 uri,
55 filename,
56 default_filters,
57 buffer_filters,
58 imports,
59 future_imports,
60 source_encoding,
61 generate_magic_comment,
62 strict_undefined,
63 enable_loop,
64 reserved_names,
65 ),
66 node,
67 )
68 return buf.getvalue()
69
70
71 class _CompileContext:
72 def __init__(
73 self,
74 uri,
75 filename,
76 default_filters,
77 buffer_filters,
78 imports,
79 future_imports,
80 source_encoding,
81 generate_magic_comment,
82 strict_undefined,
83 enable_loop,
84 reserved_names,
85 ):
86 self.uri = uri
87 self.filename = filename
88 self.default_filters = default_filters
89 self.buffer_filters = buffer_filters
90 self.imports = imports
91 self.future_imports = future_imports
92 self.source_encoding = source_encoding
93 self.generate_magic_comment = generate_magic_comment
94 self.strict_undefined = strict_undefined
95 self.enable_loop = enable_loop
96 self.reserved_names = reserved_names
97
98
99 class _GenerateRenderMethod:
100
101 """A template visitor object which generates the
102 full module source for a template.
103
104 """
105
106 def __init__(self, printer, compiler, node):
107 self.printer = printer
108 self.compiler = compiler
109 self.node = node
110 self.identifier_stack = [None]
111 self.in_def = isinstance(node, (parsetree.DefTag, parsetree.BlockTag))
112
113 if self.in_def:
114 name = "render_%s" % node.funcname
115 args = node.get_argument_expressions()
116 filtered = len(node.filter_args.args) > 0
117 buffered = eval(node.attributes.get("buffered", "False"))
118 cached = eval(node.attributes.get("cached", "False"))
119 defs = None
120 pagetag = None
121 if node.is_block and not node.is_anonymous:
122 args += ["**pageargs"]
123 else:
124 defs = self.write_toplevel()
125 pagetag = self.compiler.pagetag
126 name = "render_body"
127 if pagetag is not None:
128 args = pagetag.body_decl.get_argument_expressions()
129 if not pagetag.body_decl.kwargs:
130 args += ["**pageargs"]
131 cached = eval(pagetag.attributes.get("cached", "False"))
132 self.compiler.enable_loop = self.compiler.enable_loop or eval(
133 pagetag.attributes.get("enable_loop", "False")
134 )
135 else:
136 args = ["**pageargs"]
137 cached = False
138 buffered = filtered = False
139 if args is None:
140 args = ["context"]
141 else:
142 args = [a for a in ["context"] + args]
143
144 self.write_render_callable(
145 pagetag or node, name, args, buffered, filtered, cached
146 )
147
148 if defs is not None:
149 for node in defs:
150 _GenerateRenderMethod(printer, compiler, node)
151
152 if not self.in_def:
153 self.write_metadata_struct()
154
155 def write_metadata_struct(self):
156 self.printer.source_map[self.printer.lineno] = max(
157 self.printer.source_map
158 )
159 struct = {
160 "filename": self.compiler.filename,
161 "uri": self.compiler.uri,
162 "source_encoding": self.compiler.source_encoding,
163 "line_map": self.printer.source_map,
164 }
165 self.printer.writelines(
166 '"""',
167 "__M_BEGIN_METADATA",
168 json.dumps(struct),
169 "__M_END_METADATA\n" '"""',
170 )
171
172 @property
173 def identifiers(self):
174 return self.identifier_stack[-1]
175
176 def write_toplevel(self):
177 """Traverse a template structure for module-level directives and
178 generate the start of module-level code.
179
180 """
181 inherit = []
182 namespaces = {}
183 module_code = []
184
185 self.compiler.pagetag = None
186
187 class FindTopLevel:
188 def visitInheritTag(s, node):
189 inherit.append(node)
190
191 def visitNamespaceTag(s, node):
192 namespaces[node.name] = node
193
194 def visitPageTag(s, node):
195 self.compiler.pagetag = node
196
197 def visitCode(s, node):
198 if node.ismodule:
199 module_code.append(node)
200
201 f = FindTopLevel()
202 for n in self.node.nodes:
203 n.accept_visitor(f)
204
205 self.compiler.namespaces = namespaces
206
207 module_ident = set()
208 for n in module_code:
209 module_ident = module_ident.union(n.declared_identifiers())
210
211 module_identifiers = _Identifiers(self.compiler)
212 module_identifiers.declared = module_ident
213
214 # module-level names, python code
215 if (
216 self.compiler.generate_magic_comment
217 and self.compiler.source_encoding
218 ):
219 self.printer.writeline(
220 "# -*- coding:%s -*-" % self.compiler.source_encoding
221 )
222
223 if self.compiler.future_imports:
224 self.printer.writeline(
225 "from __future__ import %s"
226 % (", ".join(self.compiler.future_imports),)
227 )
228 self.printer.writeline("from mako import runtime, filters, cache")
229 self.printer.writeline("UNDEFINED = runtime.UNDEFINED")
230 self.printer.writeline("STOP_RENDERING = runtime.STOP_RENDERING")
231 self.printer.writeline("__M_dict_builtin = dict")
232 self.printer.writeline("__M_locals_builtin = locals")
233 self.printer.writeline("_magic_number = %r" % MAGIC_NUMBER)
234 self.printer.writeline("_modified_time = %r" % time.time())
235 self.printer.writeline("_enable_loop = %r" % self.compiler.enable_loop)
236 self.printer.writeline(
237 "_template_filename = %r" % self.compiler.filename
238 )
239 self.printer.writeline("_template_uri = %r" % self.compiler.uri)
240 self.printer.writeline(
241 "_source_encoding = %r" % self.compiler.source_encoding
242 )
243 if self.compiler.imports:
244 buf = ""
245 for imp in self.compiler.imports:
246 buf += imp + "\n"
247 self.printer.writeline(imp)
248 impcode = ast.PythonCode(
249 buf,
250 source="",
251 lineno=0,
252 pos=0,
253 filename="template defined imports",
254 )
255 else:
256 impcode = None
257
258 main_identifiers = module_identifiers.branch(self.node)
259 mit = module_identifiers.topleveldefs
260 module_identifiers.topleveldefs = mit.union(
261 main_identifiers.topleveldefs
262 )
263 module_identifiers.declared.update(TOPLEVEL_DECLARED)
264 if impcode:
265 module_identifiers.declared.update(impcode.declared_identifiers)
266
267 self.compiler.identifiers = module_identifiers
268 self.printer.writeline(
269 "_exports = %r"
270 % [n.name for n in main_identifiers.topleveldefs.values()]
271 )
272 self.printer.write_blanks(2)
273
274 if len(module_code):
275 self.write_module_code(module_code)
276
277 if len(inherit):
278 self.write_namespaces(namespaces)
279 self.write_inherit(inherit[-1])
280 elif len(namespaces):
281 self.write_namespaces(namespaces)
282
283 return list(main_identifiers.topleveldefs.values())
284
285 def write_render_callable(
286 self, node, name, args, buffered, filtered, cached
287 ):
288 """write a top-level render callable.
289
290 this could be the main render() method or that of a top-level def."""
291
292 if self.in_def:
293 decorator = node.decorator
294 if decorator:
295 self.printer.writeline(
296 "@runtime._decorate_toplevel(%s)" % decorator
297 )
298
299 self.printer.start_source(node.lineno)
300 self.printer.writelines(
301 "def %s(%s):" % (name, ",".join(args)),
302 # push new frame, assign current frame to __M_caller
303 "__M_caller = context.caller_stack._push_frame()",
304 "try:",
305 )
306 if buffered or filtered or cached:
307 self.printer.writeline("context._push_buffer()")
308
309 self.identifier_stack.append(
310 self.compiler.identifiers.branch(self.node)
311 )
312 if (not self.in_def or self.node.is_block) and "**pageargs" in args:
313 self.identifier_stack[-1].argument_declared.add("pageargs")
314
315 if not self.in_def and (
316 len(self.identifiers.locally_assigned) > 0
317 or len(self.identifiers.argument_declared) > 0
318 ):
319 self.printer.writeline(
320 "__M_locals = __M_dict_builtin(%s)"
321 % ",".join(
322 [
323 "%s=%s" % (x, x)
324 for x in self.identifiers.argument_declared
325 ]
326 )
327 )
328
329 self.write_variable_declares(self.identifiers, toplevel=True)
330
331 for n in self.node.nodes:
332 n.accept_visitor(self)
333
334 self.write_def_finish(self.node, buffered, filtered, cached)
335 self.printer.writeline(None)
336 self.printer.write_blanks(2)
337 if cached:
338 self.write_cache_decorator(
339 node, name, args, buffered, self.identifiers, toplevel=True
340 )
341
342 def write_module_code(self, module_code):
343 """write module-level template code, i.e. that which
344 is enclosed in <%! %> tags in the template."""
345 for n in module_code:
346 self.printer.write_indented_block(n.text, starting_lineno=n.lineno)
347
348 def write_inherit(self, node):
349 """write the module-level inheritance-determination callable."""
350
351 self.printer.writelines(
352 "def _mako_inherit(template, context):",
353 "_mako_generate_namespaces(context)",
354 "return runtime._inherit_from(context, %s, _template_uri)"
355 % (node.parsed_attributes["file"]),
356 None,
357 )
358
359 def write_namespaces(self, namespaces):
360 """write the module-level namespace-generating callable."""
361 self.printer.writelines(
362 "def _mako_get_namespace(context, name):",
363 "try:",
364 "return context.namespaces[(__name__, name)]",
365 "except KeyError:",
366 "_mako_generate_namespaces(context)",
367 "return context.namespaces[(__name__, name)]",
368 None,
369 None,
370 )
371 self.printer.writeline("def _mako_generate_namespaces(context):")
372
373 for node in namespaces.values():
374 if "import" in node.attributes:
375 self.compiler.has_ns_imports = True
376 self.printer.start_source(node.lineno)
377 if len(node.nodes):
378 self.printer.writeline("def make_namespace():")
379 export = []
380 identifiers = self.compiler.identifiers.branch(node)
381 self.in_def = True
382
383 class NSDefVisitor:
384 def visitDefTag(s, node):
385 s.visitDefOrBase(node)
386
387 def visitBlockTag(s, node):
388 s.visitDefOrBase(node)
389
390 def visitDefOrBase(s, node):
391 if node.is_anonymous:
392 raise exceptions.CompileException(
393 "Can't put anonymous blocks inside "
394 "<%namespace>",
395 **node.exception_kwargs,
396 )
397 self.write_inline_def(node, identifiers, nested=False)
398 export.append(node.funcname)
399
400 vis = NSDefVisitor()
401 for n in node.nodes:
402 n.accept_visitor(vis)
403 self.printer.writeline("return [%s]" % (",".join(export)))
404 self.printer.writeline(None)
405 self.in_def = False
406 callable_name = "make_namespace()"
407 else:
408 callable_name = "None"
409
410 if "file" in node.parsed_attributes:
411 self.printer.writeline(
412 "ns = runtime.TemplateNamespace(%r,"
413 " context._clean_inheritance_tokens(),"
414 " templateuri=%s, callables=%s, "
415 " calling_uri=_template_uri)"
416 % (
417 node.name,
418 node.parsed_attributes.get("file", "None"),
419 callable_name,
420 )
421 )
422 elif "module" in node.parsed_attributes:
423 self.printer.writeline(
424 "ns = runtime.ModuleNamespace(%r,"
425 " context._clean_inheritance_tokens(),"
426 " callables=%s, calling_uri=_template_uri,"
427 " module=%s)"
428 % (
429 node.name,
430 callable_name,
431 node.parsed_attributes.get("module", "None"),
432 )
433 )
434 else:
435 self.printer.writeline(
436 "ns = runtime.Namespace(%r,"
437 " context._clean_inheritance_tokens(),"
438 " callables=%s, calling_uri=_template_uri)"
439 % (node.name, callable_name)
440 )
441 if eval(node.attributes.get("inheritable", "False")):
442 self.printer.writeline("context['self'].%s = ns" % (node.name))
443
444 self.printer.writeline(
445 "context.namespaces[(__name__, %s)] = ns" % repr(node.name)
446 )
447 self.printer.write_blanks(1)
448 if not len(namespaces):
449 self.printer.writeline("pass")
450 self.printer.writeline(None)
451
452 def write_variable_declares(self, identifiers, toplevel=False, limit=None):
453 """write variable declarations at the top of a function.
454
455 the variable declarations are in the form of callable
456 definitions for defs and/or name lookup within the
457 function's context argument. the names declared are based
458 on the names that are referenced in the function body,
459 which don't otherwise have any explicit assignment
460 operation. names that are assigned within the body are
461 assumed to be locally-scoped variables and are not
462 separately declared.
463
464 for def callable definitions, if the def is a top-level
465 callable then a 'stub' callable is generated which wraps
466 the current Context into a closure. if the def is not
467 top-level, it is fully rendered as a local closure.
468
469 """
470
471 # collection of all defs available to us in this scope
472 comp_idents = {c.funcname: c for c in identifiers.defs}
473 to_write = set()
474
475 # write "context.get()" for all variables we are going to
476 # need that arent in the namespace yet
477 to_write = to_write.union(identifiers.undeclared)
478
479 # write closure functions for closures that we define
480 # right here
481 to_write = to_write.union(
482 [c.funcname for c in identifiers.closuredefs.values()]
483 )
484
485 # remove identifiers that are declared in the argument
486 # signature of the callable
487 to_write = to_write.difference(identifiers.argument_declared)
488
489 # remove identifiers that we are going to assign to.
490 # in this way we mimic Python's behavior,
491 # i.e. assignment to a variable within a block
492 # means that variable is now a "locally declared" var,
493 # which cannot be referenced beforehand.
494 to_write = to_write.difference(identifiers.locally_declared)
495
496 if self.compiler.enable_loop:
497 has_loop = "loop" in to_write
498 to_write.discard("loop")
499 else:
500 has_loop = False
501
502 # if a limiting set was sent, constraint to those items in that list
503 # (this is used for the caching decorator)
504 if limit is not None:
505 to_write = to_write.intersection(limit)
506
507 if toplevel and getattr(self.compiler, "has_ns_imports", False):
508 self.printer.writeline("_import_ns = {}")
509 self.compiler.has_imports = True
510 for ident, ns in self.compiler.namespaces.items():
511 if "import" in ns.attributes:
512 self.printer.writeline(
513 "_mako_get_namespace(context, %r)."
514 "_populate(_import_ns, %r)"
515 % (
516 ident,
517 re.split(r"\s*,\s*", ns.attributes["import"]),
518 )
519 )
520
521 if has_loop:
522 self.printer.writeline("loop = __M_loop = runtime.LoopStack()")
523
524 for ident in to_write:
525 if ident in comp_idents:
526 comp = comp_idents[ident]
527 if comp.is_block:
528 if not comp.is_anonymous:
529 self.write_def_decl(comp, identifiers)
530 else:
531 self.write_inline_def(comp, identifiers, nested=True)
532 else:
533 if comp.is_root():
534 self.write_def_decl(comp, identifiers)
535 else:
536 self.write_inline_def(comp, identifiers, nested=True)
537
538 elif ident in self.compiler.namespaces:
539 self.printer.writeline(
540 "%s = _mako_get_namespace(context, %r)" % (ident, ident)
541 )
542 else:
543 if getattr(self.compiler, "has_ns_imports", False):
544 if self.compiler.strict_undefined:
545 self.printer.writelines(
546 "%s = _import_ns.get(%r, UNDEFINED)"
547 % (ident, ident),
548 "if %s is UNDEFINED:" % ident,
549 "try:",
550 "%s = context[%r]" % (ident, ident),
551 "except KeyError:",
552 "raise NameError(\"'%s' is not defined\")" % ident,
553 None,
554 None,
555 )
556 else:
557 self.printer.writeline(
558 "%s = _import_ns.get"
559 "(%r, context.get(%r, UNDEFINED))"
560 % (ident, ident, ident)
561 )
562 else:
563 if self.compiler.strict_undefined:
564 self.printer.writelines(
565 "try:",
566 "%s = context[%r]" % (ident, ident),
567 "except KeyError:",
568 "raise NameError(\"'%s' is not defined\")" % ident,
569 None,
570 )
571 else:
572 self.printer.writeline(
573 "%s = context.get(%r, UNDEFINED)" % (ident, ident)
574 )
575
576 self.printer.writeline("__M_writer = context.writer()")
577
578 def write_def_decl(self, node, identifiers):
579 """write a locally-available callable referencing a top-level def"""
580 funcname = node.funcname
581 namedecls = node.get_argument_expressions()
582 nameargs = node.get_argument_expressions(as_call=True)
583
584 if not self.in_def and (
585 len(self.identifiers.locally_assigned) > 0
586 or len(self.identifiers.argument_declared) > 0
587 ):
588 nameargs.insert(0, "context._locals(__M_locals)")
589 else:
590 nameargs.insert(0, "context")
591 self.printer.writeline("def %s(%s):" % (funcname, ",".join(namedecls)))
592 self.printer.writeline(
593 "return render_%s(%s)" % (funcname, ",".join(nameargs))
594 )
595 self.printer.writeline(None)
596
597 def write_inline_def(self, node, identifiers, nested):
598 """write a locally-available def callable inside an enclosing def."""
599
600 namedecls = node.get_argument_expressions()
601
602 decorator = node.decorator
603 if decorator:
604 self.printer.writeline(
605 "@runtime._decorate_inline(context, %s)" % decorator
606 )
607 self.printer.writeline(
608 "def %s(%s):" % (node.funcname, ",".join(namedecls))
609 )
610 filtered = len(node.filter_args.args) > 0
611 buffered = eval(node.attributes.get("buffered", "False"))
612 cached = eval(node.attributes.get("cached", "False"))
613 self.printer.writelines(
614 # push new frame, assign current frame to __M_caller
615 "__M_caller = context.caller_stack._push_frame()",
616 "try:",
617 )
618 if buffered or filtered or cached:
619 self.printer.writelines("context._push_buffer()")
620
621 identifiers = identifiers.branch(node, nested=nested)
622
623 self.write_variable_declares(identifiers)
624
625 self.identifier_stack.append(identifiers)
626 for n in node.nodes:
627 n.accept_visitor(self)
628 self.identifier_stack.pop()
629
630 self.write_def_finish(node, buffered, filtered, cached)
631 self.printer.writeline(None)
632 if cached:
633 self.write_cache_decorator(
634 node,
635 node.funcname,
636 namedecls,
637 False,
638 identifiers,
639 inline=True,
640 toplevel=False,
641 )
642
643 def write_def_finish(
644 self, node, buffered, filtered, cached, callstack=True
645 ):
646 """write the end section of a rendering function, either outermost or
647 inline.
648
649 this takes into account if the rendering function was filtered,
650 buffered, etc. and closes the corresponding try: block if any, and
651 writes code to retrieve captured content, apply filters, send proper
652 return value."""
653
654 if not buffered and not cached and not filtered:
655 self.printer.writeline("return ''")
656 if callstack:
657 self.printer.writelines(
658 "finally:", "context.caller_stack._pop_frame()", None
659 )
660
661 if buffered or filtered or cached:
662 if buffered or cached:
663 # in a caching scenario, don't try to get a writer
664 # from the context after popping; assume the caching
665 # implemenation might be using a context with no
666 # extra buffers
667 self.printer.writelines(
668 "finally:", "__M_buf = context._pop_buffer()"
669 )
670 else:
671 self.printer.writelines(
672 "finally:",
673 "__M_buf, __M_writer = context._pop_buffer_and_writer()",
674 )
675
676 if callstack:
677 self.printer.writeline("context.caller_stack._pop_frame()")
678
679 s = "__M_buf.getvalue()"
680 if filtered:
681 s = self.create_filter_callable(
682 node.filter_args.args, s, False
683 )
684 self.printer.writeline(None)
685 if buffered and not cached:
686 s = self.create_filter_callable(
687 self.compiler.buffer_filters, s, False
688 )
689 if buffered or cached:
690 self.printer.writeline("return %s" % s)
691 else:
692 self.printer.writelines("__M_writer(%s)" % s, "return ''")
693
694 def write_cache_decorator(
695 self,
696 node_or_pagetag,
697 name,
698 args,
699 buffered,
700 identifiers,
701 inline=False,
702 toplevel=False,
703 ):
704 """write a post-function decorator to replace a rendering
705 callable with a cached version of itself."""
706
707 self.printer.writeline("__M_%s = %s" % (name, name))
708 cachekey = node_or_pagetag.parsed_attributes.get(
709 "cache_key", repr(name)
710 )
711
712 cache_args = {}
713 if self.compiler.pagetag is not None:
714 cache_args.update(
715 (pa[6:], self.compiler.pagetag.parsed_attributes[pa])
716 for pa in self.compiler.pagetag.parsed_attributes
717 if pa.startswith("cache_") and pa != "cache_key"
718 )
719 cache_args.update(
720 (pa[6:], node_or_pagetag.parsed_attributes[pa])
721 for pa in node_or_pagetag.parsed_attributes
722 if pa.startswith("cache_") and pa != "cache_key"
723 )
724 if "timeout" in cache_args:
725 cache_args["timeout"] = int(eval(cache_args["timeout"]))
726
727 self.printer.writeline("def %s(%s):" % (name, ",".join(args)))
728
729 # form "arg1, arg2, arg3=arg3, arg4=arg4", etc.
730 pass_args = [
731 "%s=%s" % ((a.split("=")[0],) * 2) if "=" in a else a for a in args
732 ]
733
734 self.write_variable_declares(
735 identifiers,
736 toplevel=toplevel,
737 limit=node_or_pagetag.undeclared_identifiers(),
738 )
739 if buffered:
740 s = (
741 "context.get('local')."
742 "cache._ctx_get_or_create("
743 "%s, lambda:__M_%s(%s), context, %s__M_defname=%r)"
744 % (
745 cachekey,
746 name,
747 ",".join(pass_args),
748 "".join(
749 ["%s=%s, " % (k, v) for k, v in cache_args.items()]
750 ),
751 name,
752 )
753 )
754 # apply buffer_filters
755 s = self.create_filter_callable(
756 self.compiler.buffer_filters, s, False
757 )
758 self.printer.writelines("return " + s, None)
759 else:
760 self.printer.writelines(
761 "__M_writer(context.get('local')."
762 "cache._ctx_get_or_create("
763 "%s, lambda:__M_%s(%s), context, %s__M_defname=%r))"
764 % (
765 cachekey,
766 name,
767 ",".join(pass_args),
768 "".join(
769 ["%s=%s, " % (k, v) for k, v in cache_args.items()]
770 ),
771 name,
772 ),
773 "return ''",
774 None,
775 )
776
777 def create_filter_callable(self, args, target, is_expression):
778 """write a filter-applying expression based on the filters
779 present in the given filter names, adjusting for the global
780 'default' filter aliases as needed."""
781
782 def locate_encode(name):
783 if re.match(r"decode\..+", name):
784 return "filters." + name
785 else:
786 return filters.DEFAULT_ESCAPES.get(name, name)
787
788 if "n" not in args:
789 if is_expression:
790 if self.compiler.pagetag:
791 args = self.compiler.pagetag.filter_args.args + args
792 if self.compiler.default_filters and "n" not in args:
793 args = self.compiler.default_filters + args
794 for e in args:
795 # if filter given as a function, get just the identifier portion
796 if e == "n":
797 continue
798 m = re.match(r"(.+?)(\(.*\))", e)
799 if m:
800 ident, fargs = m.group(1, 2)
801 f = locate_encode(ident)
802 e = f + fargs
803 else:
804 e = locate_encode(e)
805 assert e is not None
806 target = "%s(%s)" % (e, target)
807 return target
808
809 def visitExpression(self, node):
810 self.printer.start_source(node.lineno)
811 if (
812 len(node.escapes)
813 or (
814 self.compiler.pagetag is not None
815 and len(self.compiler.pagetag.filter_args.args)
816 )
817 or len(self.compiler.default_filters)
818 ):
819
820 s = self.create_filter_callable(
821 node.escapes_code.args, "%s" % node.text, True
822 )
823 self.printer.writeline("__M_writer(%s)" % s)
824 else:
825 self.printer.writeline("__M_writer(%s)" % node.text)
826
827 def visitControlLine(self, node):
828 if node.isend:
829 self.printer.writeline(None)
830 if node.has_loop_context:
831 self.printer.writeline("finally:")
832 self.printer.writeline("loop = __M_loop._exit()")
833 self.printer.writeline(None)
834 else:
835 self.printer.start_source(node.lineno)
836 if self.compiler.enable_loop and node.keyword == "for":
837 text = mangle_mako_loop(node, self.printer)
838 else:
839 text = node.text
840 self.printer.writeline(text)
841 children = node.get_children()
842 # this covers the three situations where we want to insert a pass:
843 # 1) a ternary control line with no children,
844 # 2) a primary control line with nothing but its own ternary
845 # and end control lines, and
846 # 3) any control line with no content other than comments
847 if not children or (
848 all(
849 isinstance(c, (parsetree.Comment, parsetree.ControlLine))
850 for c in children
851 )
852 and all(
853 (node.is_ternary(c.keyword) or c.isend)
854 for c in children
855 if isinstance(c, parsetree.ControlLine)
856 )
857 ):
858 self.printer.writeline("pass")
859
860 def visitText(self, node):
861 self.printer.start_source(node.lineno)
862 self.printer.writeline("__M_writer(%s)" % repr(node.content))
863
864 def visitTextTag(self, node):
865 filtered = len(node.filter_args.args) > 0
866 if filtered:
867 self.printer.writelines(
868 "__M_writer = context._push_writer()", "try:"
869 )
870 for n in node.nodes:
871 n.accept_visitor(self)
872 if filtered:
873 self.printer.writelines(
874 "finally:",
875 "__M_buf, __M_writer = context._pop_buffer_and_writer()",
876 "__M_writer(%s)"
877 % self.create_filter_callable(
878 node.filter_args.args, "__M_buf.getvalue()", False
879 ),
880 None,
881 )
882
883 def visitCode(self, node):
884 if not node.ismodule:
885 self.printer.write_indented_block(
886 node.text, starting_lineno=node.lineno
887 )
888
889 if not self.in_def and len(self.identifiers.locally_assigned) > 0:
890 # if we are the "template" def, fudge locally
891 # declared/modified variables into the "__M_locals" dictionary,
892 # which is used for def calls within the same template,
893 # to simulate "enclosing scope"
894 self.printer.writeline(
895 "__M_locals_builtin_stored = __M_locals_builtin()"
896 )
897 self.printer.writeline(
898 "__M_locals.update(__M_dict_builtin([(__M_key,"
899 " __M_locals_builtin_stored[__M_key]) for __M_key in"
900 " [%s] if __M_key in __M_locals_builtin_stored]))"
901 % ",".join([repr(x) for x in node.declared_identifiers()])
902 )
903
904 def visitIncludeTag(self, node):
905 self.printer.start_source(node.lineno)
906 args = node.attributes.get("args")
907 if args:
908 self.printer.writeline(
909 "runtime._include_file(context, %s, _template_uri, %s)"
910 % (node.parsed_attributes["file"], args)
911 )
912 else:
913 self.printer.writeline(
914 "runtime._include_file(context, %s, _template_uri)"
915 % (node.parsed_attributes["file"])
916 )
917
918 def visitNamespaceTag(self, node):
919 pass
920
921 def visitDefTag(self, node):
922 pass
923
924 def visitBlockTag(self, node):
925 if node.is_anonymous:
926 self.printer.writeline("%s()" % node.funcname)
927 else:
928 nameargs = node.get_argument_expressions(as_call=True)
929 nameargs += ["**pageargs"]
930 self.printer.writeline(
931 "if 'parent' not in context._data or "
932 "not hasattr(context._data['parent'], '%s'):" % node.funcname
933 )
934 self.printer.writeline(
935 "context['self'].%s(%s)" % (node.funcname, ",".join(nameargs))
936 )
937 self.printer.writeline("\n")
938
939 def visitCallNamespaceTag(self, node):
940 # TODO: we can put namespace-specific checks here, such
941 # as ensure the given namespace will be imported,
942 # pre-import the namespace, etc.
943 self.visitCallTag(node)
944
945 def visitCallTag(self, node):
946 self.printer.writeline("def ccall(caller):")
947 export = ["body"]
948 callable_identifiers = self.identifiers.branch(node, nested=True)
949 body_identifiers = callable_identifiers.branch(node, nested=False)
950 # we want the 'caller' passed to ccall to be used
951 # for the body() function, but for other non-body()
952 # <%def>s within <%call> we want the current caller
953 # off the call stack (if any)
954 body_identifiers.add_declared("caller")
955
956 self.identifier_stack.append(body_identifiers)
957
958 class DefVisitor:
959 def visitDefTag(s, node):
960 s.visitDefOrBase(node)
961
962 def visitBlockTag(s, node):
963 s.visitDefOrBase(node)
964
965 def visitDefOrBase(s, node):
966 self.write_inline_def(node, callable_identifiers, nested=False)
967 if not node.is_anonymous:
968 export.append(node.funcname)
969 # remove defs that are within the <%call> from the
970 # "closuredefs" defined in the body, so they dont render twice
971 if node.funcname in body_identifiers.closuredefs:
972 del body_identifiers.closuredefs[node.funcname]
973
974 vis = DefVisitor()
975 for n in node.nodes:
976 n.accept_visitor(vis)
977 self.identifier_stack.pop()
978
979 bodyargs = node.body_decl.get_argument_expressions()
980 self.printer.writeline("def body(%s):" % ",".join(bodyargs))
981
982 # TODO: figure out best way to specify
983 # buffering/nonbuffering (at call time would be better)
984 buffered = False
985 if buffered:
986 self.printer.writelines("context._push_buffer()", "try:")
987 self.write_variable_declares(body_identifiers)
988 self.identifier_stack.append(body_identifiers)
989
990 for n in node.nodes:
991 n.accept_visitor(self)
992 self.identifier_stack.pop()
993
994 self.write_def_finish(node, buffered, False, False, callstack=False)
995 self.printer.writelines(None, "return [%s]" % (",".join(export)), None)
996
997 self.printer.writelines(
998 # push on caller for nested call
999 "context.caller_stack.nextcaller = "
1000 "runtime.Namespace('caller', context, "
1001 "callables=ccall(__M_caller))",
1002 "try:",
1003 )
1004 self.printer.start_source(node.lineno)
1005 self.printer.writelines(
1006 "__M_writer(%s)"
1007 % self.create_filter_callable([], node.expression, True),
1008 "finally:",
1009 "context.caller_stack.nextcaller = None",
1010 None,
1011 )
1012
1013
1014 class _Identifiers:
1015
1016 """tracks the status of identifier names as template code is rendered."""
1017
1018 def __init__(self, compiler, node=None, parent=None, nested=False):
1019 if parent is not None:
1020 # if we are the branch created in write_namespaces(),
1021 # we don't share any context from the main body().
1022 if isinstance(node, parsetree.NamespaceTag):
1023 self.declared = set()
1024 self.topleveldefs = util.SetLikeDict()
1025 else:
1026 # things that have already been declared
1027 # in an enclosing namespace (i.e. names we can just use)
1028 self.declared = (
1029 set(parent.declared)
1030 .union([c.name for c in parent.closuredefs.values()])
1031 .union(parent.locally_declared)
1032 .union(parent.argument_declared)
1033 )
1034
1035 # if these identifiers correspond to a "nested"
1036 # scope, it means whatever the parent identifiers
1037 # had as undeclared will have been declared by that parent,
1038 # and therefore we have them in our scope.
1039 if nested:
1040 self.declared = self.declared.union(parent.undeclared)
1041
1042 # top level defs that are available
1043 self.topleveldefs = util.SetLikeDict(**parent.topleveldefs)
1044 else:
1045 self.declared = set()
1046 self.topleveldefs = util.SetLikeDict()
1047
1048 self.compiler = compiler
1049
1050 # things within this level that are referenced before they
1051 # are declared (e.g. assigned to)
1052 self.undeclared = set()
1053
1054 # things that are declared locally. some of these things
1055 # could be in the "undeclared" list as well if they are
1056 # referenced before declared
1057 self.locally_declared = set()
1058
1059 # assignments made in explicit python blocks.
1060 # these will be propagated to
1061 # the context of local def calls.
1062 self.locally_assigned = set()
1063
1064 # things that are declared in the argument
1065 # signature of the def callable
1066 self.argument_declared = set()
1067
1068 # closure defs that are defined in this level
1069 self.closuredefs = util.SetLikeDict()
1070
1071 self.node = node
1072
1073 if node is not None:
1074 node.accept_visitor(self)
1075
1076 illegal_names = self.compiler.reserved_names.intersection(
1077 self.locally_declared
1078 )
1079 if illegal_names:
1080 raise exceptions.NameConflictError(
1081 "Reserved words declared in template: %s"
1082 % ", ".join(illegal_names)
1083 )
1084
1085 def branch(self, node, **kwargs):
1086 """create a new Identifiers for a new Node, with
1087 this Identifiers as the parent."""
1088
1089 return _Identifiers(self.compiler, node, self, **kwargs)
1090
1091 @property
1092 def defs(self):
1093 return set(self.topleveldefs.union(self.closuredefs).values())
1094
1095 def __repr__(self):
1096 return (
1097 "Identifiers(declared=%r, locally_declared=%r, "
1098 "undeclared=%r, topleveldefs=%r, closuredefs=%r, "
1099 "argumentdeclared=%r)"
1100 % (
1101 list(self.declared),
1102 list(self.locally_declared),
1103 list(self.undeclared),
1104 [c.name for c in self.topleveldefs.values()],
1105 [c.name for c in self.closuredefs.values()],
1106 self.argument_declared,
1107 )
1108 )
1109
1110 def check_declared(self, node):
1111 """update the state of this Identifiers with the undeclared
1112 and declared identifiers of the given node."""
1113
1114 for ident in node.undeclared_identifiers():
1115 if ident != "context" and ident not in self.declared.union(
1116 self.locally_declared
1117 ):
1118 self.undeclared.add(ident)
1119 for ident in node.declared_identifiers():
1120 self.locally_declared.add(ident)
1121
1122 def add_declared(self, ident):
1123 self.declared.add(ident)
1124 if ident in self.undeclared:
1125 self.undeclared.remove(ident)
1126
1127 def visitExpression(self, node):
1128 self.check_declared(node)
1129
1130 def visitControlLine(self, node):
1131 self.check_declared(node)
1132
1133 def visitCode(self, node):
1134 if not node.ismodule:
1135 self.check_declared(node)
1136 self.locally_assigned = self.locally_assigned.union(
1137 node.declared_identifiers()
1138 )
1139
1140 def visitNamespaceTag(self, node):
1141 # only traverse into the sub-elements of a
1142 # <%namespace> tag if we are the branch created in
1143 # write_namespaces()
1144 if self.node is node:
1145 for n in node.nodes:
1146 n.accept_visitor(self)
1147
1148 def _check_name_exists(self, collection, node):
1149 existing = collection.get(node.funcname)
1150 collection[node.funcname] = node
1151 if (
1152 existing is not None
1153 and existing is not node
1154 and (node.is_block or existing.is_block)
1155 ):
1156 raise exceptions.CompileException(
1157 "%%def or %%block named '%s' already "
1158 "exists in this template." % node.funcname,
1159 **node.exception_kwargs,
1160 )
1161
1162 def visitDefTag(self, node):
1163 if node.is_root() and not node.is_anonymous:
1164 self._check_name_exists(self.topleveldefs, node)
1165 elif node is not self.node:
1166 self._check_name_exists(self.closuredefs, node)
1167
1168 for ident in node.undeclared_identifiers():
1169 if ident != "context" and ident not in self.declared.union(
1170 self.locally_declared
1171 ):
1172 self.undeclared.add(ident)
1173
1174 # visit defs only one level deep
1175 if node is self.node:
1176 for ident in node.declared_identifiers():
1177 self.argument_declared.add(ident)
1178
1179 for n in node.nodes:
1180 n.accept_visitor(self)
1181
1182 def visitBlockTag(self, node):
1183 if node is not self.node and not node.is_anonymous:
1184
1185 if isinstance(self.node, parsetree.DefTag):
1186 raise exceptions.CompileException(
1187 "Named block '%s' not allowed inside of def '%s'"
1188 % (node.name, self.node.name),
1189 **node.exception_kwargs,
1190 )
1191 elif isinstance(
1192 self.node, (parsetree.CallTag, parsetree.CallNamespaceTag)
1193 ):
1194 raise exceptions.CompileException(
1195 "Named block '%s' not allowed inside of <%%call> tag"
1196 % (node.name,),
1197 **node.exception_kwargs,
1198 )
1199
1200 for ident in node.undeclared_identifiers():
1201 if ident != "context" and ident not in self.declared.union(
1202 self.locally_declared
1203 ):
1204 self.undeclared.add(ident)
1205
1206 if not node.is_anonymous:
1207 self._check_name_exists(self.topleveldefs, node)
1208 self.undeclared.add(node.funcname)
1209 elif node is not self.node:
1210 self._check_name_exists(self.closuredefs, node)
1211 for ident in node.declared_identifiers():
1212 self.argument_declared.add(ident)
1213 for n in node.nodes:
1214 n.accept_visitor(self)
1215
1216 def visitTextTag(self, node):
1217 for ident in node.undeclared_identifiers():
1218 if ident != "context" and ident not in self.declared.union(
1219 self.locally_declared
1220 ):
1221 self.undeclared.add(ident)
1222
1223 def visitIncludeTag(self, node):
1224 self.check_declared(node)
1225
1226 def visitPageTag(self, node):
1227 for ident in node.declared_identifiers():
1228 self.argument_declared.add(ident)
1229 self.check_declared(node)
1230
1231 def visitCallNamespaceTag(self, node):
1232 self.visitCallTag(node)
1233
1234 def visitCallTag(self, node):
1235 if node is self.node:
1236 for ident in node.undeclared_identifiers():
1237 if ident != "context" and ident not in self.declared.union(
1238 self.locally_declared
1239 ):
1240 self.undeclared.add(ident)
1241 for ident in node.declared_identifiers():
1242 self.argument_declared.add(ident)
1243 for n in node.nodes:
1244 n.accept_visitor(self)
1245 else:
1246 for ident in node.undeclared_identifiers():
1247 if ident != "context" and ident not in self.declared.union(
1248 self.locally_declared
1249 ):
1250 self.undeclared.add(ident)
1251
1252
1253 _FOR_LOOP = re.compile(
1254 r"^for\s+((?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
1255 r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z0-9_]*),??)*\s*(?:\)?))\s+in\s+(.*):"
1256 )
1257
1258
1259 def mangle_mako_loop(node, printer):
1260 """converts a for loop into a context manager wrapped around a for loop
1261 when access to the `loop` variable has been detected in the for loop body
1262 """
1263 loop_variable = LoopVariable()
1264 node.accept_visitor(loop_variable)
1265 if loop_variable.detected:
1266 node.nodes[-1].has_loop_context = True
1267 match = _FOR_LOOP.match(node.text)
1268 if match:
1269 printer.writelines(
1270 "loop = __M_loop._enter(%s)" % match.group(2),
1271 "try:"
1272 # 'with __M_loop(%s) as loop:' % match.group(2)
1273 )
1274 text = "for %s in loop:" % match.group(1)
1275 else:
1276 raise SyntaxError("Couldn't apply loop context: %s" % node.text)
1277 else:
1278 text = node.text
1279 return text
1280
1281
1282 class LoopVariable:
1283
1284 """A node visitor which looks for the name 'loop' within undeclared
1285 identifiers."""
1286
1287 def __init__(self):
1288 self.detected = False
1289
1290 def _loop_reference_detected(self, node):
1291 if "loop" in node.undeclared_identifiers():
1292 self.detected = True
1293 else:
1294 for n in node.get_children():
1295 n.accept_visitor(self)
1296
1297 def visitControlLine(self, node):
1298 self._loop_reference_detected(node)
1299
1300 def visitCode(self, node):
1301 self._loop_reference_detected(node)
1302
1303 def visitExpression(self, node):
1304 self._loop_reference_detected(node)