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<introspection>)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<