@@ -44,10 +44,19 @@ class Project:
44
44
project_name_override : Optional [str ] = None
45
45
package_name_override : Optional [str ] = None
46
46
package_version_override : Optional [str ] = None
47
-
48
- def __init__ (self , * , openapi : GeneratorData , meta : MetaType , custom_template_path : Optional [Path ] = None ) -> None :
47
+ encoding : Optional [str ] = None
48
+
49
+ def __init__ (
50
+ self ,
51
+ * ,
52
+ openapi : GeneratorData ,
53
+ meta : MetaType ,
54
+ custom_template_path : Optional [Path ] = None ,
55
+ encoding : Optional [str ] = None ,
56
+ ) -> None :
49
57
self .openapi : GeneratorData = openapi
50
58
self .meta : MetaType = meta
59
+ self .encoding = encoding
51
60
52
61
package_loader = PackageLoader (__package__ )
53
62
loader : BaseLoader
@@ -137,15 +146,17 @@ def _create_package(self) -> None:
137
146
package_init = self .package_dir / "__init__.py"
138
147
139
148
package_init_template = self .env .get_template ("package_init.py.jinja" )
140
- package_init .write_text (package_init_template .render (description = self .package_description ))
149
+ package_init .write_text (
150
+ package_init_template .render (description = self .package_description ), encoding = self .encoding
151
+ )
141
152
142
153
if self .meta != MetaType .NONE :
143
154
pytyped = self .package_dir / "py.typed"
144
- pytyped .write_text ("# Marker file for PEP 561" )
155
+ pytyped .write_text ("# Marker file for PEP 561" , encoding = self . encoding )
145
156
146
157
types_template = self .env .get_template ("types.py.jinja" )
147
158
types_path = self .package_dir / "types.py"
148
- types_path .write_text (types_template .render ())
159
+ types_path .write_text (types_template .render (), encoding = self . encoding )
149
160
150
161
def _build_metadata (self ) -> None :
151
162
if self .meta == MetaType .NONE :
@@ -167,7 +178,7 @@ def _build_metadata(self) -> None:
167
178
# .gitignore
168
179
git_ignore_path = self .project_dir / ".gitignore"
169
180
git_ignore_template = self .env .get_template (".gitignore.jinja" )
170
- git_ignore_path .write_text (git_ignore_template .render ())
181
+ git_ignore_path .write_text (git_ignore_template .render (), encoding = self . encoding )
171
182
172
183
def _build_pyproject_toml (self , * , use_poetry : bool ) -> None :
173
184
template = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja"
@@ -204,7 +215,7 @@ def _build_models(self) -> None:
204
215
model_template = self .env .get_template ("model.py.jinja" )
205
216
for model in self .openapi .models .values ():
206
217
module_path = models_dir / f"{ model .reference .module_name } .py"
207
- module_path .write_text (model_template .render (model = model ))
218
+ module_path .write_text (model_template .render (model = model ), encoding = self . encoding )
208
219
imports .append (import_string_from_reference (model .reference ))
209
220
210
221
# Generate enums
@@ -213,25 +224,25 @@ def _build_models(self) -> None:
213
224
for enum in self .openapi .enums .values ():
214
225
module_path = models_dir / f"{ enum .reference .module_name } .py"
215
226
if enum .value_type is int :
216
- module_path .write_text (int_enum_template .render (enum = enum ))
227
+ module_path .write_text (int_enum_template .render (enum = enum ), encoding = self . encoding )
217
228
else :
218
- module_path .write_text (str_enum_template .render (enum = enum ))
229
+ module_path .write_text (str_enum_template .render (enum = enum ), encoding = self . encoding )
219
230
imports .append (import_string_from_reference (enum .reference ))
220
231
221
232
models_init_template = self .env .get_template ("models_init.py.jinja" )
222
- models_init .write_text (models_init_template .render (imports = imports ))
233
+ models_init .write_text (models_init_template .render (imports = imports ), encoding = self . encoding )
223
234
224
235
def _build_api (self ) -> None :
225
236
# Generate Client
226
237
client_path = self .package_dir / "client.py"
227
238
client_template = self .env .get_template ("client.py.jinja" )
228
- client_path .write_text (client_template .render ())
239
+ client_path .write_text (client_template .render (), encoding = self . encoding )
229
240
230
241
# Generate endpoints
231
242
api_dir = self .package_dir / "api"
232
243
api_dir .mkdir ()
233
244
api_init = api_dir / "__init__.py"
234
- api_init .write_text ('""" Contains methods for accessing the API """' )
245
+ api_init .write_text ('""" Contains methods for accessing the API """' , encoding = self . encoding )
235
246
236
247
endpoint_template = self .env .get_template ("endpoint_module.py.jinja" )
237
248
for tag , collection in self .openapi .endpoint_collections_by_tag .items ():
@@ -242,31 +253,42 @@ def _build_api(self) -> None:
242
253
243
254
for endpoint in collection .endpoints :
244
255
module_path = tag_dir / f"{ snake_case (endpoint .name )} .py"
245
- module_path .write_text (endpoint_template .render (endpoint = endpoint ))
256
+ module_path .write_text (endpoint_template .render (endpoint = endpoint ), encoding = self . encoding )
246
257
247
258
248
259
def _get_project_for_url_or_path (
249
- url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
260
+ url : Optional [str ],
261
+ path : Optional [Path ],
262
+ meta : MetaType ,
263
+ custom_template_path : Optional [Path ] = None ,
264
+ encoding : Optional [str ] = None ,
250
265
) -> Union [Project , GeneratorError ]:
251
266
data_dict = _get_document (url = url , path = path )
252
267
if isinstance (data_dict , GeneratorError ):
253
268
return data_dict
254
269
openapi = GeneratorData .from_dict (data_dict )
255
270
if isinstance (openapi , GeneratorError ):
256
271
return openapi
257
- return Project (openapi = openapi , custom_template_path = custom_template_path , meta = meta )
272
+ return Project (openapi = openapi , custom_template_path = custom_template_path , meta = meta , encoding = encoding )
258
273
259
274
260
275
def create_new_client (
261
- * , url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
276
+ * ,
277
+ url : Optional [str ],
278
+ path : Optional [Path ],
279
+ meta : MetaType ,
280
+ custom_template_path : Optional [Path ] = None ,
281
+ encoding : Optional [str ] = None ,
262
282
) -> Sequence [GeneratorError ]:
263
283
"""
264
284
Generate the client library
265
285
266
286
Returns:
267
287
A list containing any errors encountered when generating.
268
288
"""
269
- project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path , meta = meta )
289
+ project = _get_project_for_url_or_path (
290
+ url = url , path = path , custom_template_path = custom_template_path , meta = meta , encoding = encoding
291
+ )
270
292
if isinstance (project , GeneratorError ):
271
293
return [project ]
272
294
return project .build ()
0 commit comments