jinja2.meta
Functions that expose information about templates that might be interesting for introspection.
1"""Functions that expose information about templates that might be 2interesting for introspection. 3""" 4import typing as t 5 6from . import nodes 7from .compiler import CodeGenerator 8from .compiler import Frame 9 10if t.TYPE_CHECKING: 11 from .environment import Environment 12 13 14class TrackingCodeGenerator(CodeGenerator): 15 """We abuse the code generator for introspection.""" 16 17 def __init__(self, environment: "Environment") -> None: 18 super().__init__(environment, "<introspection>", "<introspection>") 19 self.undeclared_identifiers: t.Set[str] = set() 20 21 def write(self, x: str) -> None: 22 """Don't write.""" 23 24 def enter_frame(self, frame: Frame) -> None: 25 """Remember all undeclared identifiers.""" 26 super().enter_frame(frame) 27 28 for _, (action, param) in frame.symbols.loads.items(): 29 if action == "resolve" and param not in self.environment.globals: 30 self.undeclared_identifiers.add(param) 31 32 33def find_undeclared_variables(ast: nodes.Template) -> t.Set[str]: 34 """Returns a set of all variables in the AST that will be looked up from 35 the context at runtime. Because at compile time it's not known which 36 variables will be used depending on the path the execution takes at 37 runtime, all variables are returned. 38 39 >>> from jinja2 import Environment, meta 40 >>> env = Environment() 41 >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}') 42 >>> meta.find_undeclared_variables(ast) == {'bar'} 43 True 44 45 .. admonition:: Implementation 46 47 Internally the code generator is used for finding undeclared variables. 48 This is good to know because the code generator might raise a 49 :exc:`TemplateAssertionError` during compilation and as a matter of 50 fact this function can currently raise that exception as well. 51 """ 52 codegen = TrackingCodeGenerator(ast.environment) # type: ignore 53 codegen.visit(ast) 54 return codegen.undeclared_identifiers 55 56 57_ref_types = (nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include) 58_RefType = t.Union[nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include] 59 60 61def find_referenced_templates(ast: nodes.Template) -> t.Iterator[t.Optional[str]]: 62 """Finds all the referenced templates from the AST. This will return an 63 iterator over all the hardcoded template extensions, inclusions and 64 imports. If dynamic inheritance or inclusion is used, `None` will be 65 yielded. 66 67 >>> from jinja2 import Environment, meta 68 >>> env = Environment() 69 >>> ast = env.parse('{% extends "layout.html" %}{% include helper %}') 70 >>> list(meta.find_referenced_templates(ast)) 71 ['layout.html', None] 72 73 This function is useful for dependency tracking. For example if you want 74 to rebuild parts of the website after a layout template has changed. 75 """ 76 template_name: t.Any 77 78 for node in ast.find_all(_ref_types): 79 template: nodes.Expr = node.template # type: ignore 80 81 if not isinstance(template, nodes.Const): 82 # a tuple with some non consts in there 83 if isinstance(template, (nodes.Tuple, nodes.List)): 84 for template_name in template.items: 85 # something const, only yield the strings and ignore 86 # non-string consts that really just make no sense 87 if isinstance(template_name, nodes.Const): 88 if isinstance(template_name.value, str): 89 yield template_name.value 90 # something dynamic in there 91 else: 92 yield None 93 # something dynamic we don't know about here 94 else: 95 yield None 96 continue 97 # constant is a basestring, direct template name 98 if isinstance(template.value, str): 99 yield template.value 100 # a tuple or list (latter *should* not happen) made of consts, 101 # yield the consts that are strings. We could warn here for 102 # non string values 103 elif isinstance(node, nodes.Include) and isinstance( 104 template.value, (tuple, list) 105 ): 106 for template_name in template.value: 107 if isinstance(template_name, str): 108 yield template_name 109 # something else we don't care about, we could warn here 110 else: 111 yield None
15class TrackingCodeGenerator(CodeGenerator): 16 """We abuse the code generator for introspection.""" 17 18 def __init__(self, environment: "Environment") -> None: 19 super().__init__(environment, "<introspection>", "<introspection>") 20 self.undeclared_identifiers: t.Set[str] = set() 21 22 def write(self, x: str) -> None: 23 """Don't write.""" 24 25 def enter_frame(self, frame: Frame) -> None: 26 """Remember all undeclared identifiers.""" 27 super().enter_frame(frame) 28 29 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)
We abuse the code generator for introspection.
25 def enter_frame(self, frame: Frame) -> None: 26 """Remember all undeclared identifiers.""" 27 super().enter_frame(frame) 28 29 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)
Remember all undeclared identifiers.
Inherited Members
- jinja2.compiler.CodeGenerator
- environment
- name
- filename
- stream
- created_block_context
- defer_init
- optimizer
- import_aliases
- blocks
- extends_so_far
- has_known_extends
- code_lineno
- tests
- filters
- debug_info
- optimized
- fail
- temporary_identifier
- buffer
- return_buffer_contents
- indent
- outdent
- start_write
- end_write
- simple_write
- blockvisit
- writeline
- newline
- signature
- pull_dependencies
- leave_frame
- choose_async
- func
- macro_body
- macro_def
- position
- dump_local_context
- write_commons
- push_parameter_definitions
- pop_parameter_definitions
- mark_parameter_stored
- push_context_reference
- pop_context_reference
- get_context_ref
- get_resolve_func
- derive_context
- parameter_is_undeclared
- push_assign_tracking
- pop_assign_tracking
- visit_Template
- visit_Block
- visit_Extends
- visit_Include
- visit_Import
- visit_FromImport
- visit_For
- visit_If
- visit_Macro
- visit_CallBlock
- visit_FilterBlock
- visit_With
- visit_ExprStmt
- visit_Output
- visit_Assign
- visit_AssignBlock
- visit_Name
- visit_NSRef
- visit_Const
- visit_TemplateData
- visit_Tuple
- visit_List
- visit_Dict
- visit_Add
- visit_Sub
- visit_Mul
- visit_Div
- visit_FloorDiv
- visit_Pow
- visit_Mod
- visit_And
- visit_Or
- visit_Pos
- visit_Neg
- visit_Not
- visit_Concat
- visit_Compare
- visit_Operand
- visit_Getattr
- visit_Getitem
- visit_Slice
- visit_Filter
- visit_Test
- visit_CondExpr
- visit_Call
- visit_Keyword
- visit_MarkSafe
- visit_MarkSafeIfAutoescape
- visit_EnvironmentAttribute
- visit_ExtensionAttribute
- visit_ImportedName
- visit_InternalName
- visit_ContextReference
- visit_DerivedContextReference
- visit_Continue
- visit_Break
- visit_Scope
- visit_OverlayScope
- visit_EvalContextModifier
- visit_ScopedEvalContextModifier
34def find_undeclared_variables(ast: nodes.Template) -> t.Set[str]: 35 """Returns a set of all variables in the AST that will be looked up from 36 the context at runtime. Because at compile time it's not known which 37 variables will be used depending on the path the execution takes at 38 runtime, all variables are returned. 39 40 >>> from jinja2 import Environment, meta 41 >>> env = Environment() 42 >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}') 43 >>> meta.find_undeclared_variables(ast) == {'bar'} 44 True 45 46 .. admonition:: Implementation 47 48 Internally the code generator is used for finding undeclared variables. 49 This is good to know because the code generator might raise a 50 :exc:`TemplateAssertionError` during compilation and as a matter of 51 fact this function can currently raise that exception as well. 52 """ 53 codegen = TrackingCodeGenerator(ast.environment) # type: ignore 54 codegen.visit(ast) 55 return codegen.undeclared_identifiers
Returns a set of all variables in the AST that will be looked up from the context at runtime. Because at compile time it's not known which variables will be used depending on the path the execution takes at runtime, all variables are returned.
>>> from jinja2 import Environment, meta
>>> env = Environment()
>>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')
>>> meta.find_undeclared_variables(ast) == {'bar'}
True
.. admonition:: Implementation
Internally the code generator is used for finding undeclared variables.
This is good to know because the code generator might raise a
TemplateAssertionError during compilation and as a matter of
fact this function can currently raise that exception as well.
62def find_referenced_templates(ast: nodes.Template) -> t.Iterator[t.Optional[str]]: 63 """Finds all the referenced templates from the AST. This will return an 64 iterator over all the hardcoded template extensions, inclusions and 65 imports. If dynamic inheritance or inclusion is used, `None` will be 66 yielded. 67 68 >>> from jinja2 import Environment, meta 69 >>> env = Environment() 70 >>> ast = env.parse('{% extends "layout.html" %}{% include helper %}') 71 >>> list(meta.find_referenced_templates(ast)) 72 ['layout.html', None] 73 74 This function is useful for dependency tracking. For example if you want 75 to rebuild parts of the website after a layout template has changed. 76 """ 77 template_name: t.Any 78 79 for node in ast.find_all(_ref_types): 80 template: nodes.Expr = node.template # type: ignore 81 82 if not isinstance(template, nodes.Const): 83 # a tuple with some non consts in there 84 if isinstance(template, (nodes.Tuple, nodes.List)): 85 for template_name in template.items: 86 # something const, only yield the strings and ignore 87 # non-string consts that really just make no sense 88 if isinstance(template_name, nodes.Const): 89 if isinstance(template_name.value, str): 90 yield template_name.value 91 # something dynamic in there 92 else: 93 yield None 94 # something dynamic we don't know about here 95 else: 96 yield None 97 continue 98 # constant is a basestring, direct template name 99 if isinstance(template.value, str): 100 yield template.value 101 # a tuple or list (latter *should* not happen) made of consts, 102 # yield the consts that are strings. We could warn here for 103 # non string values 104 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_name 110 # something else we don't care about, we could warn here 111 else: 112 yield None
Finds all the referenced templates from the AST. This will return an
iterator over all the hardcoded template extensions, inclusions and
imports. If dynamic inheritance or inclusion is used, None will be
yielded.
>>> from jinja2 import Environment, meta
>>> env = Environment()
>>> ast = env.parse('{% extends "layout.html" %}{% include helper %}')
>>> list(meta.find_referenced_templates(ast))
['layout.html', None]
This function is useful for dependency tracking. For example if you want to rebuild parts of the website after a layout template has changed.