Fixes/upgrades
This commit is contained in:
@@ -1,17 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Introspect a Pydantic module and print its declarations as JSON.
|
||||
"""Report a Pydantic module's declarations as JSON on stdout.
|
||||
|
||||
argv[1] is a JSON object:
|
||||
- module: Python module to import (e.g. "claude_manage.schema")
|
||||
- derives: derive identifiers decoru applies to every emitted struct
|
||||
|
||||
stdout is a JSON object:
|
||||
- enums: [{"name": <python class name>, "variants": [<rust ident>, ...]}]
|
||||
- structs: [<rust source>, ...] as rendered by decoru
|
||||
|
||||
decoru itself is scoped to BaseModel, so Enum subclasses are reported as
|
||||
shapes for the caller to render; only their variant identifiers go through
|
||||
decoru, which keeps them equal to the ones it bakes into field defaults.
|
||||
Takes one argv JSON object of `module` and `derives`; returns `enums` as
|
||||
name plus variant identifiers, and `structs` as rendered Rust source.
|
||||
"""
|
||||
|
||||
import importlib
|
||||
@@ -21,9 +12,9 @@ import sys
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import BaseModel # type: ignore[import-untyped]
|
||||
from pydantic import BaseModel
|
||||
|
||||
from decoru import ( # type: ignore[import-untyped]
|
||||
from pydantic_to_rust import (
|
||||
emit_rust_struct,
|
||||
to_rust_variant_ident,
|
||||
walk_pydantic_model,
|
||||
@@ -35,8 +26,8 @@ def _declared_in(module, obj) -> bool:
|
||||
|
||||
|
||||
def discover_models(module) -> list[type[BaseModel]]:
|
||||
"""BaseModel subclasses declared in this module. Imported helpers are
|
||||
skipped — only own-module declarations qualify."""
|
||||
"""BaseModel subclasses declared here. An imported one is another
|
||||
module's to emit."""
|
||||
return [
|
||||
obj
|
||||
for _, obj in inspect.getmembers(module, inspect.isclass)
|
||||
@@ -47,8 +38,8 @@ def discover_models(module) -> list[type[BaseModel]]:
|
||||
|
||||
|
||||
def discover_enums(module) -> list[type[Enum]]:
|
||||
"""Enum subclasses declared in this module. Filters out the
|
||||
framework's own Enum class and anything imported from elsewhere."""
|
||||
"""Enum subclasses declared here. Only variant identifiers are reported;
|
||||
the caller renders the enum body."""
|
||||
return [
|
||||
obj
|
||||
for _, obj in inspect.getmembers(module, inspect.isclass)
|
||||
@@ -58,7 +49,7 @@ def discover_enums(module) -> list[type[Enum]]:
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) < 2:
|
||||
sys.stderr.write("run_decoru.py: missing JSON payload argument\n")
|
||||
sys.stderr.write("run_pydantic_to_rust.py: missing JSON payload argument\n")
|
||||
return 2
|
||||
|
||||
payload = json.loads(sys.argv[1])
|
||||
@@ -72,7 +63,8 @@ def main() -> int:
|
||||
models = discover_models(module)
|
||||
if not enums and not models:
|
||||
sys.stderr.write(
|
||||
f"run_decoru.py: no Enum or BaseModel subclasses declared in {module_name!r}\n"
|
||||
f"run_pydantic_to_rust.py: no Enum or BaseModel subclasses "
|
||||
f"declared in {module_name!r}\n"
|
||||
)
|
||||
return 3
|
||||
|
||||
Reference in New Issue
Block a user