11from pathlib import Path
22from typing import Optional , Union
33
4+ import black
45import click
56import httpx
67from httpx import ConnectError , ConnectTimeout
910import autopep8
1011
1112from openapi_schema_pydantic import OpenAPI
12- from .common import HTTPLibrary , library_config_dict
13+ from .common import HTTPLibrary , library_config_dict , AutoFormat
1314from .language_converters .python .generator import generator
1415from .language_converters .python .jinja_config import JINJA_ENV , SERVICE_TEMPLATE
1516from .models import ConversionResult
1617
1718
18- def write_code (path : Path , content ):
19+ def write_code (
20+ path : Path , content , autoformat : Optional [AutoFormat ] = AutoFormat .black
21+ ) -> None :
1922 """
2023 Write the content to the file at the given path.
24+ :param autoformat: The autoformat applied to the code written.
2125 :param path: The path to the file.
2226 :param content: The content to write.
2327 """
2428 with open (path , "w" ) as f :
25- f .write (autopep8 .fix_code (content ))
29+ if autoformat == AutoFormat .black :
30+ f .write (
31+ black .format_file_contents (
32+ content , fast = False , mode = black .FileMode (line_length = 120 )
33+ )
34+ )
35+ elif autoformat == AutoFormat .autopep8 :
36+ f .write (autopep8 .fix_code (content , options = {"max_line_length" : 120 }))
37+ else :
38+ f .write (content )
2639
2740
2841def get_open_api (source : Union [str , Path ]) -> OpenAPI :
@@ -54,11 +67,16 @@ def get_open_api(source: Union[str, Path]) -> OpenAPI:
5467 raise
5568
5669
57- def write_data (data : ConversionResult , output : Union [str , Path ]):
70+ def write_data (
71+ data : ConversionResult ,
72+ output : Union [str , Path ],
73+ autoformat : Optional [AutoFormat ] = AutoFormat .black ,
74+ ) -> None :
5875 """
5976 This function will firstly create the folderstrucutre of output, if it doesn't exist. Then it will create the
6077 models from data.models into the models sub module of the output folder. After this, the services will be created
6178 into the services sub module of the output folder.
79+ :param autoformat: The autoformat applied to the code written.
6280 :param data: The data to write.
6381 :param output: The path to the output folder.
6482 """
@@ -79,12 +97,13 @@ def write_data(data: ConversionResult, output: Union[str, Path]):
7997 # Write the models.
8098 for model in data .models :
8199 files .append (model .file_name )
82- write_code (models_path / f"{ model .file_name } .py" , model .content )
100+ write_code (models_path / f"{ model .file_name } .py" , model .content , autoformat )
83101
84102 # Create models.__init__.py file containing imports to all models.
85103 write_code (
86104 models_path / "__init__.py" ,
87105 "\n " .join ([f"from .{ file } import *" for file in files ]),
106+ autoformat ,
88107 )
89108
90109 files = []
@@ -97,33 +116,37 @@ def write_data(data: ConversionResult, output: Union[str, Path]):
97116 write_code (
98117 services_path / f"{ service .file_name } .py" ,
99118 JINJA_ENV .get_template (SERVICE_TEMPLATE ).render (** service .dict ()),
119+ autoformat ,
100120 )
101121
102122 # Create services.__init__.py file containing imports to all services.
103123 write_code (
104124 services_path / "__init__.py" ,
105125 "\n " .join ([f"from .{ file } import *" for file in files ]),
126+ autoformat ,
106127 )
107128
108129 # Write the api_config.py file.
109- write_code (Path (output ) / "api_config.py" , data .api_config .content )
130+ write_code (Path (output ) / "api_config.py" , data .api_config .content , autoformat )
110131
111132 # Write the __init__.py file.
112133 write_code (
113134 Path (output ) / "__init__.py" ,
114135 "from .models import *\n from .services import *\n from .api_config import *" ,
136+ autoformat ,
115137 )
116138
117139
118140def generate_data (
119141 source : Union [str , Path ],
120142 output : Union [str , Path ],
121143 library : Optional [HTTPLibrary ] = HTTPLibrary .httpx ,
144+ autoformat : Optional [AutoFormat ] = AutoFormat .black ,
122145) -> None :
123146 """
124147 Generate Python code from an OpenAPI 3.0 specification.
125148 """
126149 data = get_open_api (source )
127150 click .echo (f"Generating data from { source } " )
128151 result = generator (data , library_config_dict [library ])
129- write_data (result , output )
152+ write_data (result , output , autoformat )
0 commit comments