jinja2schema.core¶
-
jinja2schema.core.
parse
(template, jinja2_env=None)[source]¶ Parses Jinja2 template and returns it’s AST.
Return type: jinja2.nodes.Template
-
jinja2schema.core.
infer_from_ast
(ast, ignore_constants=True, config=<jinja2schema.config.Config object>)[source]¶ Returns a
model.Dictionary
which reflects a structure of variables used withinast
.Parameters: - ast (
jinja2.nodes.Node
) – AST - ignore_constants – excludes constant variables from a resulting structure
- config (
config.Config
) – a config
Return type: Raises: exceptions.MergeException
,exceptions.InvalidExpression
,exceptions.UnexpectedExpression
- ast (
-
jinja2schema.core.
infer
(template, config=<jinja2schema.config.Config object>)[source]¶ Returns a
model.Dictionary
which reflects a structure of the context required bytemplate
.Parameters: - template (string) – a template
- config (
config.Config
) – a config
Return type: Raises: exceptions.MergeException
,exceptions.InvalidExpression
,exceptions.UnexpectedExpression
-
class
jinja2schema.core.
JSONSchemaDraft4Encoder
[source]¶ Extensible JSON schema encoder for
model.Variable
.
-
class
jinja2schema.core.
StringJSONSchemaDraft4Encoder
[source]¶ Encodes
model.Unknown
andmodel.Scalar
(but not it’s subclasses –model.String
,model.Number
ormodel.Boolean
) variables as strings.Useful for rendering forms using resulting JSON schema, as most of form-rendering tools do not support “anyOf” validator.
-
jinja2schema.core.
to_json_schema
(var, jsonschema_encoder=<class 'jinja2schema.core.JSONSchemaDraft4Encoder'>)[source]¶ Returns JSON schema that describes
var
.Parameters: - var (
model.Variable
) – a variable - jsonschema_encoder (subclass of
JSONSchemaEncoder
) – JSON schema encoder
Returns: dict
- var (
jinja2schema.config¶
-
class
jinja2schema.config.
Config
(TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE='dictionary', TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE='list', BOOLEAN_CONDITIONS=False, PACKAGE_NAME='', TEMPLATE_DIR='templates', ORDER_NUMBER=False, ORDER_NUMBER_SUB_COUNTER=True)[source]¶ Configuration.
-
ORDER_NUMBER_SUB_COUNTER
= True¶ Independent subsection order numbers
Use a separate counter in subsections as order number creator.
-
TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE
= 'list'¶ Possible values:
"dictionary"
,"list"
or"tuple"
.For example, in the expression
xs[2]
variablexs
may be a list as well as a dictionary or a tuple. This setting is used to choose between a dictionary, a tuple and a list when the variable is being indexed with an integer.
-
TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE
= 'dictionary'¶ Possible values:
"dictionary"
or"list""
.For example, in the expression
xs[a]
variablexs
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.
-
BOOLEAN_CONDITIONS
= False¶ 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.
-
PACKAGE_NAME
= ''¶ Name of the package where you want to load templates from.
This configuration is for if you are using includes in your jinja templates. This tells jinja where to look to be able to load the included template from. If you do not plan on using
includes
this configuration is not needed.
-
TEMPLATE_DIR
= 'templates'¶ Name of the directory where you want to load templates from. Defaulted to
templates
This configuration is for if you are using includes in your jinja templates. This tells jinja which directoy to look to be able to load the included template from. If you do not plan on using
includes
this configuration is not needed.
-
ORDER_NUMBER
= False¶ Add a order number to each node
Add a order number to make schema sortable.
-
jinja2schema.model¶
-
class
jinja2schema.model.
Variable
(label=None, linenos=None, constant=False, may_be_defined=False, used_with_default=False, checked_as_undefined=False, checked_as_defined=False, value=None, order_nr=None)[source]¶ A base variable class.
-
linenos
¶ An ordered list of line numbers on which the variable occurs.
-
label
¶ A name of the variable in template.
-
constant
¶ Is true if the variable is defined using a
{% set %}
tag before it occurs within any other statement in the template, or inferred from a constant Jinja2 node.
-
may_be_defined
¶ Is true if the variable would be defined (using a
{% set %}
expression) if it’s missing from the template context. For example,x
ismay_be_defined
in the following template:{% if x is undefined %} {% set x = 1 %} {% endif %}
-
used_with_default
¶ Is true if the variable occurs only within the
default
filter.
-
checked_as_undefined
¶ Is true if the variable occurs within
{% if %}
block which condition checks if the variable is undefined.
-
checked_as_defined
¶ Is true if the variable occurs within
{% if %}
block which condition checks if the variable is defined.
-
value
¶ Value of the variable in template. Set by default filter or assignment.
-
-
class
jinja2schema.model.
Dictionary
(data=None, **kwargs)[source]¶ A dictionary.
Implements some methods of Python
dict
.
-
class
jinja2schema.model.
Tuple
(items, **kwargs)[source]¶ A tuple.
-
items
Whether new elements can be added to the tuple in the process of merge or not.
-
-
class
jinja2schema.model.
Scalar
(label=None, linenos=None, constant=False, may_be_defined=False, used_with_default=False, checked_as_undefined=False, checked_as_defined=False, value=None, order_nr=None)[source]¶ A scalar. Either string, number, boolean or
None
.
-
class
jinja2schema.model.
String
(label=None, linenos=None, constant=False, may_be_defined=False, used_with_default=False, checked_as_undefined=False, checked_as_defined=False, value=None, order_nr=None)[source]¶ A string.
-
class
jinja2schema.model.
Number
(label=None, linenos=None, constant=False, may_be_defined=False, used_with_default=False, checked_as_undefined=False, checked_as_defined=False, value=None, order_nr=None)[source]¶ A number.
jinja2schema.mergers¶
-
jinja2schema.mergers.
merge
(fst, snd, custom_merger=None)[source]¶ Merges two variables.
Parameters: - fst (
model.Variable
) – first variable - snd (
model.Variable
) – second variable
Note
fst
must reflect expressions that occur in template before the expressions ofsnd
.- fst (
jinja2schema.exceptions¶
-
exception
jinja2schema.exceptions.
MergeException
(fst, snd)[source]¶ Conflict of merging two structures.
-
fst
¶ Variable
-
snd
¶ Variable
-
-
exception
jinja2schema.exceptions.
UnexpectedExpression
(expected_struct, actual_ast, actual_struct)[source]¶ Raised when a visitor was expecting compatibility with
expected_struct
, but gotactual_ast
of structureactual_struct
.Compatibility is checked by merging expected structure with actual one.
-
expected_struct
¶ expected
model.Variable
-
actual_ast
¶ actual
jinja2.nodes.Node
-
actual_ast
model.Variable
described byactual_ast
-
-
exception
jinja2schema.exceptions.
InvalidExpression
(ast, message)[source]¶ 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).-
ast
¶ jinja2.nodes.Node
caused the exception
-
jinja2schema.util¶
jinja2schema.visitors.stmt¶
Statement is an instance of jinja2.nodes.Stmt
.
Statement visitors return models.Dictionary
of structures of variables used within the statement.
-
jinja2schema.visitors.stmt.
visits_stmt
(node_cls)[source]¶ Decorator that registers a function as a visitor for
node_cls
.Parameters: node_cls – subclass of jinja2.nodes.Stmt
-
jinja2schema.visitors.stmt.
visit_stmt
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶ Returns a structure of
ast
.Parameters: ast – instance of jinja2.nodes.Stmt
Returns: model.Dictionary
-
jinja2schema.visitors.stmt.
visit_for
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶
-
jinja2schema.visitors.stmt.
visit_if
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶
-
jinja2schema.visitors.stmt.
visit_assign
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶
-
jinja2schema.visitors.stmt.
visit_output
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶
-
jinja2schema.visitors.stmt.
visit_macro
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶
-
jinja2schema.visitors.stmt.
visit_block
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶
-
jinja2schema.visitors.stmt.
visit_include
(ast, macroses=None, config=<jinja2schema.config.Config object>, child_blocks=None)[source]¶
jinja2schema.visitors.expr¶
Expression is an instance of jinja2.nodes.Expr
.
Expression visitors return a tuple which contains expression type and expression structure.
-
class
jinja2schema.visitors.expr.
Context
(ctx=None, return_struct_cls=None, predicted_struct=None)[source]¶ 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:- capturing a return type and passing it to the innermost expression node;
- passing a structure “under construction” to the visitors of nested nodes.
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’sattr
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 anodes.Name
, so that it callsvisit_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 givennodes.Name
node, so it just returns a pair:(instance of context.return_struct_cls, Dictionary({data: context.predicted_struct}})
-
jinja2schema.visitors.expr.
visits_expr
(node_cls)[source]¶ Decorator that registers a function as a visitor for
node_cls
.Parameters: node_cls – subclass of jinja2.nodes.Expr
-
jinja2schema.visitors.expr.
visit_expr
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶ Returns a structure of
ast
.Parameters: - ctx –
Context
- ast – instance of
jinja2.nodes.Expr
Returns: a tuple where the first element is an expression type (instance of
Variable
) and the second element is an expression structure (instance ofmodel.Dictionary
)- ctx –
-
jinja2schema.visitors.expr.
visit_bin_expr
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_unary_expr
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_compare
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_slice
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_name
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_getattr
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_getitem
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_test
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_concat
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_cond_expr
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_call
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_filter
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_template_data
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_const
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
-
jinja2schema.visitors.expr.
visit_tuple
(ast, ctx, macroses=None, config=<jinja2schema.config.Config object>)[source]¶
jinja2schema.visitors.util¶
-
jinja2schema.visitors.util.
visit_many
(nodes, macroses, config, predicted_struct_cls=<class 'jinja2schema.model.Scalar'>, return_struct_cls=<class 'jinja2schema.model.Unknown'>)[source]¶ Visits
nodes
and merges results.Parameters: - nodes – list of
jinja2.nodes.Node
- predicted_struct_cls –
predicted_struct
for expression visitors will be constructed using this class by callingfrom_ast()
method
Returns: Dictionary
- nodes – list of