| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 """ | 2 """ |
| 3 jinja2.meta | 3 jinja2.meta |
| 4 ~~~~~~~~~~~ | 4 ~~~~~~~~~~~ |
| 5 | 5 |
| 6 This module implements various functions that exposes information about | 6 This module implements various functions that exposes information about |
| 7 templates that might be interesting for various kinds of applications. | 7 templates that might be interesting for various kinds of applications. |
| 8 | 8 |
| 9 :copyright: (c) 2010 by the Jinja Team, see AUTHORS for more details. | 9 :copyright: (c) 2010 by the Jinja Team, see AUTHORS for more details. |
| 10 :license: BSD, see LICENSE for more details. | 10 :license: BSD, see LICENSE for more details. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 def find_undeclared_variables(ast): | 33 def find_undeclared_variables(ast): |
| 34 """Returns a set of all variables in the AST that will be looked up from | 34 """Returns a set of all variables in the AST that will be looked up from |
| 35 the context at runtime. Because at compile time it's not known which | 35 the context at runtime. Because at compile time it's not known which |
| 36 variables will be used depending on the path the execution takes at | 36 variables will be used depending on the path the execution takes at |
| 37 runtime, all variables are returned. | 37 runtime, all variables are returned. |
| 38 | 38 |
| 39 >>> from jinja2 import Environment, meta | 39 >>> from jinja2 import Environment, meta |
| 40 >>> env = Environment() | 40 >>> env = Environment() |
| 41 >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}') | 41 >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}') |
| 42 >>> meta.find_undeclared_variables(ast) | 42 >>> meta.find_undeclared_variables(ast) == set(['bar']) |
| 43 set(['bar']) | 43 True |
| 44 | 44 |
| 45 .. admonition:: Implementation | 45 .. admonition:: Implementation |
| 46 | 46 |
| 47 Internally the code generator is used for finding undeclared variables. | 47 Internally the code generator is used for finding undeclared variables. |
| 48 This is good to know because the code generator might raise a | 48 This is good to know because the code generator might raise a |
| 49 :exc:`TemplateAssertionError` during compilation and as a matter of | 49 :exc:`TemplateAssertionError` during compilation and as a matter of |
| 50 fact this function can currently raise that exception as well. | 50 fact this function can currently raise that exception as well. |
| 51 """ | 51 """ |
| 52 codegen = TrackingCodeGenerator(ast.environment) | 52 codegen = TrackingCodeGenerator(ast.environment) |
| 53 codegen.visit(ast) | 53 codegen.visit(ast) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 # yield the consts that are strings. We could warn here for | 94 # yield the consts that are strings. We could warn here for |
| 95 # non string values | 95 # non string values |
| 96 elif isinstance(node, nodes.Include) and \ | 96 elif isinstance(node, nodes.Include) and \ |
| 97 isinstance(node.template.value, (tuple, list)): | 97 isinstance(node.template.value, (tuple, list)): |
| 98 for template_name in node.template.value: | 98 for template_name in node.template.value: |
| 99 if isinstance(template_name, string_types): | 99 if isinstance(template_name, string_types): |
| 100 yield template_name | 100 yield template_name |
| 101 # something else we don't care about, we could warn here | 101 # something else we don't care about, we could warn here |
| 102 else: | 102 else: |
| 103 yield None | 103 yield None |
| OLD | NEW |