forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py.jinja
134 lines (111 loc) · 4.8 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
from typing import Any, Dict, Type, TypeVar
{% if model.additional_properties %}
from typing import List
{% endif %}
import attr
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 }}:
""" {{ model.description }} """
{% 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 %}
def to_dict(self) -> Dict[str, Any]:
{% for property in model.required_properties + model.optional_properties %}
{% if property.template %}
{% from "property_templates/" + property.template import transform %}
{{ transform(property, "self." + property.python_name, property.python_name) | indent(8) }}
{% else %}
{{ property.python_name }} = self.{{ property.python_name }}
{% endif %}
{% endfor %}
field_dict: Dict[str, Any] = {}
{% if model.additional_properties %}
{% if model.additional_properties.template %}
{% from "property_templates/" + model.additional_properties.template import transform %}
for prop_name, prop in self.additional_properties.items():
{{ transform(model.additional_properties, "prop", "field_dict[prop_name]") | indent(12) }}
{% 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
@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 %}
{% if property.template %}
{% from "property_templates/" + property.template import construct %}
{{ 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 %}
{% from "property_templates/" + model.additional_properties.template import construct %}
additional_properties = {}
for prop_name, prop_dict in d.items():
{{ 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 %}