-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerator.py
More file actions
35 lines (28 loc) · 1.14 KB
/
generator.py
File metadata and controls
35 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright (c) 2025 Jascha Wanger / Tarnover, LLC
# SPDX-License-Identifier: MIT
#
# This file is part of the MockLoop project. (https://mockloop.com)
# You may obtain a copy of the license at https://opensource.org/licenses/MIT
from pathlib import Path
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader("templates"), autoescape=select_autoescape(["html", "xml"])
)
def generate_routes(spec: dict, output_path: str):
template = env.get_template("route_template.j2")
paths = spec.get("paths", {})
routes_code = ""
for path, methods in paths.items():
for method, details in methods.items():
route = template.render(
method=method,
path=path.replace("{", "{"),
summary=details.get("summary", ""),
response_body={"message": "mock response"},
)
routes_code += route + "\n"
output_file = Path(output_path)
output_file.parent.mkdir(parents=True, exist_ok=True)
output_file.write_text(
f"from fastapi import FastAPI\n\napp = FastAPI()\n\n{routes_code}"
)