This commit is contained in:
67
app/localapi-docs/src/core/generator.py
Normal file
67
app/localapi-docs/src/core/generator.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""Generator for documentation output."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional
|
||||
from src.core.parser import parse_openapi_spec, _basic_validate
|
||||
from src.core.models import OpenAPISpec
|
||||
|
||||
|
||||
def generate_docs(
|
||||
spec_source: str | Path | Dict[str, Any],
|
||||
format: str = "html",
|
||||
output_path: Optional[str] = None,
|
||||
template_path: Optional[str] = None,
|
||||
) -> str:
|
||||
if isinstance(spec_source, dict):
|
||||
spec_data = spec_source
|
||||
elif isinstance(spec_source, (str, Path)):
|
||||
spec_path = Path(spec_source)
|
||||
if spec_path.exists():
|
||||
content = spec_path.read_text()
|
||||
if spec_path.suffix in [".yaml", ".yml"]:
|
||||
import yaml
|
||||
spec_data = yaml.safe_load(content)
|
||||
else:
|
||||
spec_data = json.loads(content)
|
||||
else:
|
||||
raise FileNotFoundError(f"Spec file not found: {spec_source}")
|
||||
else:
|
||||
raise ValueError(f"Invalid spec source type: {type(spec_source)}")
|
||||
|
||||
is_valid, errors = _basic_validate(spec_data)
|
||||
if not is_valid:
|
||||
raise ValueError(f"Invalid spec: {errors}")
|
||||
|
||||
spec = parse_openapi_spec(spec_data)
|
||||
return spec
|
||||
|
||||
|
||||
def extract_endpoints(spec: OpenAPISpec) -> list:
|
||||
endpoints = []
|
||||
for path, path_item in spec.paths.items():
|
||||
for method, operation in path_item.model_dump().items():
|
||||
if method in ["get", "put", "post", "delete", "options", "head", "patch", "trace"]:
|
||||
if operation:
|
||||
endpoints.append({
|
||||
"path": path,
|
||||
"method": method.upper(),
|
||||
"summary": operation.get("summary", ""),
|
||||
"description": operation.get("description", ""),
|
||||
"tags": operation.get("tags", []),
|
||||
})
|
||||
return endpoints
|
||||
|
||||
|
||||
def generate_template_context(spec: OpenAPISpec) -> Dict[str, Any]:
|
||||
spec_dict = spec.model_dump()
|
||||
return {
|
||||
"spec": spec_dict,
|
||||
"info": spec_dict.get("info", {}),
|
||||
"paths": spec_dict.get("paths", {}),
|
||||
"servers": spec_dict.get("servers", []),
|
||||
"tags": spec_dict.get("tags", []),
|
||||
"components": spec_dict.get("components", {}),
|
||||
"security": spec_dict.get("security", []),
|
||||
"external_docs": spec_dict.get("externalDocs"),
|
||||
}
|
||||
Reference in New Issue
Block a user