from typing import Any, Dict, Type, TypeVar, Tuple, Optional, BinaryIO, TextIO {% if model.additional_properties %} from typing import List {% endif %} import attr {% if model.is_multipart_body %} import json {% endif %} from ..types import UNSET, Unset {% for relative in model.relative_imports %} {{ relative }} {% endfor %} {% if model.additional_properties %} {% set additional_property_type = 'Any' if model.additional_properties == True else model.additional_properties.get_type_string() %} {% endif %} {% set class_name = model.class_info.name %} {% set module_name = model.class_info.module_name %} T = TypeVar("T", bound="{{ class_name }}") @attr.s(auto_attribs=True) class {{ class_name }}: """{% if model.title %}{{ model.title | wordwrap(116) }} {% endif -%} {%- if model.description %}{{ model.description | wordwrap(116) }} {% endif %} {% if not model.title and not model.description %} {# Leave extra space so that a section doesn't start on the first line #} {% endif %} {% if model.example %} Example: {{ model.example | string | wordwrap(112) | indent(12) }} {% endif %} {% if model.required_properties or model.optional_properties %} Attributes: {% for property in model.required_properties + model.optional_properties %} {{ property.to_docstring() | wordwrap(112) | indent(12) }} {% endfor %}{% endif %} """ {% for property in model.required_properties + model.optional_properties %} {% if property.default is none and property.required %} {{ property.to_string() }} {% endif %} {% endfor %} {% for property in model.required_properties + model.optional_properties %} {% if property.default is not none or not property.required %} {{ property.to_string() }} {% endif %} {% endfor %} {% if model.additional_properties %} additional_properties: Dict[str, {{ additional_property_type }}] = attr.ib(init=False, factory=dict) {% endif %} {% macro _to_dict(multipart=False) %} {% for property in model.required_properties + model.optional_properties %} {% import "property_templates/" + property.template as prop_template %} {% if prop_template.transform %} {{ prop_template.transform(property, "self." + property.python_name, property.python_name, multipart=multipart) }} {% elif multipart %} {{ property.python_name }} = self.{{ property.python_name }} if isinstance(self.{{ property.python_name }}, Unset) else (None, str(self.{{ property.python_name }}).encode(), "text/plain") {% else %} {{ property.python_name }} = self.{{ property.python_name }} {% endif %} {% endfor %} field_dict: Dict[str, Any] = {} {% if model.additional_properties %} {% if model.additional_properties.template %}{# Can be a bool instead of an object #} {% import "property_templates/" + model.additional_properties.template as prop_template %} {% else %} {% set prop_template = None %} {% endif %} {% if prop_template and prop_template.transform %} for prop_name, prop in self.additional_properties.items(): {{ prop_template.transform(model.additional_properties, "prop", "field_dict[prop_name]", multipart=multipart) | indent(4) }} {% elif multipart %} field_dict.update({ key: (None, str(value).encode(), "text/plain") for key, value in self.additional_properties.items() }) {% else %} field_dict.update(self.additional_properties) {% endif %} {% endif %} field_dict.update({ {% for property in model.required_properties + model.optional_properties %} {% if property.required %} "{{ property.name }}": {{ property.python_name }}, {% endif %} {% endfor %} }) {% for property in model.optional_properties %} {% if not property.required %} if {{ property.python_name }} is not UNSET: field_dict["{{ property.name }}"] = {{ property.python_name }} {% endif %} {% endfor %} return field_dict {% endmacro %} def to_dict(self) -> Dict[str, Any]: {{ _to_dict() | indent(8) }} {% if model.is_multipart_body %} def to_multipart(self) -> Dict[str, Any]: {{ _to_dict(multipart=True) | indent(8) }} {% endif %} @classmethod def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T: _d = src_dict.copy() {% for property in model.required_properties + model.optional_properties %} {% if property.required %} {% set property_source = '_d.pop("' + property.name + '")' %} {% else %} {% set property_source = '_d.pop("' + property.name + '", UNSET)' %} {% endif %} {% import "property_templates/" + property.template as prop_template %} {% if prop_template.construct %} {{ prop_template.construct(property, property_source) | indent(8) }} {% else %} {{ property.python_name }} = {{ property_source }} {% endif %} {% endfor %} {{ module_name }} = cls( {% for property in model.required_properties + model.optional_properties %} {{ property.python_name }}={{ property.python_name }}, {% endfor %} ) {% if model.additional_properties %} {% if model.additional_properties.template %}{# Can be a bool instead of an object #} {% import "property_templates/" + model.additional_properties.template as prop_template %} {% else %} {% set prop_template = None %} {% endif %} {% if prop_template and prop_template.construct %} additional_properties = {} for prop_name, prop_dict in _d.items(): {{ prop_template.construct(model.additional_properties, "prop_dict") | indent(12) }} additional_properties[prop_name] = {{ model.additional_properties.python_name }} {{ module_name }}.additional_properties = additional_properties {% else %} {{ module_name }}.additional_properties = _d {% endif %} {% endif %} return {{ module_name }} {% if model.additional_properties %} @property def additional_keys(self) -> List[str]: return list(self.additional_properties.keys()) def __getitem__(self, key: str) -> {{ additional_property_type }}: return self.additional_properties[key] def __setitem__(self, key: str, value: {{ additional_property_type }}) -> None: self.additional_properties[key] = value def __delitem__(self, key: str) -> None: del self.additional_properties[key] def __contains__(self, key: str) -> bool: return key in self.additional_properties {% endif %}