3
aG‘\ô ã @ sP d Z ddlmZ ddlmZ ddlmZmZ G dd„ deƒZdd„ Z d d
„ Z
dS )a5
jinja2.meta
~~~~~~~~~~~
This module implements various functions that exposes information about
templates that might be interesting for various kinds of applications.
:copyright: (c) 2017 by the Jinja Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
é )Únodes)Ú
CodeGenerator)Ústring_typesÚ iteritemsc @ s( e Zd ZdZdd„ Zdd„ Zdd„ ZdS ) ÚTrackingCodeGeneratorz.We abuse the code generator for introspection.c C s t j| |ddƒ tƒ | _d S )Nz)r Ú__init__ÚsetÚundeclared_identifiers)ÚselfÚenvironment© r ú/usr/lib/python3.6/meta.pyr s
zTrackingCodeGenerator.__init__c C s dS )zDon't write.Nr )r
Úxr r r
Úwrite s zTrackingCodeGenerator.writec C sB t j| |ƒ x0t|jjƒD ] \}\}}|dkr| jj|ƒ qW dS )z$Remember all undeclared identifiers.ZresolveN)r Úenter_framer ZsymbolsÚloadsr Ú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ƒ}|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ð xê| j tjtjtjtjfƒD ]Ì}t|jtjƒs†t|jtj tj
fƒr~x@|jjD ],}t|tjƒrrt|jt
ƒrx|jV qLdV qLW qdV qt|jjt
ƒr |jjV qt|tjƒrât|jjttfƒrâx(|jjD ]}t|t
ƒrÈ|V qÈW qdV qW dS )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ÚtemplateZConstZTupleZListÚitemsÚvaluer ÚtupleÚlist)r ZnodeZ
template_namer r r
Úfind_referenced_templates<