MCPcopy
hub / github.com/pallets/jinja / pull_dependencies

Method pull_dependencies

src/jinja2/compiler.py:538–582  ·  view source on GitHub ↗

Find all filter and test names used in the template and assign them to variables in the compiled namespace. Checking that the names are registered with the environment is done when compiling the Filter and Test nodes. If the node is in an If or CondExpr node, the chec

(self, nodes: t.Iterable[nodes.Node])

Source from the content-addressed store, hash-verified

536 self.visit(node.dyn_kwargs, frame)
537
538 def pull_dependencies(self, nodes: t.Iterable[nodes.Node]) -> None:
539 """Find all filter and test names used in the template and
540 assign them to variables in the compiled namespace. Checking
541 that the names are registered with the environment is done when
542 compiling the Filter and Test nodes. If the node is in an If or
543 CondExpr node, the check is done at runtime instead.
544
545 .. versionchanged:: 3.0
546 Filters and tests in If and CondExpr nodes are checked at
547 runtime instead of compile time.
548 """
549 visitor = DependencyFinderVisitor()
550
551 for node in nodes:
552 visitor.visit(node)
553
554 for id_map, names, dependency in (
555 (self.filters, visitor.filters, "filters"),
556 (
557 self.tests,
558 visitor.tests,
559 "tests",
560 ),
561 ):
562 for name in sorted(names):
563 if name not in id_map:
564 id_map[name] = self.temporary_identifier()
565
566 # add check during runtime that dependencies used inside of executed
567 # blocks are defined, as this step may be skipped during compile time
568 self.writeline("try:")
569 self.indent()
570 self.writeline(f"{id_map[name]} = environment.{dependency}[{name!r}]")
571 self.outdent()
572 self.writeline("except KeyError:")
573 self.indent()
574 self.writeline("@internalcode")
575 self.writeline(f"def {id_map[name]}(*unused):")
576 self.indent()
577 self.writeline(
578 f'raise TemplateRuntimeError("No {dependency[:-1]}'
579 f' named {name!r} found.")'
580 )
581 self.outdent()
582 self.outdent()
583
584 def enter_frame(self, frame: Frame) -> None:
585 undefs = []

Callers 1

visit_TemplateMethod · 0.95

Calls 6

temporary_identifierMethod · 0.95
writelineMethod · 0.95
indentMethod · 0.95
outdentMethod · 0.95
visitMethod · 0.80

Tested by

no test coverage detected