Skip to content
Myr

meta.py

This file

Path

src/jinja2/meta.py

112 lines · 4.3 KiB · Python

Last changed by

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

History of this file

Take it

1"""Functions that expose information about templates that might be2interesting for introspection.3"""45import typing as t67from . import nodes8from .compiler import CodeGenerator9from .compiler import Frame1011if t.TYPE_CHECKING:12    from .environment import Environment131415class TrackingCodeGenerator(CodeGenerator):16    """We abuse the code generator for introspection."""1718    def __init__(self, environment: "Environment") -> None:19        super().__init__(environment, "<introspection>", "<introspection>")20        self.undeclared_identifiers: set[str] = set()2122    def write(self, x: str) -> None:23        """Don't write."""2425    def enter_frame(self, frame: Frame) -> None:26        """Remember all undeclared identifiers."""27        super().enter_frame(frame)2829        for _, (action, param) in frame.symbols.loads.items():30            if action == "resolve" and param not in self.environment.globals:31                self.undeclared_identifiers.add(param)323334def find_undeclared_variables(ast: nodes.Template) -> set[str]:35    """Returns a set of all variables in the AST that will be looked up from36    the context at runtime.  Because at compile time it's not known which37    variables will be used depending on the path the execution takes at38    runtime, all variables are returned.3940    >>> from jinja2 import Environment, meta41    >>> env = Environment()42    >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')43    >>> meta.find_undeclared_variables(ast) == {'bar'}44    True4546    .. admonition:: Implementation4748       Internally the code generator is used for finding undeclared variables.49       This is good to know because the code generator might raise a50       :exc:`TemplateAssertionError` during compilation and as a matter of51       fact this function can currently raise that exception as well.52    """53    codegen = TrackingCodeGenerator(ast.environment)  # type: ignore54    codegen.visit(ast)55    return codegen.undeclared_identifiers565758_ref_types = (nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include)59_RefType = nodes.Extends | nodes.FromImport | nodes.Import | nodes.Include606162def find_referenced_templates(ast: nodes.Template) -> t.Iterator[str | None]:63    """Finds all the referenced templates from the AST.  This will return an64    iterator over all the hardcoded template extensions, inclusions and65    imports.  If dynamic inheritance or inclusion is used, `None` will be66    yielded.6768    >>> from jinja2 import Environment, meta69    >>> env = Environment()70    >>> ast = env.parse('{% extends "layout.html" %}{% include helper %}')71    >>> list(meta.find_referenced_templates(ast))72    ['layout.html', None]7374    This function is useful for dependency tracking.  For example if you want75    to rebuild parts of the website after a layout template has changed.76    """77    template_name: t.Any7879    for node in ast.find_all(_ref_types):80        template: nodes.Expr = node.template  # type: ignore8182        if not isinstance(template, nodes.Const):83            # a tuple with some non consts in there84            if isinstance(template, (nodes.Tuple, nodes.List)):85                for template_name in template.items:86                    # something const, only yield the strings and ignore87                    # non-string consts that really just make no sense88                    if isinstance(template_name, nodes.Const):89                        if isinstance(template_name.value, str):90                            yield template_name.value91                    # something dynamic in there92                    else:93                        yield None94            # something dynamic we don't know about here95            else:96                yield None97            continue98        # constant is a basestring, direct template name99        if isinstance(template.value, str):100            yield template.value101        # a tuple or list (latter *should* not happen) made of consts,102        # yield the consts that are strings.  We could warn here for103        # non string values104        elif isinstance(node, nodes.Include) and isinstance(105            template.value, (tuple, list)106        ):107            for template_name in template.value:108                if isinstance(template_name, str):109                    yield template_name110        # something else we don't care about, we could warn here111        else:112            yield None