Add pyopenapi fork to the repository, update RFC assets

This commit is contained in:
Ashwin Bharambe 2024-09-03 21:22:30 -07:00
parent 0d619b9f8e
commit b60c125c55
12 changed files with 2240 additions and 452 deletions

View file

@ -10,17 +10,13 @@
# This source code is licensed under the terms described found in the
# LICENSE file in the root directory of this source tree.
import inspect
from datetime import datetime
from pathlib import Path
from typing import Callable, Iterator, List, Tuple
import fire
import yaml
from llama_models import schema_utils
from pyopenapi import Info, operations, Options, Server, Specification
# We do a series of monkey-patching to ensure our definitions only use the minimal
# (json_schema_type, webmethod) definitions from the llama_models package. For
@ -28,7 +24,10 @@ from pyopenapi import Info, operations, Options, Server, Specification
# (python-openapi, json-strong-typing) packages.
from strong_typing.schema import json_schema_type
from termcolor import colored
from .pyopenapi.options import Options
from .pyopenapi.specification import Info, Server
from .pyopenapi.utility import Specification
schema_utils.json_schema_type = json_schema_type
@ -36,45 +35,6 @@ schema_utils.json_schema_type = json_schema_type
from llama_toolchain.stack import LlamaStack
def patched_get_endpoint_functions(
endpoint: type, prefixes: List[str]
) -> Iterator[Tuple[str, str, str, Callable]]:
if not inspect.isclass(endpoint):
raise ValueError(f"object is not a class type: {endpoint}")
functions = inspect.getmembers(endpoint, inspect.isfunction)
for func_name, func_ref in functions:
webmethod = getattr(func_ref, "__webmethod__", None)
if not webmethod:
continue
print(f"Processing {colored(func_name, 'white')}...")
operation_name = func_name
if operation_name.startswith("get_") or operation_name.endswith("/get"):
prefix = "get"
elif (
operation_name.startswith("delete_")
or operation_name.startswith("remove_")
or operation_name.endswith("/delete")
or operation_name.endswith("/remove")
):
prefix = "delete"
else:
if webmethod.method == "GET":
prefix = "get"
elif webmethod.method == "DELETE":
prefix = "delete"
else:
# by default everything else is a POST
prefix = "post"
yield prefix, operation_name, func_name, func_ref
# Patch this so all methods are correctly parsed with correct HTTP methods
operations._get_endpoint_functions = patched_get_endpoint_functions
def main(output_dir: str):
output_dir = Path(output_dir)
if not output_dir.exists():