@@ -44,6 +44,7 @@ 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
+ encoding : Optional [str ] = None
47
48
48
49
def __init__ (self , * , openapi : GeneratorData , meta : MetaType , custom_template_path : Optional [Path ] = None ) -> None :
49
50
self .openapi : GeneratorData = openapi
@@ -137,15 +138,17 @@ def _create_package(self) -> None:
137
138
package_init = self .package_dir / "__init__.py"
138
139
139
140
package_init_template = self .env .get_template ("package_init.py.jinja" )
140
- package_init .write_text (package_init_template .render (description = self .package_description ))
141
+ package_init .write_text (
142
+ package_init_template .render (description = self .package_description ), encoding = self .encoding
143
+ )
141
144
142
145
if self .meta != MetaType .NONE :
143
146
pytyped = self .package_dir / "py.typed"
144
- pytyped .write_text ("# Marker file for PEP 561" )
147
+ pytyped .write_text ("# Marker file for PEP 561" , encoding = self . encoding )
145
148
146
149
types_template = self .env .get_template ("types.py.jinja" )
147
150
types_path = self .package_dir / "types.py"
148
- types_path .write_text (types_template .render ())
151
+ types_path .write_text (types_template .render (), encoding = self . encoding )
149
152
150
153
def _build_metadata (self ) -> None :
151
154
if self .meta == MetaType .NONE :
@@ -167,7 +170,7 @@ def _build_metadata(self) -> None:
167
170
# .gitignore
168
171
git_ignore_path = self .project_dir / ".gitignore"
169
172
git_ignore_template = self .env .get_template (".gitignore.jinja" )
170
- git_ignore_path .write_text (git_ignore_template .render ())
173
+ git_ignore_path .write_text (git_ignore_template .render (), encoding = self . encoding )
171
174
172
175
def _build_pyproject_toml (self , * , use_poetry : bool ) -> None :
173
176
template = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja"
@@ -204,7 +207,7 @@ def _build_models(self) -> None:
204
207
model_template = self .env .get_template ("model.py.jinja" )
205
208
for model in self .openapi .models .values ():
206
209
module_path = models_dir / f"{ model .reference .module_name } .py"
207
- module_path .write_text (model_template .render (model = model ))
210
+ module_path .write_text (model_template .render (model = model ), encoding = self . encoding )
208
211
imports .append (import_string_from_reference (model .reference ))
209
212
210
213
# Generate enums
@@ -213,25 +216,25 @@ def _build_models(self) -> None:
213
216
for enum in self .openapi .enums .values ():
214
217
module_path = models_dir / f"{ enum .reference .module_name } .py"
215
218
if enum .value_type is int :
216
- module_path .write_text (int_enum_template .render (enum = enum ))
219
+ module_path .write_text (int_enum_template .render (enum = enum ), encoding = self . encoding )
217
220
else :
218
- module_path .write_text (str_enum_template .render (enum = enum ))
221
+ module_path .write_text (str_enum_template .render (enum = enum ), encoding = self . encoding )
219
222
imports .append (import_string_from_reference (enum .reference ))
220
223
221
224
models_init_template = self .env .get_template ("models_init.py.jinja" )
222
- models_init .write_text (models_init_template .render (imports = imports ))
225
+ models_init .write_text (models_init_template .render (imports = imports ), encoding = self . encoding )
223
226
224
227
def _build_api (self ) -> None :
225
228
# Generate Client
226
229
client_path = self .package_dir / "client.py"
227
230
client_template = self .env .get_template ("client.py.jinja" )
228
- client_path .write_text (client_template .render ())
231
+ client_path .write_text (client_template .render (), encoding = self . encoding )
229
232
230
233
# Generate endpoints
231
234
api_dir = self .package_dir / "api"
232
235
api_dir .mkdir ()
233
236
api_init = api_dir / "__init__.py"
234
- api_init .write_text ('""" Contains methods for accessing the API """' )
237
+ api_init .write_text ('""" Contains methods for accessing the API """' , encoding = self . encoding )
235
238
236
239
endpoint_template = self .env .get_template ("endpoint_module.py.jinja" )
237
240
for tag , collection in self .openapi .endpoint_collections_by_tag .items ():
@@ -242,7 +245,7 @@ def _build_api(self) -> None:
242
245
243
246
for endpoint in collection .endpoints :
244
247
module_path = tag_dir / f"{ snake_case (endpoint .name )} .py"
245
- module_path .write_text (endpoint_template .render (endpoint = endpoint ))
248
+ module_path .write_text (endpoint_template .render (endpoint = endpoint ), encoding = self . encoding )
246
249
247
250
248
251
def _get_project_for_url_or_path (
@@ -258,7 +261,12 @@ def _get_project_for_url_or_path(
258
261
259
262
260
263
def create_new_client (
261
- * , url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
264
+ * ,
265
+ url : Optional [str ],
266
+ path : Optional [Path ],
267
+ meta : MetaType ,
268
+ custom_template_path : Optional [Path ] = None ,
269
+ encoding = Optional [str ],
262
270
) -> Sequence [GeneratorError ]:
263
271
"""
264
272
Generate the client library
@@ -267,6 +275,7 @@ def create_new_client(
267
275
A list containing any errors encountered when generating.
268
276
"""
269
277
project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path , meta = meta )
278
+ project .encoding = encoding
270
279
if isinstance (project , GeneratorError ):
271
280
return [project ]
272
281
return project .build ()
0 commit comments