1
1
from pathlib import Path
2
+ from typing import List
2
3
from typing import Optional
3
4
from typing import Union
4
5
14
15
from openapi_pydantic .v3 .v3_0 import OpenAPI
15
16
from pydantic import ValidationError
16
17
17
- from .common import HTTPLibrary , PydanticVersion
18
+ from .common import FormatOptions , Formatter , HTTPLibrary , PydanticVersion
18
19
from .common import library_config_dict
19
20
from .language_converters .python .generator import generator
20
21
from .language_converters .python .jinja_config import SERVICE_TEMPLATE
21
22
from .language_converters .python .jinja_config import create_jinja_env
22
23
from .models import ConversionResult
23
24
24
25
25
- def write_code (path : Path , content ) -> None :
26
+ def write_code (path : Path , content : str , formatter : Formatter ) -> None :
26
27
"""
27
28
Write the content to the file at the given path.
28
- :param autoformat: The autoformat applied to the code written.
29
29
:param path: The path to the file.
30
30
:param content: The content to write.
31
+ :param formatter: The formatter applied to the code written.
31
32
"""
32
- try :
33
- with open (path , "w" ) as f :
34
- try :
35
- formatted_contend = black .format_file_contents (
36
- content , fast = False , mode = black .FileMode (line_length = 120 )
37
- )
33
+ if formatter == Formatter .BLACK :
34
+ formatted_contend = format_using_black (content )
35
+ elif formatter == Formatter .NONE :
36
+ formatted_contend = content
37
+ else :
38
+ raise NotImplementedError (f"Missing implementation for formatter { formatter !r} ." )
39
+ with open (path , "w" ) as f :
40
+ f .write (formatted_contend )
41
+
38
42
39
- except NothingChanged :
40
- formatted_contend = content
41
- formatted_contend = isort .code (formatted_contend , line_length = 120 )
42
- f .write (formatted_contend )
43
- except Exception as e :
44
- raise e
43
+ def format_using_black (content : str ) -> str :
44
+ try :
45
+ formatted_contend = black .format_file_contents (
46
+ content , fast = FormatOptions .skip_validation , mode = black .FileMode (line_length = FormatOptions .line_length )
47
+ )
48
+ except NothingChanged :
49
+ return content
50
+ return isort .code (formatted_contend , line_length = FormatOptions .line_length )
45
51
46
52
47
53
def get_open_api (source : Union [str , Path ]) -> OpenAPI :
@@ -105,14 +111,14 @@ def get_open_api(source: Union[str, Path]) -> OpenAPI:
105
111
raise
106
112
107
113
108
- def write_data (data : ConversionResult , output : Union [str , Path ]) -> None :
114
+ def write_data (data : ConversionResult , output : Union [str , Path ], formatter : Formatter ) -> None :
109
115
"""
110
- This function will firstly create the folderstrucutre of output, if it doesn't exist. Then it will create the
116
+ This function will firstly create the folder structure of output, if it doesn't exist. Then it will create the
111
117
models from data.models into the models sub module of the output folder. After this, the services will be created
112
118
into the services sub module of the output folder.
113
- :param autoformat: The autoformat applied to the code written.
114
119
:param data: The data to write.
115
120
:param output: The path to the output folder.
121
+ :param formatter: The formatter applied to the code written.
116
122
"""
117
123
118
124
# Create the folder structure of the output folder.
@@ -126,17 +132,18 @@ def write_data(data: ConversionResult, output: Union[str, Path]) -> None:
126
132
services_path = Path (output ) / "services"
127
133
services_path .mkdir (parents = True , exist_ok = True )
128
134
129
- files = []
135
+ files : List [ str ] = []
130
136
131
137
# Write the models.
132
138
for model in data .models :
133
139
files .append (model .file_name )
134
- write_code (models_path / f"{ model .file_name } .py" , model .content )
140
+ write_code (models_path / f"{ model .file_name } .py" , model .content , formatter )
135
141
136
142
# Create models.__init__.py file containing imports to all models.
137
143
write_code (
138
144
models_path / "__init__.py" ,
139
145
"\n " .join ([f"from .{ file } import *" for file in files ]),
146
+ formatter ,
140
147
)
141
148
142
149
files = []
@@ -150,18 +157,20 @@ def write_data(data: ConversionResult, output: Union[str, Path]) -> None:
150
157
write_code (
151
158
services_path / f"{ service .file_name } .py" ,
152
159
jinja_env .get_template (SERVICE_TEMPLATE ).render (** service .dict ()),
160
+ formatter ,
153
161
)
154
162
155
163
# Create services.__init__.py file containing imports to all services.
156
- write_code (services_path / "__init__.py" , "" )
164
+ write_code (services_path / "__init__.py" , "" , formatter )
157
165
158
166
# Write the api_config.py file.
159
- write_code (Path (output ) / "api_config.py" , data .api_config .content )
167
+ write_code (Path (output ) / "api_config.py" , data .api_config .content , formatter )
160
168
161
169
# Write the __init__.py file.
162
170
write_code (
163
171
Path (output ) / "__init__.py" ,
164
172
"from .models import *\n from .services import *\n from .api_config import *" ,
173
+ formatter ,
165
174
)
166
175
167
176
@@ -173,6 +182,7 @@ def generate_data(
173
182
use_orjson : bool = False ,
174
183
custom_template_path : Optional [str ] = None ,
175
184
pydantic_version : PydanticVersion = PydanticVersion .V2 ,
185
+ formatter : Formatter = Formatter .BLACK ,
176
186
) -> None :
177
187
"""
178
188
Generate Python code from an OpenAPI 3.0 specification.
@@ -189,4 +199,4 @@ def generate_data(
189
199
pydantic_version ,
190
200
)
191
201
192
- write_data (result , output )
202
+ write_data (result , output , formatter )
0 commit comments