Parses Jinja2 template and returns it’s AST.
Return type: | jinja2.nodes.Template |
---|
Returns a model.Dictionary which reflects a structure of variables used within ast.
Parameters: |
|
---|---|
Return type: | |
Raises: | exceptions.MergeException, exceptions.InvalidExpression, exceptions.UnexpectedExpression |
Returns a model.Dictionary which reflects a structure of the context required by template.
Parameters: |
|
---|---|
Return type: | |
Raises: | exceptions.MergeException, exceptions.InvalidExpression, exceptions.UnexpectedExpression |
Configuration.
Possible values: "dictionary" or "list"".
For example, in the expression xs[a] variable xs may be a list as well as a dictionary. This setting is used to choose between a dictionary and a list when the variable is being indexed with another variable.
Possible values: "dictionary" or "list"".
For example, in the expression xs[2] variable xs may be a list as well as a dictionary. This setting is used to choose between a dictionary and a list when the variable is being indexed with an integer.
Whether or not to consider conditions in if statements as boolean.
If this variable is not set, xs variable in template {% if xs %}{% endif %} will have unknown structure. If this variable is set, xs will be a boolean.
A base variable class.
An ordered list of line numbers on which the variable occurs.
A name of the variable in template.
Is true if the variable is defined using a {% set %} tag before used in the template.
Is true if the variable would be defined (using a {% set %} expression) if it is missing from the template context. For example, x is may_be_defined in the following template:
{% if x is undefined %} {% set x = 1 %} {% endif %}
Is true if the variable occurs only within the default filter.
A dictionary.
Implements some methods of Python dict.
A scalar. Either string, number, boolean or None.
A string.
A number.
Merges two variables.
Parameters: |
|
---|
Note
fst must reflect expressions that occur in template before the expressions of snd.
Conflict of merging two structures.
Variable
Variable
Raised when a visitor was expecting compatibility with expected_struct, but got actual_ast of structure actual_struct.
Compatibility is checked by merging expected structure with actual one.
expected model.Variable
actual jinja2.nodes.Node
model.Variable described by actual_ast
Raised when a template uses Jinja2 features that are not supported by the library or when a template contains incorrect expressions (i.e., such as applying divisibleby filter without an argument).
jinja2.nodes.Node caused the exception
Statement is an instance of jinja2.nodes.Stmt. Statement visitors return models.Dictionary of structures of variables used within the statement.
Decorator that registers a function as a visitor for node_cls.
Parameters: | node_cls – subclass of jinja2.nodes.Stmt |
---|
Returns a structure of ast.
Parameters: | ast – instance of jinja2.nodes.Stmt |
---|---|
Returns: | model.Dictionary |
Expression is an instance of jinja2.nodes.Expr. Expression visitors return a tuple which contains expression type and expression structure.
Context is used when parsing expressions.
Suppose there is an expression:
{{ data.field.subfield }}
It has the following AST:
Getattr(
node=Getattr(
node=Name(name='data')
attr='field'
),
attr='subfield'
)
visit_getattr() returns a pair that looks like this:
(
# return type:
Scalar(...),
# structure:
{
'data: {
'field': {
'subfield': Scalar(...)
}
}
}
}
The return type is defined by the outermost nodes.Getattr node, which in this case is being printed. The structure is build during AST traversal from outer to inners nodes and it is kind of “reversed” in relation to the AST. Context is intended for:
Let’s look through an example.
Suppose visit_getattr() is called with the following arguments:
ast = Getattr(node=Getattr(node=Name(name='data'), attr='field'), attr='subfield'))
context = Context(return_struct_cls=Scalar, predicted_struct=Scalar())
It looks to the outermost AST node and based on it’s type (which is nodes.Getattr) and it’s attr field (which equals to "subfield") infers that a variable described by the nested AST node must a dictionary with "subfield" key.
It calls a visitor for inner node and visit_getattr() gets called again, but with different arguments:
ast = Getattr(node=Name(name='data', ctx='load'), attr='field')
ctx = Context(return_struct_cls=Scalar, predicted_struct=Dictionary({subfield: Scalar()}))
visit_getattr() applies the same logic again. The inner node is a nodes.Name, so that it calls visit_name() with the following arguments:
ast = Name(name='data')
ctx = Context(
return_struct_cls=Scalar,
predicted_struct=Dictionary({
field: Dictionary({subfield: Scalar()}))
})
)
visit_name() does not do much by itself. Based on a context it knows what structure and what type must have a variable described by a given nodes.Name node, so it just returns a pair:
(instance of context.return_struct_cls, Dictionary({data: context.predicted_struct}})
Decorator that registers a function as a visitor for node_cls.
Parameters: | node_cls – subclass of jinja2.nodes.Expr |
---|
Returns a structure of ast.
Parameters: |
|
---|---|
Returns: | a tuple where the first element is an expression type (instance of Variable) and the second element is an expression structure (instance of model.Dictionary) |
Visits nodes and merges results.
Parameters: |
|
---|---|
Returns: | Dictionary |