38 lines
1.0 KiB
Python
Executable File
38 lines
1.0 KiB
Python
Executable File
#!/usr/bin/env python
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from uvicorn.importer import import_from_string
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(prog="gen-openapi")
|
|
parser.add_argument(
|
|
"app", help='App import string. Eg. "main:app"', default="main:app"
|
|
)
|
|
parser.add_argument("--app-dir", help="Directory containing the app", default=None)
|
|
parser.add_argument(
|
|
"--out", help="Output file ending in .json", default="openapi.json"
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if args.app_dir is not None:
|
|
print(f"adding {args.app_dir} to sys.path")
|
|
sys.path.insert(0, args.app_dir)
|
|
|
|
print(f"importing app from {args.app}")
|
|
app = import_from_string(args.app)
|
|
openapi = app.openapi()
|
|
version = openapi.get("openapi", "unknown version")
|
|
|
|
print(f"writing openapi spec v{version}")
|
|
out = Path(args.out)
|
|
out.parent.mkdir(parents=True, exist_ok=True)
|
|
out.write_text(json.dumps(openapi, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|