comicbox.formats.base.online.selector

[docs] module comicbox.formats.base.online.selector

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
SelectorCallback API for ambiguous-match resolution.

A `SelectorCallback` receives a profile and a list of ranked
`Candidate`s and returns one of:

- `("choose", index)` — pick the i-th candidate.
- `("skip", None)` — drop this file silently.
- `("manual", "<source>:<id>")` — re-tag from an explicit id; falls
  through to the explicit-id code path.
- `("abort", None)` — abort the entire run.
- `("set_unattended", None)` — switch the rest of the session to
  unattended mode. The box re-resolves the current candidates (which
  collapses to SKIP under unattended) without auto-writing this
  prompt's selection.
- `("set_policy", "<name>")` — switch the rest of the session's match
  policy to one of `ask | careful | auto | eager` (a
  :class:`MatchMode` value). The box re-resolves the current
  candidates under the new policy; if the new policy auto-writes,
  the current candidate set's top is written; otherwise the user is
  re-prompted (or the run goes silent under unattended).

The `set_*` actions are flat in the API so programmatic callers
(codex, library consumers) can drive session state from one return
value. The default CLI selector hides them under a nested "Options"
menu to keep the top-level prompt uncluttered.

Comicbox ships a default CLI implementation in `prompt.py` (uses
`questionary`). Programmatic callers (codex, library users) provide
their own.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, Literal, TypeAlias

if TYPE_CHECKING:
    from collections.abc import Callable, Sequence
    from pathlib import Path

    from comicbox.config.settings import ComicboxSettings
    from comicbox.formats.base.online.profile import Candidate, ComicProfile


SelectorAction: TypeAlias = Literal[
    "choose",
    "skip",
    "manual",
    "abort",
    "set_unattended",
    "set_policy",
]
SelectorResult: TypeAlias = tuple[SelectorAction, "int | str | None"]


@dataclass(frozen=True, slots=True)
class SelectorContext:
    """Context passed alongside the candidates to a selector callback."""

    file_path: Path | None
    source: str
    settings: ComicboxSettings
    triggered_hashing: bool


SelectorCallback: TypeAlias = (
    "Callable[[ComicProfile, Sequence[Candidate], SelectorContext], SelectorResult]"
)