{#
This macro is used to create an attribute in a data model class.
The attribute is created with the following parameters:
#}
{% macro create_attribute(attr) %}
{{ attr.name }}: {{ get_type(attr) }} = {{ xml_type(attr.xml) }}(
{% if attr.required is false and attr.multiple is false -%}
default={%- if attr.default -%}{{ attr.default }}{%- else -%}None{%- endif -%},
{% endif -%}
{% if attr.multiple is true -%}
default_factory=list,
{% endif -%}
tag="{{ xml_tag(attr.xml) }}",
{% if attr.docstring | length > 0 -%}
description="""{{ wrap(attr.docstring, 80, "", " ", None ) }}""",
{% endif -%}
json_schema_extra={{ create_options(attr.options, attr.term) }},
)
{% endmacro %}
{#
This macro is used to create the options for an attribute in a data model class.
The options are created with the following parameters:
#}
{%- macro create_options(options, term) -%}
dict(
{%- if term -%}
term = "{{ term }}",
{%- endif -%}
{%- for option in options -%}
{{ option.key }} = "{{ option.value }}",
{%- endfor -%}
)
{%- endmacro -%}
{#
This macro is used to determine the XML tag
#}
{%- macro xml_tag(xml_option) -%}
{{ xml_option.name }}
{%- endmacro -%}
{#
This macro is used to determine the XML type
The XML type is determined by the following parameters:
- is_attr: True if the attribute is an XML attribute, False if the attribute is an XML element
#}
{%- macro xml_type(xml_option) %}
{%- if xml_option.is_attr is false -%}
element{%- else -%} attr {%- endif -%}
{% endmacro %}
{#
This macro is used to to determine the type of the attribute
#}
{%- macro type(attr, in_list) -%}
{%- if attr.dtypes | length == 1 -%}
{%- if attr.required or in_list -%}
{{ attr.dtypes[0] }}
{%- else -%}
Optional[{{ attr.dtypes[0] }}]
{%- endif -%}
{%- else -%}
Union[
{%- if attr.required is false -%}None, {%- endif -%}
{%- for dtype in attr.dtypes -%}
{{ dtype }}{%- if not loop.last -%}, {%- endif -%}
{%- endfor -%}
]
{%- endif -%}
{%- endmacro -%}
{#
This macro checks if the attribute is a list and returns the type of the attribute
#}
{%- macro get_type(attr) -%}
{%- if attr.multiple is true -%}
list[{{ type(attr, true) }}]
{%- else -%}
{{ type(attr) }}
{%- endif -%}
{%- endmacro -%}
{#
This macro is used to generate a signature for a method
of a class. Mostly for adding entries to list objects.
#}
{%- macro signature(objects, name) -%}
{%- for object in objects -%}
{%- if object.name == name -%}
self,
{%- for attr in object.attributes %}
{{ attr.name | to_identifier }}: {{ get_type(attr) }}{{ get_default(attr) }},
{%- endfor %}
**kwargs,
{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{#
This macro returns only the default value of an attribute (without the
leading ` = `). Used when the default must be embedded inside a
`field(default=...)` call, e.g. when a dashed name needs a `field_name` alias.
#}
{% macro default_value_only(attr) -%}
{%- if attr.multiple -%}
[]
{%- elif 'default' in attr -%}
{%- if attr.default is true or attr.default is false -%}
{{ attr.default | capitalize }}
{%- else -%}
{{ default_value(attr) }}
{%- endif -%}
{%- elif attr.required is false -%}
None
{%- endif -%}
{%- endmacro %}
{#
This macro is used to determine the default of an attribute
#}
{% macro get_default(attr) -%}
{%- if attr.multiple -%}
= []
{%- elif 'default' in attr -%}
= {% if attr.default is true or attr.default is false -%}
{{ attr.default | capitalize }}
{%- else -%}
{{ default_value(attr) }}
{%- endif -%}
{%- elif attr.required is false -%}
= None
{%- endif -%}
{%- endmacro -%}
{#
This macro is used to generate parameters for the body
of a method. Mostly for adding entries to list objects.
#}
{%- macro params(objects, name) %}
{%- for object in objects -%}
{%- if object.name == name %}
{%- for attr in object.attributes %}
"{{ attr.name | to_identifier }}": {{ attr.name | to_identifier }}{% if not loop.last %}, {% endif %}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- endmacro %}
{#
This macro rebuilds the classes to evaluate forward references
#}
{%- macro rebuild_classes(objects) %}
# Rebuild all the classes within this file
for cls in [
{%- for object in objects %}
{{ object.name }},
{%- endfor %}
]:
cls.model_rebuild()
{%- endmacro -%}
{#
This macro is used to generate a docstring for an object
#}
{%- macro object_docstring(object) -%}
"""
{{ wrap(object.docstring, 80, "", " ", None) }}
"""
{%- endmacro -%}