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
Provenance
- Commit
eaa1d64- Tree
0a1cf81- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
63cf9b8
1 file changed+77 added−0 removed
added:jinja2/meta.py+77 added
1+# -*- coding: utf-8 -*-2+"""3+ jinja2.meta4+ ~~~~~~~~~~~5+6+ This module implements various functions that exposes information about7+ templates that might be interesting for various kinds of applications.8+9+ :copyright: (c) 2009 by the Jinja2 Team, see AUTHORS for more details.10+ :license: BSD, see LICENSE for more details.11+"""12+from jinja2 import nodes13+from jinja2.compiler import CodeGenerator14+15+16+class TrackingCodeGenerator(CodeGenerator):17+ """We abuse the code generator for introspection."""18+19+ def __init__(self, environment):20+ CodeGenerator.__init__(self, environment, '<introspection>',21+ '<introspection>')22+ self.undeclared_identifiers = set()23+24+ def write(self, x):25+ """Don't write."""26+27+ def pull_locals(self, frame):28+ """Remember all undeclared identifiers."""29+ self.undeclared_identifiers.update(frame.identifiers.undeclared)30+31+32+def 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.37+38+ >>> 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'])43+44+ .. admonition:: Implementation45+46+ 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_identifiers54+55+56+def 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.61+62+ >>> 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]67+68+ 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