This commit is contained in:
Waylon Walker 2023-05-19 08:35:16 -05:00
commit 4be274d9e2
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
39 changed files with 2548 additions and 0 deletions

View file

View file

@ -0,0 +1,28 @@
import typer
import uvicorn
from learn_sql_model.cli.common import verbose_callback
api_app = typer.Typer()
@api_app.callback()
def api(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
"model cli"
@api_app.command()
def run(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
uvicorn.run("learn_sql_model.api.app:app", port=5000, log_level="info")

View file

@ -0,0 +1,58 @@
import typer
from learn_sql_model.cli.api import api_app
from learn_sql_model.cli.common import verbose_callback
from learn_sql_model.cli.config import config_app
from learn_sql_model.cli.hero import hero_app
from learn_sql_model.cli.model_app import model_app
from learn_sql_model.cli.tui import tui_app
app = typer.Typer(
name="learn_sql_model",
help="A rich terminal report for coveragepy.",
)
app.add_typer(config_app)
app.add_typer(tui_app)
app.add_typer(model_app)
app.add_typer(api_app)
app.add_typer(hero_app, name="hero")
def version_callback(value: bool) -> None:
"""Callback function to print the version of the learn-sql-model package.
Args:
value (bool): Boolean value to determine if the version should be printed.
Raises:
typer.Exit: If the value is True, the version will be printed and the program will exit.
Example:
version_callback(True)
"""
if value:
from learn_sql_model.__about__ import __version__
typer.echo(f"{__version__}")
raise typer.Exit()
@app.callback()
def main(
version: bool = typer.Option(
None,
"--version",
callback=version_callback,
is_eager=True,
),
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
) -> None:
return
if __name__ == "__main__":
typer.run(main)

View file

@ -0,0 +1,6 @@
from learn_sql_model.console import console
def verbose_callback(value: bool) -> None:
if value:
console.quiet = False

View file

@ -0,0 +1,29 @@
from rich.console import Console
import typer
from learn_sql_model.cli.common import verbose_callback
from learn_sql_model.config import config as configuration
config_app = typer.Typer()
@config_app.callback()
def config(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
"configuration cli"
@config_app.command()
def show(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
Console().print(configuration)

View file

@ -0,0 +1,30 @@
from typing import List, Union
from pydantic_typer import expand_pydantic_args
from rich.console import Console
import typer
from learn_sql_model.models import Hero
hero_app = typer.Typer()
@hero_app.callback()
def hero():
"model cli"
@hero_app.command()
def get(id: int = None) -> Union[Hero, List[Hero]]:
"get one hero"
hero = Hero.get(item_id=id)
Console().print(hero)
return hero
@hero_app.command()
@expand_pydantic_args(typer=True)
def create(hero: Hero) -> Hero:
"read all the heros"
hero = hero.post()
Console().print(hero)

View file

@ -0,0 +1,119 @@
from rich.console import Console
from sqlmodel import SQLModel, Session
import typer
from learn_sql_model.cli.common import verbose_callback
from learn_sql_model.config import config
from learn_sql_model.models import Hero, Pet
model_app = typer.Typer()
@model_app.callback()
def model(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
"model cli"
@model_app.command()
def create_revision(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
message: str = typer.Option(
prompt=True,
),
):
import alembic
# python -m alembic revision --autogenerate -m "New Attribute"
from alembic.config import Config
alembic_cfg = Config("alembic.ini")
alembic.command.revision(
config=alembic_cfg,
message=message,
autogenerate=True,
)
@model_app.command()
def show(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
SQLModel.metadata.create_all(config.engine)
with Session(config.engine) as session:
heros = session.exec(select(Hero)).all()
Console().print(heros)
@model_app.command()
def read(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
from learn_sql_model.api import read_heroes
Console().print(read_heroes())
# @model_app.command()
# @expand_pydantic_args(typer=True)
# def create(
# hero: Hero,
# ):
# hero.post()
# try:
# httpx.post("http://localhost:5000/heroes/", json=hero.dict())
# except httpx.ConnectError:
# console.log("local failover")
# with Session(config.engine) as session:
# session.add(hero)
# session.commit()
@model_app.command()
def populate(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
pet_1 = Pet(name="Deadpond-Dog")
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson", pets=[pet_1])
hero_2 = Hero(
name="Spider-Boy",
secret_name="Pedro Parqueador",
pet=Pet(name="Spider-Boy-Dog"),
)
hero_3 = Hero(
name="Rusty-Man",
secret_name="Tommy Sharp",
age=48,
pet=Pet(name="Rusty-Man-Dog"),
)
SQLModel.metadata.create_all(config.engine)
with Session(config.engine) as session:
session.add(hero_1)
session.add(hero_2)
session.add(hero_3)
session.commit()

View file

@ -0,0 +1,18 @@
import typer
from learn_sql_model.cli.common import verbose_callback
from learn_sql_model.tui.app import run_app
tui_app = typer.Typer()
@tui_app.callback(invoke_without_command=True)
def i(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
"interactive tui"
run_app()