vtt/src/ttfrog/cli.py

81 lines
1.9 KiB
Python
Raw Normal View History

2025-09-21 13:44:59 -07:00
import logging
from pathlib import Path
from typing import Optional
2025-09-21 22:11:56 -07:00
import click
2025-09-21 13:44:59 -07:00
import typer
2025-09-21 22:11:56 -07:00
from flask.cli import FlaskGroup
from rich import print
2025-09-21 13:44:59 -07:00
from rich.logging import RichHandler
2025-09-21 22:11:56 -07:00
import ttfrog.app
main_app = typer.Typer()
2025-09-21 13:44:59 -07:00
logger = logging.getLogger("ttfrog.cli")
2025-09-21 22:11:56 -07:00
@main_app.callback(invoke_without_command=True)
def callback(
2025-09-21 13:44:59 -07:00
context: typer.Context,
verbose: bool = typer.Option(False, help="Enable verbose output."),
log_level: str = typer.Option("error", help=" Set the log level."),
config_file: Optional[Path] = typer.Option(
"~/.dnd/ttfrog/defaults",
2025-09-21 13:44:59 -07:00
help="Path to the ttfrog configuration file",
),
):
"""
Configure the execution environment with global parameters.
"""
logging.basicConfig(
format="%(message)s",
level=getattr(logging, log_level.upper()),
handlers=[RichHandler(rich_tracebacks=True, tracebacks_suppress=[typer])],
)
ttfrog.app.load_config(config_file)
2025-09-24 22:03:30 -07:00
ttfrog.app.initialize()
ttfrog.app.web.shell_context_processors.append(make_shell_context)
2025-09-24 22:03:30 -07:00
2025-09-21 13:44:59 -07:00
if context.invoked_subcommand is None:
logger.debug("No command specified; invoking default handler.")
run(context)
@main_app.command()
def init(context: typer.Context, drop: bool = typer.Option(False, help="Drop tabless before initializing.")):
"""
Initialize the database.
"""
if drop:
ttfrog.app.db.drop_tables()
ttfrog.app.db.close()
ttfrog.app.initialize(force=True)
ttfrog.app.bootstrap()
print(ttfrog.app.db)
@main_app.command()
2025-09-21 13:44:59 -07:00
def run(context: typer.Context):
"""
The default CLI entrypoint is ttfrog.cli.run().
"""
2025-09-24 22:03:30 -07:00
ttfrog.app.web.run()
2025-09-21 22:11:56 -07:00
def make_shell_context():
2025-09-24 22:03:30 -07:00
return ttfrog.app
2025-09-21 22:11:56 -07:00
2025-09-24 22:03:30 -07:00
@click.group(cls=FlaskGroup, create_app=lambda: ttfrog.app.web)
2025-09-21 22:11:56 -07:00
@click.pass_context
def app(ctx):
"""
2025-09-24 22:03:30 -07:00
Web Application management functions
2025-09-21 22:11:56 -07:00
"""
main = typer.main.get_command(main_app)
main.add_command(app, "app")