Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/Add new cli to generate schema as yaml #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def load_requirements(file: Union[str, Path]) -> List[str]:
packages=find_packages(),
entry_points={
"console_scripts": [
"etl-uvicorn=unstructured_platform_plugins.etl_uvicorn.main:cmd",
"etl-validate=unstructured_platform_plugins.validate_api:validate_api",
"etl-uvicorn=unstructured_platform_plugins.cli.etl_uvicorn.main:cmd",
"etl-validate=unstructured_platform_plugins.cli.validate.main:validate_api",
"etl-schema=unstructured_platform_plugins.cli.schema_yaml.main:generate_yaml",
],
},
install_requires=load_requirements("requirements/cli.in")
Expand Down
22 changes: 22 additions & 0 deletions test/expected_results/simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
inputs:
properties:
content:
properties:
name:
type: string
value:
type: number
required:
- name
- value
type: object
required:
- content
type: object
outputs:
properties:
response:
type: object
required:
- response
type: object
2 changes: 1 addition & 1 deletion test/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import BaseModel

import unstructured_platform_plugins.schema.json_schema as js
from unstructured_platform_plugins.etl_uvicorn.utils import get_input_schema
from unstructured_platform_plugins.cli.utils import get_input_schema
from unstructured_platform_plugins.schema.model import is_validate_dict


Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydantic import BaseModel
from uvicorn.importer import import_from_string

from unstructured_platform_plugins.etl_uvicorn import utils
from unstructured_platform_plugins.cli import utils


def test_get_func_simple():
Expand Down
14 changes: 14 additions & 0 deletions test/test_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pathlib import Path

from unstructured_platform_plugins.cli.schema_yaml.yaml_generator import generate_yaml

test_path = Path(__file__).parent
results_path = test_path / "expected_results"


def test_generate_yaml_simple():
output = generate_yaml(app="test.assets.typed_dict_response:sample_function")
file_path = results_path / "simple.yaml"
Comment on lines +10 to +11
Copy link

@hubert-rutkowski85 hubert-rutkowski85 Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: there is no obvious relation between sample_function stored in test.assets.typed_dict_response.py, and expected_results/simple.yaml . Naming it like expected_results/typed_dict_response:sample_function.yaml would make it painfully clear, unfortunately it's a mouthful. Maybe it's ok, or maybe just using the filename, or a function name, would be good enough. I'll leave it for you to decide, to not keep this PR waiting any longer.

with file_path.open("r") as f:
expected = f.read()
assert expected == output
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pydantic import BaseModel
from uvicorn.importer import import_from_string

from unstructured_platform_plugins.etl_uvicorn.utils import (
from unstructured_platform_plugins.cli.utils import (
get_func,
get_input_schema,
get_output_sig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uvicorn.config import LOGGING_CONFIG, Config, RawConfigParser
from uvicorn.main import main, run

from unstructured_platform_plugins.etl_uvicorn.api_generator import generate_fast_api
from unstructured_platform_plugins.cli.etl_uvicorn.api_generator import generate_fast_api


@dataclass
Expand Down
20 changes: 20 additions & 0 deletions unstructured_platform_plugins/cli/schema_yaml/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Optional

import click

from unstructured_platform_plugins.cli.schema_yaml.yaml_generator import generate_yaml


@click.command()
@click.argument("app", envvar="UVICORN_APP")
@click.option(
"--method-name",
required=False,
type=str,
default=None,
help="If passed in instance is a class, what method to wrap. "
"Will fall back to __call__ if none is provided.",
)
def yaml(app: str, method_name: Optional[str] = None) -> None:
output = generate_yaml(app=app, method_name=method_name)
print(output)
14 changes: 14 additions & 0 deletions unstructured_platform_plugins/cli/schema_yaml/yaml_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Optional

from uvicorn.importer import import_from_string
from yaml import Dumper, dump

from unstructured_platform_plugins.cli.utils import get_func, get_schema_dict


def generate_yaml(app: str, method_name: Optional[str] = None) -> str:
instance = import_from_string(app)
func = get_func(instance=instance, method_name=method_name)
schemas = get_schema_dict(func)
output = dump(schemas, Dumper=Dumper)
return output
Loading