refactor: simplify CLI command logic by extracting helper functions #13
@@ -1,4 +1,5 @@
|
||||
from .cli import app
|
||||
|
||||
|
||||
def main():
|
||||
app()
|
||||
|
||||
+11
-4
@@ -1,5 +1,5 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
from typing import Annotated, Any
|
||||
|
||||
import typer
|
||||
|
||||
@@ -8,10 +8,11 @@ from photools3.photo import Photo
|
||||
|
||||
app = typer.Typer(help="PhotoTools3: A CLI for image processing.")
|
||||
|
||||
|
||||
@app.callback(invoke_without_command=True)
|
||||
def main(
|
||||
ctx: typer.Context,
|
||||
version: Optional[bool] = typer.Option(None, "--version", is_eager=True),
|
||||
version: bool | None = typer.Option(None, "--version", is_eager=True),
|
||||
):
|
||||
"""
|
||||
Main entry point for PhotoTools3 CLI.
|
||||
@@ -24,8 +25,11 @@ def main(
|
||||
typer.echo(ctx.get_help())
|
||||
raise typer.Exit()
|
||||
|
||||
|
||||
@app.command("keywords")
|
||||
def keywords(file_path: Path = typer.Argument(..., help="Path to the image file")):
|
||||
def keywords(
|
||||
file_path: Annotated[Path, typer.Argument(..., help="Path to the image file")],
|
||||
):
|
||||
"""Extract keywords from a single image."""
|
||||
if not file_path.exists():
|
||||
typer.echo(f"Error: File '{file_path}' does not exist.", err=True)
|
||||
@@ -35,16 +39,18 @@ def keywords(file_path: Path = typer.Argument(..., help="Path to the image file"
|
||||
typer.echo(f"Processing: {file_path}")
|
||||
result = _run_keywords_extraction(file_path)
|
||||
_print_results(result)
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
typer.echo(f"Error processing image: {e}", err=True)
|
||||
raise typer.Exit(code=1)
|
||||
|
||||
|
||||
def _run_keywords_extraction(file_path: Path) -> Any:
|
||||
"""Handles the core logic of loading the photo and calling the LLM."""
|
||||
session = LlmSessionCaptionsAndKeywords()
|
||||
photo = Photo(file_path)
|
||||
return session.call([photo])
|
||||
|
||||
|
||||
def _print_results(result: Any) -> None:
|
||||
"""Formats and prints the extraction results to the console."""
|
||||
typer.echo("\n--- Results ---")
|
||||
@@ -57,5 +63,6 @@ def _print_results(result: Any) -> None:
|
||||
typer.echo(str(result))
|
||||
typer.echo("---------------")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app()
|
||||
|
||||
Reference in New Issue
Block a user