11from re import Pattern
2- from typing import Dict , List , Union , Tuple
2+ from typing import Dict , List , Union
33import logging
44import re
55
66from .plugin import Document , Init
77
8- class DocumentReduced (Document ):
8+
9+ class Reduce (Document , Init ):
910 log = logging .getLogger ("aiopenapi3.extra.Reduced" )
1011
1112 def __init__ (self , operations : Dict [Union [str , Pattern ], List [Union [str , Pattern ]]]):
12- self .operations = operations
13+ self .operations : List [ Union [ str , Pattern ]] = operations
1314 super ().__init__ ()
14-
15- def _reduced_paths (self , ctx : "Document.Context" ) -> "Document.Context" :
15+
16+ def _reduced_paths (self , ctx : "Document.Context" ) -> dict :
1617 return {
1718 key : {
1819 operation_key : operation_value
@@ -35,7 +36,43 @@ def _reduced_paths(self, ctx: "Document.Context") -> "Document.Context":
3536 }.items ()
3637 }
3738
38- class Culled (DocumentReduced ):
39+ def parsed (self , ctx : "Document.Context" ) -> "Document.Context" :
40+ """Parse the given context."""
41+ ctx .document ["paths" ] = self ._reduced_paths (ctx )
42+ return ctx
43+
44+ def paths (self , ctx : "Init.Context" ) -> "Init.Context" :
45+ """Clear the paths of the context."""
46+ ctx .paths = None
47+ return ctx
48+
49+ def initialized (self , ctx : "Init.Context" ) -> "Init.Context" :
50+ """Process the initialized context."""
51+ for name , parameter in list (ctx .initialized .components .parameters .items ()):
52+ if parameter .schema_ ._model_type is None :
53+ del ctx .initialized .components .parameters [name ]
54+ break
55+
56+ for name , schema in list (ctx .initialized .components .schemas .items ()):
57+ if schema ._model_type is None :
58+ del ctx .initialized .components .schemas [name ]
59+ break
60+
61+ for name , response in list (ctx .initialized .components .responses .items ()):
62+ for k , v in response .content .items ():
63+ if v .schema_ ._model_type is None :
64+ del ctx .initialized .components .responses [name ]
65+ break
66+
67+ for name , requestBody in list (ctx .initialized .components .requestBodies .items ()):
68+ for k , v in requestBody .content .items ():
69+ if v .schema_ ._model_type is None :
70+ del ctx .initialized .components .requestBodies [name ]
71+ break
72+ return ctx
73+
74+
75+ class Cull (Reduce ):
3976 @staticmethod
4077 def _extract_references (data , root = None ):
4178 """
@@ -104,42 +141,3 @@ def parsed(self, ctx: "Document.Context") -> "Document.Context":
104141
105142 ctx .document = document
106143 return ctx
107-
108- class LazyLoaded (DocumentReduced , Init ):
109- def parsed (self , ctx : "Document.Context" ) -> "Document.Context" :
110- """Parse the given context."""
111- ctx .document ["paths" ] = self ._reduced_paths (ctx )
112- return ctx
113-
114- def paths (self , ctx : "Init.Context" ) -> "Init.Context" :
115- """Clear the paths of the context."""
116- ctx .paths = None
117- return ctx
118-
119- def initialized (self , ctx : "Init.Context" ) -> "Init.Context" :
120- """Process the initialized context."""
121- for name , parameter in list (ctx .initialized .components .parameters .items ()):
122- if parameter .schema_ ._model_type is None :
123- del ctx .initialized .components .parameters [name ]
124- break
125-
126- for name , schema in list (ctx .initialized .components .schemas .items ()):
127- if schema ._model_type is None :
128- del ctx .initialized .components .schemas [name ]
129- break
130-
131- for name , response in list (ctx .initialized .components .responses .items ()):
132- for k , v in response .content .items ():
133- if v .schema_ ._model_type is None :
134- del ctx .initialized .components .responses [name ]
135- break
136-
137- for name , requestBody in list (ctx .initialized .components .requestBodies .items ()):
138- for k , v in requestBody .content .items ():
139- if v .schema_ ._model_type is None :
140- del ctx .initialized .components .requestBodies [name ]
141- break
142- return ctx
143-
144- class Reduced (Culled , LazyLoaded ):
145- pass
0 commit comments