Files
stack/src/cli/focus.py

91 lines
2.5 KiB
Python

"""stack focus — deep-focus annotations."""
from __future__ import annotations
from pathlib import Path
import typer
app = typer.Typer(no_args_is_help=True)
_DEFAULT_DB = Path("data/zotero/data/zotero.sqlite")
def _live_store(): # pragma: no cover
"""The annotation store on the llm database."""
from focus.schema import ensure
from focus.store import Store
from llm.config import load as load_config
from llm.index import _engine
engine = _engine(load_config())
ensure(engine)
return Store(engine), engine
@app.command("export-notes")
def export_notes_cmd(
protocol: str | None = typer.Option(
None,
"--protocol",
help="Only this protocol. Default: every protocol.",
),
write: bool = typer.Option(
False,
"--write",
help="Write notes. Without this flag the command only reports.",
),
db: Path | None = typer.Option(
None,
"--db",
help=(
f"Zotero sqlite. Default: {_DEFAULT_DB}. "
"Do not pass --write while Zotero is running."
),
),
focus_db: str = typer.Option(
"",
"--focus-db",
help="SQLAlchemy URL for the annotation store. Default: the llm database.",
),
) -> None:
"""Export human highlights to one Zotero note per item and protocol.
Dry-run unless ``--write``. Does not schedule itself and does not
write labels.sqlite. Stop Zotero before ``--write`` on the live database.
"""
from sqlalchemy import create_engine
from focus.export import export_notes
from focus.schema import ensure
from focus.store import Store
from zot.db import Db
path = db or _DEFAULT_DB
if not path.is_file():
raise typer.BadParameter(f"Zotero SQLite not found: {path}")
if focus_db:
engine = create_engine(focus_db)
ensure(engine)
store = Store(engine)
else: # pragma: no cover
store, engine = _live_store()
try:
with Db(str(path)) as zot:
try:
summary = export_notes(
store,
zot,
protocol=protocol or None,
dry_run=not write,
)
except ValueError as exc:
typer.echo(str(exc), err=True)
raise typer.Exit(code=1) from exc
if write:
zot.commit()
finally:
engine.dispose()
for key in ("dry_run", "create", "replace", "skipped_no_zotero_key"):
typer.echo(f"{key}: {summary[key]}")