-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
model.py.jinja
200 lines (168 loc) · 6.97 KB
/
model.py.jinja
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from typing import Any, TypeVar, Optional, BinaryIO, TextIO, TYPE_CHECKING
from attrs import define as _attrs_define
from attrs import field as _attrs_field
{% if model.is_multipart_body %}
import json
{% endif %}
from ..types import UNSET, Unset
{% for relative in model.relative_imports | sort %}
{{ relative }}
{% endfor %}
{% for lazy_import in model.lazy_imports %}
{% if loop.first %}
if TYPE_CHECKING:
{% endif %}
{{ lazy_import }}
{% endfor %}
{% if model.additional_properties %}
{% set additional_property_type = 'Any' if model.additional_properties == True else model.additional_properties.get_type_string(quoted=not model.additional_properties.is_base_type) %}
{% endif %}
{% set class_name = model.class_info.name %}
{% set module_name = model.class_info.module_name %}
{% from "helpers.jinja" import safe_docstring %}
T = TypeVar("T", bound="{{ class_name }}")
{% macro class_docstring_content(model) %}
{% 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 %}
{% endmacro %}
@_attrs_define
class {{ class_name }}:
{{ safe_docstring(class_docstring_content(model)) | indent(4) }}
{% 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 }}] = _attrs_field(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 multipart %}
{{ prop_template.transform_multipart(property, "self." + property.python_name, property.python_name) }}
{% elif prop_template.transform %}
{{ prop_template.transform(property=property, source="self." + property.python_name, destination=property.python_name) }}
{% else %}
{{ property.python_name }} = self.{{ property.python_name }}
{% endif %}
{% endfor %}
field_dict: dict[str, Any] = {}
{% if model.additional_properties %}
{% import "property_templates/" + model.additional_properties.template as prop_template %}
{% if multipart %}
for prop_name, prop in self.additional_properties.items():
{{ prop_template.transform_multipart(model.additional_properties, "prop", "field_dict[prop_name]") | indent(4) }}
{% elif prop_template.transform %}
for prop_name, prop in self.additional_properties.items():
{{ prop_template.transform(model.additional_properties, "prop", "field_dict[prop_name]", declare_type=false) | indent(4) }}
{% else %}
field_dict.update(self.additional_properties)
{% endif %}
{% endif %}
{% if model.required_properties | length > 0 or model.optional_properties | length > 0 %}
field_dict.update({
{% for property in model.required_properties + model.optional_properties %}
{% if property.required %}
"{{ property.name }}": {{ property.python_name }},
{% endif %}
{% endfor %}
})
{% endif %}
{% 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]:
{% for lazy_import in model.lazy_imports %}
{{ lazy_import }}
{% endfor %}
{{ _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:
{% for lazy_import in model.lazy_imports %}
{{ lazy_import }}
{% endfor %}
{% if (model.required_properties or model.optional_properties or model.additional_properties) %}
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 %}
{% endif %}
{{ 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 %}
{% if model.additional_properties.lazy_imports %}
{% for lazy_import in model.additional_properties.lazy_imports %}
{{ lazy_import }}
{% endfor %}
{% endif %}
{% 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 %}