Skip to content
Myr

Added missing module.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 63cf9b8 · Browse files · 1 file+77 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+77 added−0 removed

  • other:+77 added−0 removed

1 file+77 added−0 removed

added:jinja2/meta.py+77 added
1# -*- coding: utf-8 -*-2"""3 jinja2.meta4 ~~~~~~~~~~~56 This module implements various functions that exposes information about7 templates that might be interesting for various kinds of applications.89 :copyright: (c) 2009 by the Jinja2 Team, see AUTHORS for more details.10 :license: BSD, see LICENSE for more details.11"""12from jinja2 import nodes13from jinja2.compiler import CodeGenerator141516class TrackingCodeGenerator(CodeGenerator):17 """We abuse the code generator for introspection."""1819 def __init__(self, environment):20 CodeGenerator.__init__(self, environment, '<introspection>',21 '<introspection>')22 self.undeclared_identifiers = set()2324 def write(self, x):25 """Don't write."""2627 def pull_locals(self, frame):28 """Remember all undeclared identifiers."""29 self.undeclared_identifiers.update(frame.identifiers.undeclared)303132def find_undeclared_variables(ast):33 """Returns a set of all variables in the AST that will be looked up from34 the context at runtime. Because at compile time it's not known which35 variables will be used depending on the path the execution takes at36 runtime, all variables are returned.3738 >>> from jinja2 import Environment, meta39 >>> env = Environment()40 >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')41 >>> meta.find_undeclared_variables(ast)42 set(['bar'])4344 .. admonition:: Implementation4546 Internally the code generator is used for finding undeclared variables.47 This is good to know because the code generator might raise a48 :exc:`TemplateAssertionError` during compilation and as a matter of49 fact this function can currently raise that exception as well.50 """51 codegen = TrackingCodeGenerator(ast.environment)52 codegen.visit(ast)53 return codegen.undeclared_identifiers545556def find_referenced_templates(ast):57 """Finds all the referenced templates from the AST. This will return an58 iterator over all the hardcoded template extensions, inclusions and59 imports. If dynamic inheritance or inclusion is used, `None` will be60 yielded.6162 >>> from jinja2 import Environment, meta63 >>> env = Environment()64 >>> ast = env.parse('{% extends "layout.html" %}{% include helper %}')65 >>> list(meta.find_referenced_templates(ast))66 ['layout.html', None]6768 This function is useful for dependency tracking. For example if you want69 to rebuild parts of the website after a layout template has changed.70 """71 for node in ast.find_all((nodes.Extends, nodes.FromImport, nodes.Import,72 nodes.Include)):73 if isinstance(node.template, nodes.Const) and \74 isinstance(node.template.value, basestring):75 yield node.template.value76 else:77 yield None