-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathv2.py
267 lines (236 loc) · 8.84 KB
/
v2.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Model for dumping Pydantic models.
See Also
--------
Roundtrip comments with ruamel.yaml
https://yaml.readthedocs.io/en/latest/detail.html#round-trip-including-comments
Currently, it's not possible to round-trip comments in `pydantic-yaml`.
If you need to keep comments, you'll have to have parallel updating and validation.
"""
# mypy: ignore-errors
import json
from io import BytesIO, IOBase, StringIO
from pathlib import Path
from typing import Any, Optional, Tuple, Type, TypeVar, Union
from typing_extensions import Literal # noqa
from pydantic.version import VERSION as PYDANTIC_VERSION
from ruamel.yaml import YAML
if (PYDANTIC_VERSION < "2") or (PYDANTIC_VERSION > "3"):
raise ImportError("This module can only be imported in Pydantic v2.")
from pydantic import BaseModel, TypeAdapter
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic.v1 import parse_obj_as
T = TypeVar("T", bound=Union[BaseModel, BaseModelV1])
def _chk_model(model: Any) -> Tuple[Union[BaseModel, BaseModelV1], Literal[1, 2]]:
"""Ensure the model passed is a Pydantic model."""
if isinstance(model, BaseModel):
return model, 2
elif isinstance(model, BaseModelV1):
return model, 1
raise TypeError(
"We can currently only write `pydantic.BaseModel` or `pydantic.v1.BaseModel`"
f"but recieved: {model!r}"
)
def _write_yaml_model(
stream: IOBase,
model: Union[BaseModel, BaseModelV1],
*,
default_flow_style: Optional[bool] = None,
indent: Optional[int] = None,
map_indent: Optional[int] = None,
sequence_indent: Optional[int] = None,
sequence_dash_offset: Optional[int] = None,
custom_yaml_writer: Optional[YAML] = None,
**json_kwargs,
) -> None:
"""Write YAML model to the stream object.
This uses JSON dumping as an intermediary.
Parameters
----------
stream : IOBase
The stream to write to.
model : BaseModel
The model to write.
default_flow_style : bool
Whether to use "flow style" (more human-readable).
https://yaml.readthedocs.io/en/latest/detail.html?highlight=default_flow_style#indentation-of-block-sequences
indent : None or int
General indent value. Leave as None for the default.
map_indent, sequence_indent, sequence_dash_offset : None or int
More specific indent values.
custom_yaml_writer : None or YAML
An instance of ruamel.yaml.YAML (or a subclass) to use as the writer.
The above options will be set on it, if given.
json_kwargs : Any
Keyword arguments to pass `model.json()`.
"""
model, vers = _chk_model(model)
if vers == 1:
json_val = model.json(**json_kwargs) # type: ignore
else:
json_val = model.model_dump_json(**json_kwargs) # type: ignore
val = json.loads(json_val)
# Allow setting custom writer
if custom_yaml_writer is None:
writer = YAML(typ="safe", pure=True)
elif isinstance(custom_yaml_writer, YAML):
writer = custom_yaml_writer
else:
raise TypeError(f"Please pass a YAML instance or subclass. Got {custom_yaml_writer!r}")
# Set options
if default_flow_style is not None:
writer.default_flow_style = default_flow_style
writer.indent(mapping=indent, sequence=indent, offset=indent)
writer.indent(mapping=map_indent, sequence=sequence_indent, offset=sequence_dash_offset)
# TODO: Configure writer further?
writer.dump(val, stream)
def to_yaml_str(
model: Union[BaseModel, BaseModelV1],
*,
default_flow_style: Optional[bool] = False,
indent: Optional[int] = None,
map_indent: Optional[int] = None,
sequence_indent: Optional[int] = None,
sequence_dash_offset: Optional[int] = None,
custom_yaml_writer: Optional[YAML] = None,
**json_kwargs,
) -> str:
"""Generate a YAML string representation of the model.
Parameters
----------
model : BaseModel
The model to convert.
default_flow_style : bool
Whether to use "flow style" (more human-readable).
https://yaml.readthedocs.io/en/latest/detail.html?highlight=default_flow_style#indentation-of-block-sequences
indent : None or int
General indent value. Leave as None for the default.
map_indent, sequence_indent, sequence_dash_offset : None or int
More specific indent values.
custom_yaml_writer : None or YAML
An instance of ruamel.yaml.YAML (or a subclass) to use as the writer.
The above options will be set on it, if given.
json_kwargs : Any
Keyword arguments to pass `model.json()`.
Notes
-----
This currently uses JSON dumping as an intermediary.
This means that you can use `json_encoders` in your model.
"""
model, _ = _chk_model(model)
stream = StringIO()
_write_yaml_model(
stream,
model,
default_flow_style=default_flow_style,
indent=indent,
map_indent=map_indent,
sequence_indent=sequence_indent,
sequence_dash_offset=sequence_dash_offset,
custom_yaml_writer=custom_yaml_writer,
**json_kwargs,
)
stream.seek(0)
return stream.read()
def to_yaml_file(
file: Union[Path, str, IOBase],
model: Union[BaseModel, BaseModelV1],
*,
default_flow_style: Optional[bool] = False,
indent: Optional[int] = None,
map_indent: Optional[int] = None,
sequence_indent: Optional[int] = None,
sequence_dash_offset: Optional[int] = None,
custom_yaml_writer: Optional[YAML] = None,
**json_kwargs,
) -> None:
"""Write a YAML file representation of the model.
Parameters
----------
file : Path or str or IOBase
The file path or stream to write to.
model : BaseModel
The model to write.
default_flow_style : bool
Whether to use "flow style" (more human-readable).
https://yaml.readthedocs.io/en/latest/detail.html?highlight=default_flow_style#indentation-of-block-sequences
indent : None or int
General indent value. Leave as None for the default.
map_indent, sequence_indent, sequence_dash_offset : None or int
More specific indent values.
custom_yaml_writer : None or YAML
An instance of ruamel.yaml.YAML (or a subclass) to use as the writer.
The above options will be set on it, if given.
json_kwargs : Any
Keyword arguments to pass `model.json()`.
Notes
-----
This currently uses JSON dumping as an intermediary.
This means that you can use `json_encoders` in your model.
"""
model, _ = _chk_model(model)
write_kwargs = dict(
default_flow_style=default_flow_style,
indent=indent,
map_indent=map_indent,
sequence_indent=sequence_indent,
sequence_dash_offset=sequence_dash_offset,
custom_yaml_writer=custom_yaml_writer,
**json_kwargs,
)
if isinstance(file, IOBase): # open file handle
_write_yaml_model(file, model, **write_kwargs)
return
if isinstance(file, str): # local path to file
file = Path(file).resolve()
elif isinstance(file, Path):
file = file.resolve()
else:
raise TypeError(f"Expected Path, str, or stream, but got {file!r}")
with file.open(mode="w") as f:
_write_yaml_model(f, model, **write_kwargs)
return
def parse_yaml_raw_as(model_type: Type[T], raw: Union[str, bytes, IOBase]) -> T:
"""Parse raw YAML string as the passed model type.
Parameters
----------
model_type : Type[BaseModel]
The resulting model type.
raw : str or bytes or IOBase
The YAML string or stream.
"""
stream: IOBase
if isinstance(raw, str):
stream = StringIO(raw)
elif isinstance(raw, bytes):
stream = BytesIO(raw)
elif isinstance(raw, IOBase):
stream = raw
else:
raise TypeError(f"Expected str, bytes or IO, but got {raw!r}")
reader = YAML(typ="safe", pure=True) # YAML 1.2 support
objects = reader.load(stream)
if isinstance(model_type, type) and issubclass(model_type, BaseModelV1):
return parse_obj_as(model_type, objects) # type:ignore
else:
ta = TypeAdapter(model_type) # type: ignore
return ta.validate_python(objects)
def parse_yaml_file_as(model_type: Type[T], file: Union[Path, str, IOBase]) -> T:
"""Parse YAML file as the passed model type.
Parameters
----------
model_type : Type[BaseModel]
The resulting model type.
file : Path or str or IOBase
The file path or stream to read from.
"""
# Short-circuit
if isinstance(file, IOBase):
return parse_yaml_raw_as(model_type, raw=file)
if isinstance(file, str):
file = Path(file).resolve()
elif isinstance(file, Path):
file = file.resolve()
else:
raise TypeError(f"Expected Path, str or IO, but got {file!r}")
with file.open(mode="r") as f:
return parse_yaml_raw_as(model_type, f)