Refactor code structure for improved readability and maintainability

This commit is contained in:
claudi 2026-04-07 09:10:53 +02:00
parent 389d72a136
commit aa4c067ea8
1685 changed files with 393439 additions and 71932 deletions

View file

@ -3,16 +3,13 @@ from __future__ import annotations
import argparse
import shutil
import subprocess
import sys
import tempfile
from dataclasses import dataclass
from pathlib import Path
import yaml
ROOT = Path(__file__).resolve().parent.parent
GENERATED_ROOT = ROOT / "ebay_client" / "generated"
CODEGEN_PYTHON = ROOT / ".venv_codegen" / "Scripts" / "python.exe"
@dataclass(frozen=True)
@ -20,8 +17,6 @@ class ApiSpec:
name: str
spec_path: Path
output_path: Path
project_name: str
package_name: str
API_SPECS = {
@ -29,36 +24,26 @@ API_SPECS = {
name="notification",
spec_path=ROOT / "commerce_notification_v1_oas3.yaml",
output_path=GENERATED_ROOT / "notification",
project_name="ebay-notification-generated",
package_name="notification_generated",
),
"inventory": ApiSpec(
name="inventory",
spec_path=ROOT / "sell_inventory_v1_oas3.yaml",
output_path=GENERATED_ROOT / "inventory",
project_name="ebay-inventory-generated",
package_name="inventory_generated",
),
"fulfillment": ApiSpec(
name="fulfillment",
spec_path=ROOT / "sell_fulfillment_v1_oas3.yaml",
output_path=GENERATED_ROOT / "fulfillment",
project_name="ebay-fulfillment-generated",
package_name="fulfillment_generated",
),
"account": ApiSpec(
name="account",
spec_path=ROOT / "sell_account_v1_oas3.yaml",
output_path=GENERATED_ROOT / "account",
project_name="ebay-account-generated",
package_name="account_generated",
),
"feed": ApiSpec(
name="feed",
spec_path=ROOT / "sell_feed_v1_oas3.yaml",
output_path=GENERATED_ROOT / "feed",
project_name="ebay-feed-generated",
package_name="feed_generated",
),
}
@ -70,55 +55,45 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()
def build_config(spec: ApiSpec) -> dict[str, object]:
return {
"project_name_override": spec.project_name,
"package_name_override": spec.package_name,
"field_prefix": "field_",
"generate_all_tags": False,
"http_timeout": 30,
"literal_enums": False,
"post_hooks": [],
"use_path_prefixes_for_title_model_names": False,
}
def run_generation(spec: ApiSpec, *, fail_on_warning: bool) -> None:
if not spec.spec_path.exists():
raise FileNotFoundError(f"OpenAPI spec not found: {spec.spec_path}")
if not spec.spec_path.exists():
raise FileNotFoundError(f"OpenAPI spec not found: {spec.spec_path}")
if not CODEGEN_PYTHON.exists():
raise FileNotFoundError(
"Code generation interpreter not found. Create .venv_codegen with a supported Python version first."
)
with tempfile.TemporaryDirectory(prefix=f"ebay_{spec.name}_") as temp_dir_name:
temp_dir = Path(temp_dir_name)
config_path = temp_dir / "config.yaml"
temp_output = temp_dir / "output"
config_path.write_text(yaml.safe_dump(build_config(spec), sort_keys=True), encoding="utf-8")
if spec.output_path.exists():
shutil.rmtree(spec.output_path)
spec.output_path.mkdir(parents=True, exist_ok=True)
command = [
sys.executable,
"-m",
"openapi_python_client",
"generate",
"--path",
str(spec.spec_path),
"--meta",
"none",
"--overwrite",
"--config",
str(config_path),
"--output-path",
str(temp_output),
]
if fail_on_warning:
command.append("--fail-on-warning")
command = [
str(CODEGEN_PYTHON),
"-m",
"datamodel_code_generator",
"--input",
str(spec.spec_path),
"--input-file-type",
"openapi",
"--output",
str(spec.output_path / "models.py"),
"--output-model-type",
"pydantic_v2.BaseModel",
"--target-python-version",
"3.11",
"--reuse-model",
"--use-schema-description",
"--field-constraints",
"--use-double-quotes",
]
if fail_on_warning:
command.append("--disable-warnings")
subprocess.run(command, check=True, cwd=str(ROOT))
generated_package = temp_output / spec.package_name
if not generated_package.exists():
generated_package = temp_output
if spec.output_path.exists():
shutil.rmtree(spec.output_path)
shutil.copytree(generated_package, spec.output_path)
subprocess.run(command, check=True, cwd=str(ROOT))
(spec.output_path / "__init__.py").write_text(
'"""Generated Pydantic models from the OpenAPI contract."""\n\nfrom .models import *\n',
encoding="utf-8",
)
def main() -> int: