Skip to content

gen

faststream.cli.docs.app.gen #

gen(app=Argument(..., help='[python_module:FastStream] - path to your application.'), yaml=Option(False, '--yaml', is_flag=True, help='Generate `asyncapi.yaml` schema.'), out=Option(None, help='Output filename.'), app_dir=Option('.', '--app-dir', help='Look for APP in the specified directory, by adding this to the PYTHONPATH. Defaults to the current working directory.'), is_factory=Option(False, '--factory', is_flag=True, help='Treat APP as an application factory.'))

Generate project AsyncAPI schema.

Source code in faststream/cli/docs/app.py
@docs_app.command(name="gen")
def gen(
    app: str = typer.Argument(
        ...,
        help="[python_module:FastStream] - path to your application.",
    ),
    yaml: bool = typer.Option(
        False,
        "--yaml",
        is_flag=True,
        help="Generate `asyncapi.yaml` schema.",
    ),
    out: Optional[str] = typer.Option(
        None,
        help="Output filename.",
    ),
    app_dir: str = typer.Option(
        ".",
        "--app-dir",
        help=(
            "Look for APP in the specified directory, by adding this to the PYTHONPATH."
            " Defaults to the current working directory."
        ),
    ),
    is_factory: bool = typer.Option(
        False,
        "--factory",
        is_flag=True,
        help="Treat APP as an application factory.",
    ),
) -> None:
    """Generate project AsyncAPI schema."""
    if app_dir:  # pragma: no branch
        sys.path.insert(0, app_dir)

    _, app_obj = import_from_string(app, is_factory=is_factory)

    raw_schema = get_app_schema(app_obj)

    if yaml:
        try:
            schema = raw_schema.to_yaml()
        except ImportError as e:  # pragma: no cover
            typer.echo(INSTALL_YAML, err=True)
            raise typer.Exit(1) from e

        name = out or "asyncapi.yaml"

        with Path(name).open("w") as f:
            f.write(schema)

    else:
        schema = raw_schema.to_jsonable()
        name = out or "asyncapi.json"

        with Path(name).open("w") as f:
            json.dump(schema, f, indent=2)

    typer.echo(f"Your project AsyncAPI scheme was placed to `{name}`")