14
14
15
15
from openapi_python_client import utils
16
16
17
- from .parser import GeneratorData , import_string_from_reference
17
+ from .config import Config
18
+ from .parser import GeneratorData , import_string_from_class
18
19
from .parser .errors import GeneratorError
19
20
from .utils import snake_case
20
21
@@ -41,15 +42,12 @@ class MetaType(str, Enum):
41
42
42
43
43
44
class Project :
44
- project_name_override : Optional [str ] = None
45
- package_name_override : Optional [str ] = None
46
- package_version_override : Optional [str ] = None
47
-
48
45
def __init__ (
49
46
self ,
50
47
* ,
51
48
openapi : GeneratorData ,
52
49
meta : MetaType ,
50
+ config : Config ,
53
51
custom_template_path : Optional [Path ] = None ,
54
52
file_encoding : str = "utf-8" ,
55
53
) -> None :
@@ -70,17 +68,17 @@ def __init__(
70
68
loader = package_loader
71
69
self .env : Environment = Environment (loader = loader , trim_blocks = True , lstrip_blocks = True )
72
70
73
- self .project_name : str = self .project_name_override or f"{ utils .kebab_case (openapi .title ).lower ()} -client"
71
+ self .project_name : str = config .project_name_override or f"{ utils .kebab_case (openapi .title ).lower ()} -client"
74
72
self .project_dir : Path = Path .cwd ()
75
73
if meta != MetaType .NONE :
76
74
self .project_dir /= self .project_name
77
75
78
- self .package_name : str = self .package_name_override or self .project_name .replace ("-" , "_" )
76
+ self .package_name : str = config .package_name_override or self .project_name .replace ("-" , "_" )
79
77
self .package_dir : Path = self .project_dir / self .package_name
80
78
self .package_description : str = utils .remove_string_escapes (
81
79
f"A client library for accessing { self .openapi .title } "
82
80
)
83
- self .version : str = self .package_version_override or openapi .version
81
+ self .version : str = config .package_version_override or openapi .version
84
82
85
83
self .env .filters .update (TEMPLATE_FILTERS )
86
84
@@ -215,21 +213,21 @@ def _build_models(self) -> None:
215
213
imports = []
216
214
217
215
model_template = self .env .get_template ("model.py.jinja" )
218
- for model in self .openapi .models . values () :
219
- module_path = models_dir / f"{ model .reference .module_name } .py"
216
+ for model in self .openapi .models :
217
+ module_path = models_dir / f"{ model .class_info .module_name } .py"
220
218
module_path .write_text (model_template .render (model = model ), encoding = self .file_encoding )
221
- imports .append (import_string_from_reference (model .reference ))
219
+ imports .append (import_string_from_class (model .class_info ))
222
220
223
221
# Generate enums
224
222
str_enum_template = self .env .get_template ("str_enum.py.jinja" )
225
223
int_enum_template = self .env .get_template ("int_enum.py.jinja" )
226
- for enum in self .openapi .enums . values () :
227
- module_path = models_dir / f"{ enum .reference .module_name } .py"
224
+ for enum in self .openapi .enums :
225
+ module_path = models_dir / f"{ enum .class_info .module_name } .py"
228
226
if enum .value_type is int :
229
227
module_path .write_text (int_enum_template .render (enum = enum ), encoding = self .file_encoding )
230
228
else :
231
229
module_path .write_text (str_enum_template .render (enum = enum ), encoding = self .file_encoding )
232
- imports .append (import_string_from_reference (enum .reference ))
230
+ imports .append (import_string_from_class (enum .class_info ))
233
231
234
232
models_init_template = self .env .get_template ("models_init.py.jinja" )
235
233
models_init .write_text (models_init_template .render (imports = imports ), encoding = self .file_encoding )
@@ -261,23 +259,31 @@ def _get_project_for_url_or_path(
261
259
url : Optional [str ],
262
260
path : Optional [Path ],
263
261
meta : MetaType ,
262
+ config : Config ,
264
263
custom_template_path : Optional [Path ] = None ,
265
264
file_encoding : str = "utf-8" ,
266
265
) -> Union [Project , GeneratorError ]:
267
266
data_dict = _get_document (url = url , path = path )
268
267
if isinstance (data_dict , GeneratorError ):
269
268
return data_dict
270
- openapi = GeneratorData .from_dict (data_dict )
269
+ openapi = GeneratorData .from_dict (data_dict , config = config )
271
270
if isinstance (openapi , GeneratorError ):
272
271
return openapi
273
- return Project (openapi = openapi , custom_template_path = custom_template_path , meta = meta , file_encoding = file_encoding )
272
+ return Project (
273
+ openapi = openapi ,
274
+ custom_template_path = custom_template_path ,
275
+ meta = meta ,
276
+ file_encoding = file_encoding ,
277
+ config = config ,
278
+ )
274
279
275
280
276
281
def create_new_client (
277
282
* ,
278
283
url : Optional [str ],
279
284
path : Optional [Path ],
280
285
meta : MetaType ,
286
+ config : Config ,
281
287
custom_template_path : Optional [Path ] = None ,
282
288
file_encoding : str = "utf-8" ,
283
289
) -> Sequence [GeneratorError ]:
@@ -288,7 +294,12 @@ def create_new_client(
288
294
A list containing any errors encountered when generating.
289
295
"""
290
296
project = _get_project_for_url_or_path (
291
- url = url , path = path , custom_template_path = custom_template_path , meta = meta , file_encoding = file_encoding
297
+ url = url ,
298
+ path = path ,
299
+ custom_template_path = custom_template_path ,
300
+ meta = meta ,
301
+ file_encoding = file_encoding ,
302
+ config = config ,
292
303
)
293
304
if isinstance (project , GeneratorError ):
294
305
return [project ]
@@ -300,6 +311,7 @@ def update_existing_client(
300
311
url : Optional [str ],
301
312
path : Optional [Path ],
302
313
meta : MetaType ,
314
+ config : Config ,
303
315
custom_template_path : Optional [Path ] = None ,
304
316
file_encoding : str = "utf-8" ,
305
317
) -> Sequence [GeneratorError ]:
@@ -310,7 +322,12 @@ def update_existing_client(
310
322
A list containing any errors encountered when generating.
311
323
"""
312
324
project = _get_project_for_url_or_path (
313
- url = url , path = path , custom_template_path = custom_template_path , meta = meta , file_encoding = file_encoding
325
+ url = url ,
326
+ path = path ,
327
+ custom_template_path = custom_template_path ,
328
+ meta = meta ,
329
+ file_encoding = file_encoding ,
330
+ config = config ,
314
331
)
315
332
if isinstance (project , GeneratorError ):
316
333
return [project ]
0 commit comments