{# ============================================================================ #}
{# Import Utilities #}
{# ============================================================================ #}
{% import "python-macros.jinja" as utils %}
{# ============================================================================ #}
{# Type Generation Macros #}
{# ============================================================================ #}
{# Creates the inner type definition for an attribute with XML configuration #}
{%- macro create_inner_type(attr) -%}
{{ utils.xml_type(attr.xml) }}(
{%- if attr.multiple is false %}
default {{- utils.get_default(attr) -}},
{%- endif %}
{%- if attr.multiple is true %}
default_factory=list,{%- endif %}
tag="{{ utils.xml_tag(attr.xml) }}",
{%- if attr.docstring | length > 0 %}
description="""{{ wrap(attr.docstring, 80, "", " ", None ) }}""",
{%- endif %}
json_schema_extra={{ utils.create_options(attr.options, attr.term) }},
)
{%- endmacro -%}
{# Creates a complete attribute definition with XML wrapping if needed #}
{% macro create_attribute(attr) -%}
{%- if attr.xml.wrapped %}
{{ attr.name | to_identifier }}: {{ utils.get_type(attr) }} = wrapped(
"{{ attr.xml.wrapped | join('/') }}",
{{ create_inner_type(attr) | indent(4) }},
)
{%- else %}
{{ attr.name | to_identifier }}: {{ utils.get_type(attr) }} = {{ create_inner_type(attr) }}
{%- endif %}
{%- endmacro %}
{# ============================================================================ #}
{# File Header Documentation #}
{# ============================================================================ #}
"""
This file contains Pydantic XML model definitions for data validation.
Pydantic is a data validation library that uses Python type annotations.
It allows you to define data models with type hints that are validated
at runtime while providing static type checking.
Usage example:
```python
from my_model import MyModel
# Validates data at runtime
my_model = MyModel(name="John", age=30)
# Type-safe - my_model has correct type hints
print(my_model.name)
# Will raise error if validation fails
try:
MyModel(name="", age=30)
except ValidationError as e:
print(e)
```
For more information see:
https://pydantic-xml.readthedocs.io/en/latest/
WARNING: This is an auto-generated file.
Do not edit directly - any changes will be overwritten.
"""
## This is a generated file. Do not modify it manually!
{# ============================================================================ #}
{# Imports #}
{# ============================================================================ #}
from __future__ import annotations
from typing import Dict, List, Optional, Union
from uuid import uuid4
from datetime import date, datetime
from xml.dom import minidom
{% if enums | length > 0 -%}
from enum import Enum
{%- endif %}
from lxml.etree import _Element
from pydantic import PrivateAttr, model_validator
from pydantic_xml import attr, wrapped, element, BaseXmlModel
{# ============================================================================ #}
{# Model Definitions #}
{# ============================================================================ #}
{% for object in objects %}
class {{object.name}}(
BaseXmlModel,
search_mode="unordered",
{%- if object.term and ":" not in object.term %}
ns="{{ object.term }}",
{%- endif %}
{%- if nsmap %}
nsmap={
{%- for key, value in nsmap | dictsort %}
"{{ key }}": "{{ value }}",
{%- endfor %}
},
{%- endif %}
):
{%- if object.docstring %}
{{ utils.object_docstring(object) }}
{%- endif %}
{%- if object.attributes | length == 0 %}
pass
{%- endif %}
{%- for attr in object.attributes -%}
{{ create_attribute(attr) }}
{%- endfor %}
{# Generate helper methods for list attributes #}
{% for attr in object.attributes %}
{%- for dtype in attr.dtypes %}
{%- if dtype in object_names and attr.multiple is true %}
def add_to_{{ attr.name | to_identifier }}(
{{ utils.signature(objects, dtype) }}
):
"""Helper method to add a new {{ dtype }} to the {{ attr.name }} list."""
params = { {{ utils.params(objects, dtype) }}
}
self.{{ attr.name | to_identifier }}.append(
{{ dtype }}(**params)
)
return self.{{ attr.name | to_identifier }}[-1]
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- if object.attributes | length > 0 %}
def xml(self, encoding: str = "unicode") -> str | bytes:
"""Converts the object to an XML string
Args:
encoding (str, optional): The encoding to use. If set to "bytes", will return a bytes string.
Defaults to "unicode".
Returns:
str | bytes: The XML representation of the object
"""
if encoding == "bytes":
return self.to_xml()
raw_xml = self.to_xml(encoding=None)
parsed_xml = minidom.parseString(raw_xml)
return parsed_xml.toprettyxml(indent=" ")
{%- endif %}
{% endfor %}
{# ============================================================================ #}
{# Enum Definitions #}
{# ============================================================================ #}
{%- for enum in enums %}
class {{ enum.name }}(Enum):
"""Enumeration for {{ enum.name }} values"""
{%- for key, value in enum.mappings | dictsort %}
{{ key }} = "{{ value }}"
{%- endfor %}
{% endfor %}
{# ============================================================================ #}
{# Utility Functions #}
{# ============================================================================ #}