File: //lib/python3.9/site-packages/jinja2/__pycache__/meta.cpython-39.pyc
a
�`# � @ sX d Z ddlmZ ddlmZ ddlmZ ddlmZ G dd� de�Zdd � Z d
d� Z
dS )
z_Functions that expose information about templates that might be
interesting for introspection.
� )�nodes)� iteritems)�string_types)�
CodeGeneratorc @ s( e Zd ZdZdd� Zdd� Zdd� ZdS ) �TrackingCodeGeneratorz.We abuse the code generator for introspection.c C s t �| |dd� t� | _d S )Nz<introspection>)r �__init__�set�undeclared_identifiers)�self�environment� r �//usr/lib/python3.9/site-packages/jinja2/meta.pyr s zTrackingCodeGenerator.__init__c C s dS )zDon't write.Nr )r
�xr r r
�write s zTrackingCodeGenerator.writec C sJ t �| |� t|jj�D ],\}\}}|dkr|| jjvr| j�|� qdS )z$Remember all undeclared identifiers.�resolveN) r �enter_framer Zsymbols�loadsr �globalsr �add)r
�frame�_�actionZparamr r r
r s z!TrackingCodeGenerator.enter_frameN)�__name__�
__module__�__qualname__�__doc__r r r r r r r
r s r c C s t | j�}|�| � |jS )a 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) == set(['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
:exc:`TemplateAssertionError` during compilation and as a matter of
fact this function can currently raise that exception as well.
)r r Zvisitr )�astZcodegenr r r
�find_undeclared_variables s
r c c s� | � tjtjtjtjf�D ]�}t|jtj�s�t|jtj tj
f�rx|jjD ],}t|tj�rnt|jt
�rt|jV qHdV qHqdV qt|jjt
�r�|jjV qt|tj�r�t|jjttf�r�|jjD ]}t|t
�r�|V q�qdV qdS )ab 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.
N)Zfind_allr ZExtendsZ
FromImportZImportZInclude�
isinstance�templateZConst�Tuple�List�items�valuer �tuple�list)r ZnodeZ
template_namer r r
�find_referenced_templates5 s* �
�
r&