-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy path__init__.py
372 lines (319 loc) · 12.9 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
""" Generate modern Python clients from OpenAPI """
import shutil
import subprocess
import sys
from enum import Enum
from pathlib import Path
from typing import Any, Dict, Optional, Sequence, Union
import httpcore
import httpx
import yaml
from jinja2 import BaseLoader, ChoiceLoader, Environment, FileSystemLoader, PackageLoader
from openapi_python_client import utils
from .config import Config
from .parser import GeneratorData, import_string_from_class
from .parser.errors import GeneratorError
if sys.version_info.minor < 8: # version did not exist before 3.8, need to use a backport
from importlib_metadata import version
else:
from importlib.metadata import version # type: ignore
__version__ = version(__package__)
class MetaType(str, Enum):
"""The types of metadata supported for project generation."""
NONE = "none"
POETRY = "poetry"
SETUP = "setup"
TEMPLATE_FILTERS = {
"snakecase": utils.snake_case,
"kebabcase": utils.kebab_case,
"pascalcase": utils.pascal_case,
"any": any,
}
class Project: # pylint: disable=too-many-instance-attributes
"""Represents a Python project (the top level file-tree) to generate"""
def __init__(
self,
*,
openapi: GeneratorData,
meta: MetaType,
config: Config,
custom_template_path: Optional[Path] = None,
file_encoding: str = "utf-8",
) -> None:
self.openapi: GeneratorData = openapi
self.meta: MetaType = meta
self.file_encoding = file_encoding
self.config = config
package_loader = PackageLoader(__package__)
loader: BaseLoader
if custom_template_path is not None:
loader = ChoiceLoader(
[
FileSystemLoader(str(custom_template_path)),
package_loader,
]
)
else:
loader = package_loader
self.env: Environment = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
self.project_name: str = config.project_name_override or f"{utils.kebab_case(openapi.title).lower()}-client"
self.project_dir: Path = Path.cwd()
if meta != MetaType.NONE:
self.project_dir /= self.project_name
self.package_name: str = config.package_name_override or self.project_name.replace("-", "_")
self.package_dir: Path = self.project_dir / self.package_name
self.package_description: str = utils.remove_string_escapes(
f"A client library for accessing {self.openapi.title}"
)
self.version: str = config.package_version_override or openapi.version
self.env.filters.update(TEMPLATE_FILTERS)
self.env.globals.update(
utils=utils,
python_identifier=lambda x: utils.PythonIdentifier(x, config.field_prefix),
class_name=lambda x: utils.ClassName(x, config.field_prefix),
package_name=self.package_name,
package_dir=self.package_dir,
package_description=self.package_description,
package_version=self.version,
project_name=self.project_name,
project_dir=self.project_dir,
)
def build(self) -> Sequence[GeneratorError]:
"""Create the project from templates"""
if self.meta == MetaType.NONE:
print(f"Generating {self.package_name}")
else:
print(f"Generating {self.project_name}")
try:
self.project_dir.mkdir()
except FileExistsError:
return [GeneratorError(detail="Directory already exists. Delete it or use the update command.")]
self._create_package()
self._build_metadata()
self._build_models()
self._build_api()
self._reformat()
return self._get_errors()
def update(self) -> Sequence[GeneratorError]:
"""Update an existing project"""
if not self.package_dir.is_dir():
raise FileNotFoundError()
print(f"Updating {self.package_name}")
shutil.rmtree(self.package_dir)
self._create_package()
self._build_models()
self._build_api()
self._reformat()
return self._get_errors()
def _reformat(self) -> None:
subprocess.run(
"autoflake -i -r --remove-all-unused-imports --remove-unused-variables --ignore-init-module-imports .",
cwd=self.package_dir,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
subprocess.run(
"isort .",
cwd=self.project_dir,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
subprocess.run(
"black .", cwd=self.project_dir, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True
)
def _get_errors(self) -> Sequence[GeneratorError]:
errors = []
for collection in self.openapi.endpoint_collections_by_tag.values():
errors.extend(collection.parse_errors)
errors.extend(self.openapi.errors)
return errors
def _create_package(self) -> None:
self.package_dir.mkdir()
# Package __init__.py
package_init = self.package_dir / "__init__.py"
package_init_template = self.env.get_template("package_init.py.jinja")
package_init.write_text(package_init_template.render(), encoding=self.file_encoding)
if self.meta != MetaType.NONE:
pytyped = self.package_dir / "py.typed"
pytyped.write_text("# Marker file for PEP 561", encoding=self.file_encoding)
types_template = self.env.get_template("types.py.jinja")
types_path = self.package_dir / "types.py"
types_path.write_text(types_template.render(), encoding=self.file_encoding)
def _build_metadata(self) -> None:
if self.meta == MetaType.NONE:
return
self._build_pyproject_toml(use_poetry=self.meta == MetaType.POETRY)
if self.meta == MetaType.SETUP:
self._build_setup_py()
# README.md
readme = self.project_dir / "README.md"
readme_template = self.env.get_template("README.md.jinja")
readme.write_text(
readme_template.render(),
encoding=self.file_encoding,
)
# .gitignore
git_ignore_path = self.project_dir / ".gitignore"
git_ignore_template = self.env.get_template(".gitignore.jinja")
git_ignore_path.write_text(git_ignore_template.render(), encoding=self.file_encoding)
def _build_pyproject_toml(self, *, use_poetry: bool) -> None:
template = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja"
pyproject_template = self.env.get_template(template)
pyproject_path = self.project_dir / "pyproject.toml"
pyproject_path.write_text(
pyproject_template.render(),
encoding=self.file_encoding,
)
def _build_setup_py(self) -> None:
template = self.env.get_template("setup.py.jinja")
path = self.project_dir / "setup.py"
path.write_text(
template.render(),
encoding=self.file_encoding,
)
def _build_models(self) -> None:
# Generate models
models_dir = self.package_dir / "models"
models_dir.mkdir()
models_init = models_dir / "__init__.py"
imports = []
model_template = self.env.get_template("model.py.jinja")
for model in self.openapi.models:
module_path = models_dir / f"{model.class_info.module_name}.py"
module_path.write_text(model_template.render(model=model), encoding=self.file_encoding)
imports.append(import_string_from_class(model.class_info))
# Generate enums
str_enum_template = self.env.get_template("str_enum.py.jinja")
int_enum_template = self.env.get_template("int_enum.py.jinja")
for enum in self.openapi.enums:
module_path = models_dir / f"{enum.class_info.module_name}.py"
if enum.value_type is int:
module_path.write_text(int_enum_template.render(enum=enum), encoding=self.file_encoding)
else:
module_path.write_text(str_enum_template.render(enum=enum), encoding=self.file_encoding)
imports.append(import_string_from_class(enum.class_info))
models_init_template = self.env.get_template("models_init.py.jinja")
models_init.write_text(models_init_template.render(imports=imports), encoding=self.file_encoding)
def _build_api(self) -> None:
# Generate Client
client_path = self.package_dir / "client.py"
client_template = self.env.get_template("client.py.jinja")
client_path.write_text(client_template.render(), encoding=self.file_encoding)
# Generate endpoints
endpoint_collections_by_tag = self.openapi.endpoint_collections_by_tag
api_dir = self.package_dir / "api"
api_dir.mkdir()
api_init_path = api_dir / "__init__.py"
api_init_template = self.env.get_template("api_init.py.jinja")
api_init_path.write_text(
api_init_template.render(
endpoint_collections_by_tag=endpoint_collections_by_tag,
),
encoding=self.file_encoding,
)
endpoint_template = self.env.get_template("endpoint_module.py.jinja")
for tag, collection in endpoint_collections_by_tag.items():
tag_dir = api_dir / tag
tag_dir.mkdir()
endpoint_init_path = tag_dir / "__init__.py"
endpoint_init_template = self.env.get_template("endpoint_init.py.jinja")
endpoint_init_path.write_text(
endpoint_init_template.render(endpoint_collection=collection),
encoding=self.file_encoding,
)
for endpoint in collection.endpoints:
module_path = tag_dir / f"{utils.PythonIdentifier(endpoint.name, self.config.field_prefix)}.py"
module_path.write_text(endpoint_template.render(endpoint=endpoint), encoding=self.file_encoding)
def _get_project_for_url_or_path( # pylint: disable=too-many-arguments
url: Optional[str],
path: Optional[Path],
meta: MetaType,
config: Config,
custom_template_path: Optional[Path] = None,
file_encoding: str = "utf-8",
) -> Union[Project, GeneratorError]:
data_dict = _get_document(url=url, path=path)
if isinstance(data_dict, GeneratorError):
return data_dict
openapi = GeneratorData.from_dict(data_dict, config=config)
if isinstance(openapi, GeneratorError):
return openapi
return Project(
openapi=openapi,
custom_template_path=custom_template_path,
meta=meta,
file_encoding=file_encoding,
config=config,
)
def create_new_client(
*,
url: Optional[str],
path: Optional[Path],
meta: MetaType,
config: Config,
custom_template_path: Optional[Path] = None,
file_encoding: str = "utf-8",
) -> Sequence[GeneratorError]:
"""
Generate the client library
Returns:
A list containing any errors encountered when generating.
"""
project = _get_project_for_url_or_path(
url=url,
path=path,
custom_template_path=custom_template_path,
meta=meta,
file_encoding=file_encoding,
config=config,
)
if isinstance(project, GeneratorError):
return [project]
return project.build()
def update_existing_client(
*,
url: Optional[str],
path: Optional[Path],
meta: MetaType,
config: Config,
custom_template_path: Optional[Path] = None,
file_encoding: str = "utf-8",
) -> Sequence[GeneratorError]:
"""
Update an existing client library
Returns:
A list containing any errors encountered when generating.
"""
project = _get_project_for_url_or_path(
url=url,
path=path,
custom_template_path=custom_template_path,
meta=meta,
file_encoding=file_encoding,
config=config,
)
if isinstance(project, GeneratorError):
return [project]
return project.update()
def _get_document(*, url: Optional[str], path: Optional[Path]) -> Union[Dict[str, Any], GeneratorError]:
yaml_bytes: bytes
if url is not None and path is not None:
return GeneratorError(header="Provide URL or Path, not both.")
if url is not None:
try:
response = httpx.get(url)
yaml_bytes = response.content
except (httpx.HTTPError, httpcore.NetworkError):
return GeneratorError(header="Could not get OpenAPI document from provided URL")
elif path is not None:
yaml_bytes = path.read_bytes()
else:
return GeneratorError(header="No URL or Path provided")
try:
return yaml.safe_load(yaml_bytes)
except yaml.YAMLError:
return GeneratorError(header="Invalid YAML from provided source")