forked from yukinarit/pyserde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamlfile.py
85 lines (64 loc) · 1.45 KB
/
yamlfile.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
"""
yamlfile.py
Read swagger echo example yaml.
Usage:
$ poetry install
$ poetry run python yamlfile.py
"""
from dataclasses import dataclass
from typing import Dict, List, Optional, Union
from serde import Untagged, serde
from serde.yaml import from_yaml
@serde(rename_all='camelcase')
@dataclass
class Info:
title: str
description: str
version: str
@serde(rename_all='camelcase')
@dataclass
class Parameter:
name: str
# not yet supported.
# infield: str = field(rename='in')
type: str
required: bool
@serde(rename_all='camelcase')
@dataclass
class Response:
description: str
@serde(rename_all='camelcase', tagging=Untagged)
@dataclass
class Path:
description: str
operation_id: str
parameters: List[Union[str, Parameter]]
responses: Dict[Union[str, int], Response]
@serde(rename_all='camelcase')
@dataclass
class Prop:
type: str
format: Optional[str]
@serde(rename_all='camelcase')
@dataclass
class Definition:
required: Optional[List[str]]
properties: Dict[str, Prop]
@serde(rename_all='camelcase')
@dataclass
class Swagger:
swagger: int
info: Info
host: str
schemes: List[str]
base_path = str
produces: List[str]
paths: Dict[str, Dict[str, Path]]
definitions: Dict[str, Definition]
def main():
with open('swagger.yml') as f:
yaml = f.read()
swagger = from_yaml(Swagger, yaml)
print(swagger)
if __name__ == '__main__':
main()