1
+ from typing import (
2
+ MutableMapping ,
3
+ Mapping ,
4
+ )
1
5
from collections import defaultdict
2
- from os .path import abspath , dirname , join
6
+ from os .path import (
7
+ abspath ,
8
+ dirname ,
9
+ join ,
10
+ )
3
11
4
12
import yaml
5
13
from aiohttp import web
8
16
9
17
try :
10
18
import ujson as json
11
- except ImportError : # pragma: no cover
19
+ except ImportError : # pragma: no cover
12
20
import json
13
21
14
22
@@ -36,37 +44,41 @@ def _extract_swagger_docs(end_point_doc, method="get"):
36
44
}
37
45
return {method : end_point_swagger_doc }
38
46
47
+
39
48
def _build_doc_from_func_doc (route ):
40
49
41
50
out = {}
42
51
43
52
if issubclass (route .handler , web .View ) and route .method == METH_ANY :
44
53
method_names = {
45
- attr for attr in dir (route .handler ) \
54
+ attr for attr in dir (route .handler )
46
55
if attr .upper () in METH_ALL
47
56
}
48
57
for method_name in method_names :
49
58
method = getattr (route .handler , method_name )
50
59
if method .__doc__ is not None and "---" in method .__doc__ :
51
60
end_point_doc = method .__doc__ .splitlines ()
52
- out .update (_extract_swagger_docs (end_point_doc , method = method_name ))
61
+ out .update (
62
+ _extract_swagger_docs (end_point_doc , method = method_name ))
53
63
54
64
else :
55
65
try :
56
66
end_point_doc = route .handler .__doc__ .splitlines ()
57
67
except AttributeError :
58
68
return {}
59
- out .update (_extract_swagger_docs (end_point_doc ))
69
+ out .update (_extract_swagger_docs (
70
+ end_point_doc , method = route .method .lower ()))
60
71
return out
61
72
73
+
62
74
def generate_doc_from_each_end_point (
63
75
app : web .Application ,
64
76
* ,
65
77
api_base_url : str = "/" ,
66
78
description : str = "Swagger API definition" ,
67
79
api_version : str = "1.0.0" ,
68
80
title : str = "Swagger API" ,
69
- contact : str = "" ):
81
+ contact : str = "" ) -> MutableMapping :
70
82
# Clean description
71
83
_start_desc = 0
72
84
for i , word in enumerate (description ):
@@ -92,8 +104,6 @@ def generate_doc_from_each_end_point(
92
104
93
105
for route in app .router .routes ():
94
106
95
- end_point_doc = None
96
-
97
107
# If route has a external link to doc, we use it, not function doc
98
108
if getattr (route .handler , "swagger_file" , False ):
99
109
try :
@@ -133,13 +143,14 @@ def generate_doc_from_each_end_point(
133
143
url = url_info .get ("formatter" )
134
144
135
145
swagger ["paths" ][url ].update (end_point_doc )
136
-
137
- return json .dumps (swagger )
146
+ return swagger
138
147
139
148
140
- def load_doc_from_yaml_file (doc_path : str ):
141
- loaded_yaml = yaml .load (open (doc_path , "r" ).read ())
142
- return json .dumps (loaded_yaml )
149
+ def load_doc_from_yaml_file (doc_path : str ) -> MutableMapping :
150
+ return yaml .load (open (doc_path , "r" ).read ())
143
151
144
152
145
- __all__ = ("generate_doc_from_each_end_point" , "load_doc_from_yaml_file" )
153
+ __all__ = (
154
+ "generate_doc_from_each_end_point" ,
155
+ "load_doc_from_yaml_file"
156
+ )
0 commit comments