78 lines
2.1 KiB
Python
78 lines
2.1 KiB
Python
"""
|
|
Model Generator
|
|
|
|
Orchestrates model generation from various sources to various formats.
|
|
Delegates to loaders for input and generators for output.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
from typing import Dict, Type
|
|
|
|
from .generator import GENERATORS, BaseGenerator
|
|
from .loader import ConfigLoader
|
|
|
|
|
|
class ModelGenerator:
|
|
"""
|
|
Generates typed models from configuration.
|
|
|
|
This is the main entry point for model generation.
|
|
Delegates to format-specific generators.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
config: ConfigLoader,
|
|
output_path: Path,
|
|
output_format: str = "pydantic",
|
|
):
|
|
"""
|
|
Initialize the generator.
|
|
|
|
Args:
|
|
config: Loaded configuration
|
|
output_path: Exact path where to write (file or directory depending on format)
|
|
output_format: Output format (pydantic, django, prisma, typescript, protobuf)
|
|
"""
|
|
self.config = config
|
|
self.output_path = Path(output_path)
|
|
self.output_format = output_format
|
|
|
|
if output_format not in GENERATORS:
|
|
raise ValueError(
|
|
f"Unknown output format: {output_format}. "
|
|
f"Available: {list(GENERATORS.keys())}"
|
|
)
|
|
|
|
self.generator = GENERATORS[output_format]()
|
|
|
|
def generate(self) -> Path:
|
|
"""
|
|
Generate models to the specified output path.
|
|
|
|
Returns:
|
|
Path to the generated file/directory
|
|
"""
|
|
# Determine output file path
|
|
if self.output_path.suffix:
|
|
# User specified a file path
|
|
output_file = self.output_path
|
|
else:
|
|
# User specified a directory, add default filename
|
|
output_file = (
|
|
self.output_path / f"__init__{self.generator.file_extension()}"
|
|
)
|
|
|
|
self.generator.generate(self.config, output_file)
|
|
print(f"Generated {self.output_format} models: {output_file}")
|
|
return output_file
|
|
|
|
@classmethod
|
|
def available_formats(cls) -> list:
|
|
"""Return list of available output formats."""
|
|
return list(GENERATORS.keys())
|
|
|
|
|
|
# Re-export for backwards compatibility
|
|
WRITERS = GENERATORS
|