117 lines
2.7 KiB
Python
117 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import importlib
|
|
import json
|
|
from pathlib import Path
|
|
from pydantic import ValidationError
|
|
|
|
from engine.registry import doc_dir, list_docs
|
|
from engine.render import render_from_dir
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
# keep cli parsing separate so main stays about workflow
|
|
parser = argparse.ArgumentParser(description="Render document from schema + templates")
|
|
|
|
parser.add_argument(
|
|
"--list-documents",
|
|
action="store_true",
|
|
help="List all available documents",
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--doc",
|
|
help="document type name (folder in documents/)"
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--interactive",
|
|
action="store_true",
|
|
help="Prompt for answers interactively (CLI POC)",
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--input",
|
|
"-i",
|
|
type=Path,
|
|
# required=True,
|
|
help="Path to input JSON file (e.g. examples/input.json)"
|
|
)
|
|
parser.add_argument(
|
|
"--template",
|
|
"-t",
|
|
default="template.md.j2",
|
|
help="Template file (e.g. doc.md.j2)"
|
|
)
|
|
parser.add_argument(
|
|
"--output",
|
|
"-o",
|
|
type=Path,
|
|
default=Path("out.md"),
|
|
# required=True,
|
|
help="Path to write rendered output (e.g. out.md)"
|
|
)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
def load_schema_model(doc_name: str):
|
|
"""
|
|
import documents.<doc_name>.schema and return DocumentInput model.
|
|
"""
|
|
module = importlib.import_module(f"documents.{doc_name}.schema")
|
|
return getattr(module, "DocumentInput")
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
if args.list_documents:
|
|
docs = list_docs()
|
|
if not docs:
|
|
print("No documents found. (missing documents/ packages?)")
|
|
return
|
|
for d in docs:
|
|
print(d)
|
|
return
|
|
|
|
if not args.doc:
|
|
print("error: --doc is required (or use --list-docs)")
|
|
return
|
|
|
|
if args.interactive:
|
|
return # work on later
|
|
|
|
if not args.input:
|
|
print("error: --input is required")
|
|
|
|
# load input data
|
|
# path() over hardcoding str
|
|
data = json.loads(args.input.read_text(encoding="utf-8"))
|
|
|
|
# validate + normalise input
|
|
# validate w/ schema
|
|
model = load_schema_model(args.doc)
|
|
try:
|
|
validated = model(**data)
|
|
except ValidationError as e:
|
|
print(e)
|
|
return
|
|
|
|
# render using doc template
|
|
ddir = doc_dir(args.doc)
|
|
rendered_text = render_from_dir(
|
|
doc_dir=ddir,
|
|
template_filename=args.template,
|
|
data=validated.model_dump(),
|
|
)
|
|
|
|
# write output - render md to file
|
|
args.output.write_text(rendered_text, encoding="utf-8")
|
|
print(f"wrote {args.output}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|