@@ -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 :
@@ -161,13 +172,14 @@ def _build_metadata(self) -> None:
161
172
readme .write_text (
162
173
readme_template .render (
163
174
project_name = self .project_name , description = self .package_description , package_name = self .package_name
164
- )
175
+ ),
176
+ encoding = self .encoding ,
165
177
)
166
178
167
179
# .gitignore
168
180
git_ignore_path = self .project_dir / ".gitignore"
169
181
git_ignore_template = self .env .get_template (".gitignore.jinja" )
170
- git_ignore_path .write_text (git_ignore_template .render ())
182
+ git_ignore_path .write_text (git_ignore_template .render (), encoding = self . encoding )
171
183
172
184
def _build_pyproject_toml (self , * , use_poetry : bool ) -> None :
173
185
template = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja"
@@ -179,7 +191,8 @@ def _build_pyproject_toml(self, *, use_poetry: bool) -> None:
179
191
package_name = self .package_name ,
180
192
version = self .version ,
181
193
description = self .package_description ,
182
- )
194
+ ),
195
+ encoding = self .encoding ,
183
196
)
184
197
185
198
def _build_setup_py (self ) -> None :
@@ -191,7 +204,8 @@ def _build_setup_py(self) -> None:
191
204
package_name = self .package_name ,
192
205
version = self .version ,
193
206
description = self .package_description ,
194
- )
207
+ ),
208
+ encoding = self .encoding ,
195
209
)
196
210
197
211
def _build_models (self ) -> None :
@@ -204,7 +218,7 @@ def _build_models(self) -> None:
204
218
model_template = self .env .get_template ("model.py.jinja" )
205
219
for model in self .openapi .models .values ():
206
220
module_path = models_dir / f"{ model .reference .module_name } .py"
207
- module_path .write_text (model_template .render (model = model ))
221
+ module_path .write_text (model_template .render (model = model ), encoding = self . encoding )
208
222
imports .append (import_string_from_reference (model .reference ))
209
223
210
224
# Generate enums
@@ -213,25 +227,25 @@ def _build_models(self) -> None:
213
227
for enum in self .openapi .enums .values ():
214
228
module_path = models_dir / f"{ enum .reference .module_name } .py"
215
229
if enum .value_type is int :
216
- module_path .write_text (int_enum_template .render (enum = enum ))
230
+ module_path .write_text (int_enum_template .render (enum = enum ), encoding = self . encoding )
217
231
else :
218
- module_path .write_text (str_enum_template .render (enum = enum ))
232
+ module_path .write_text (str_enum_template .render (enum = enum ), encoding = self . encoding )
219
233
imports .append (import_string_from_reference (enum .reference ))
220
234
221
235
models_init_template = self .env .get_template ("models_init.py.jinja" )
222
- models_init .write_text (models_init_template .render (imports = imports ))
236
+ models_init .write_text (models_init_template .render (imports = imports ), encoding = self . encoding )
223
237
224
238
def _build_api (self ) -> None :
225
239
# Generate Client
226
240
client_path = self .package_dir / "client.py"
227
241
client_template = self .env .get_template ("client.py.jinja" )
228
- client_path .write_text (client_template .render ())
242
+ client_path .write_text (client_template .render (), encoding = self . encoding )
229
243
230
244
# Generate endpoints
231
245
api_dir = self .package_dir / "api"
232
246
api_dir .mkdir ()
233
247
api_init = api_dir / "__init__.py"
234
- api_init .write_text ('""" Contains methods for accessing the API """' )
248
+ api_init .write_text ('""" Contains methods for accessing the API """' , encoding = self . encoding )
235
249
236
250
endpoint_template = self .env .get_template ("endpoint_module.py.jinja" )
237
251
for tag , collection in self .openapi .endpoint_collections_by_tag .items ():
@@ -242,31 +256,42 @@ def _build_api(self) -> None:
242
256
243
257
for endpoint in collection .endpoints :
244
258
module_path = tag_dir / f"{ snake_case (endpoint .name )} .py"
245
- module_path .write_text (endpoint_template .render (endpoint = endpoint ))
259
+ module_path .write_text (endpoint_template .render (endpoint = endpoint ), encoding = self . encoding )
246
260
247
261
248
262
def _get_project_for_url_or_path (
249
- url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
263
+ url : Optional [str ],
264
+ path : Optional [Path ],
265
+ meta : MetaType ,
266
+ custom_template_path : Optional [Path ] = None ,
267
+ encoding : Optional [str ] = None ,
250
268
) -> Union [Project , GeneratorError ]:
251
269
data_dict = _get_document (url = url , path = path )
252
270
if isinstance (data_dict , GeneratorError ):
253
271
return data_dict
254
272
openapi = GeneratorData .from_dict (data_dict )
255
273
if isinstance (openapi , GeneratorError ):
256
274
return openapi
257
- return Project (openapi = openapi , custom_template_path = custom_template_path , meta = meta )
275
+ return Project (openapi = openapi , custom_template_path = custom_template_path , meta = meta , encoding = encoding )
258
276
259
277
260
278
def create_new_client (
261
- * , url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
279
+ * ,
280
+ url : Optional [str ],
281
+ path : Optional [Path ],
282
+ meta : MetaType ,
283
+ custom_template_path : Optional [Path ] = None ,
284
+ encoding : Optional [str ] = None ,
262
285
) -> Sequence [GeneratorError ]:
263
286
"""
264
287
Generate the client library
265
288
266
289
Returns:
267
290
A list containing any errors encountered when generating.
268
291
"""
269
- project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path , meta = meta )
292
+ project = _get_project_for_url_or_path (
293
+ url = url , path = path , custom_template_path = custom_template_path , meta = meta , encoding = encoding
294
+ )
270
295
if isinstance (project , GeneratorError ):
271
296
return [project ]
272
297
return project .build ()
0 commit comments