Generate the python source for a node tree.
(
node: nodes.Template,
environment: "Environment",
name: t.Optional[str],
filename: t.Optional[str],
stream: t.Optional[t.TextIO] = None,
defer_init: bool = False,
optimized: bool = True,
)
| 99 | |
| 100 | |
| 101 | def generate( |
| 102 | node: nodes.Template, |
| 103 | environment: "Environment", |
| 104 | name: t.Optional[str], |
| 105 | filename: t.Optional[str], |
| 106 | stream: t.Optional[t.TextIO] = None, |
| 107 | defer_init: bool = False, |
| 108 | optimized: bool = True, |
| 109 | ) -> t.Optional[str]: |
| 110 | """Generate the python source for a node tree.""" |
| 111 | if not isinstance(node, nodes.Template): |
| 112 | raise TypeError("Can't compile non template nodes") |
| 113 | |
| 114 | generator = environment.code_generator_class( |
| 115 | environment, name, filename, stream, defer_init, optimized |
| 116 | ) |
| 117 | generator.visit(node) |
| 118 | |
| 119 | if stream is None: |
| 120 | return generator.stream.getvalue() # type: ignore |
| 121 | |
| 122 | return None |
| 123 | |
| 124 | |
| 125 | def has_safe_repr(value: t.Any) -> bool: |