Skip to content

Commit 13bbd3e

Browse files
committed
✨ Add option formater Fix #2
[yapf](https://github.com/google/yapf) or [black](https://github.com/psf/black)
1 parent 24fcb18 commit 13bbd3e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
##### ✨ New
55

6+
- Add option `formater` Fix #2.
7+
[yapf](https://github.com/google/yapf) or [black](https://github.com/psf/black)
68
- Add option `skip_deprecated` Fix #7.
79
Don't generate code for deprecated operations
810

apiclient_pydantic_generator/__main__.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import json
22
import sys
3+
from enum import Enum
34
from pathlib import Path
4-
from typing import Any, Dict, List, Optional
5+
from typing import Any, Dict, List, Optional, Type, Union
56

67
import toml
78
import typer
8-
from datamodel_code_generator import Error, PythonVersion, chdir
9+
from datamodel_code_generator import Error, chdir
910
from datamodel_code_generator.__main__ import Config, Exit
11+
from datamodel_code_generator.format import CodeFormatter as BlackCodeFormatter
1012
from datamodel_code_generator.imports import Import, Imports
1113
from datamodel_code_generator.reference import Reference
1214
from datamodel_code_generator.types import DataType
@@ -24,6 +26,17 @@
2426
MODEL_PATH: Path = Path('models.py')
2527

2628

29+
class Formatters(str, Enum):
30+
YAPF = 'yapf'
31+
BLACK = 'black'
32+
33+
34+
formaters = {
35+
Formatters.YAPF.value: YapfCodeFormatter,
36+
Formatters.BLACK.value: BlackCodeFormatter,
37+
}
38+
39+
2740
@app.command()
2841
def main(
2942
input_file: typer.FileText = typer.Option(..., '--input', '-i'), # noqa: B008
@@ -42,6 +55,7 @@ def main(
4255
'-a',
4356
help='Base class for client class',
4457
),
58+
formater: Optional[Formatters] = typer.Option(Formatters.YAPF.value, case_sensitive=False), # noqa: B008
4559
skip_deprecated: Optional[bool] = typer.Option( # noqa: B008
4660
True,
4761
'--skip-deprecated',
@@ -75,6 +89,7 @@ def main(
7589
prefix_api_cls,
7690
base_apiclient_cls,
7791
skip_deprecated,
92+
formaters.get(formater.value) or YapfCodeFormatter,
7893
)
7994

8095

@@ -114,6 +129,7 @@ def generate_code(
114129
prefix_api_cls: Optional[str],
115130
base_apiclient_cls: Optional[str],
116131
skip_deprecated: Optional[bool],
132+
code_formatter_cls: Type[Union[YapfCodeFormatter, BlackCodeFormatter]],
117133
) -> None:
118134
output_dir.mkdir(parents=True, exist_ok=True)
119135
template_dir = template_dir or BUILTIN_TEMPLATE_DIR
@@ -177,12 +193,15 @@ def generate_code(
177193
if reference:
178194
imports.append(data_type.all_imports)
179195
imports.append(Import.from_full_path(f'.{MODEL_PATH.stem}.{reference.name}'))
196+
180197
for from_, imports_ in parser.imports_for_endpoints.items():
181198
imports[from_].update(imports_)
199+
182200
results: Dict[Path, str] = {}
183-
# TODO: Choose formater from cli
184-
code_formatter = YapfCodeFormatter(PythonVersion.PY_38, Path().resolve())
201+
202+
code_formatter = code_formatter_cls(data_config.target_python_version, Path().resolve())
185203
sorted_operations: List[Operation] = sorted(parser.operations.values(), key=lambda m: m.path)
204+
186205
for target in template_dir.rglob('*'):
187206
relative_path = target.relative_to(template_dir)
188207
result = environment.get_template(str(relative_path)).render(

0 commit comments

Comments
 (0)