Skip to content

comicbox.predict

Public prediction API: where a write would land, before anything is written.

write_metadata(dry_run=True) previews the payload and deliberately keeps reporting final_path=None; it says nothing about the destination. This module is the destination preview.

WriteDestination dataclass

Where a write of one archive would land.

Source code in comicbox/predict.py
21
22
23
24
25
26
27
28
29
30
31
32
33
@dataclass(frozen=True, slots=True)
class WriteDestination:
    """Where a write of one archive would land."""

    # The path as submitted.
    path: Path
    # Where the write lands; equal to ``path`` for an in-place write.
    destination: Path
    # True when the write would repack to a new file rather than rewrite
    # the archive in place.
    converts: bool
    # True when ``converts`` and something already occupies ``destination``.
    occupied: bool

predict_write_destination(path, *, base_config=None)

Report where a write to path would land, and whether that path is taken.

The archive’s sniffed type decides, never its suffix: a RAR named X.cbz reports converts=False because its repack lands on its own name, and a zip named X.cbr is rewritten in place.

Opens nothing. Construction sniffs the archive type from the file’s leading bytes and stops there – no member listing, no decompression, no schema load – so this is cheap enough to run over a whole batch before any of it is written.

Raises whatever Comicbox construction raises and leaves the handling to the caller: FileNotFoundError and IsADirectoryError for a path that is not a readable file, and :class:comicbox.exceptions.UnsupportedArchiveTypeError for bytes that are no archive comicbox knows.

Pass a prebuilt base_config when predicting a batch. None rebuilds the config from files and environment on every call, which is fine for one file and wasteful for many.

Source code in comicbox/predict.py
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
70
71
72
def predict_write_destination(
    path: Path | str, *, base_config: ComicboxSettings | None = None
) -> WriteDestination:
    """
    Report where a write to ``path`` would land, and whether that path is taken.

    The archive's sniffed type decides, never its suffix: a RAR named
    ``X.cbz`` reports ``converts=False`` because its repack lands on its
    own name, and a zip named ``X.cbr`` is rewritten in place.

    Opens nothing. Construction sniffs the archive type from the file's
    leading bytes and stops there -- no member listing, no decompression,
    no schema load -- so this is cheap enough to run over a whole batch
    before any of it is written.

    Raises whatever ``Comicbox`` construction raises and leaves the
    handling to the caller: ``FileNotFoundError`` and
    ``IsADirectoryError`` for a path that is not a readable file, and
    :class:`comicbox.exceptions.UnsupportedArchiveTypeError` for bytes
    that are no archive comicbox knows.

    Pass a prebuilt ``base_config`` when predicting a batch. ``None``
    rebuilds the config from files and environment on every call, which
    is fine for one file and wasteful for many.
    """
    box = Comicbox(path, config=base_config)
    # get_write_destination() raises without a path, so get_path() is set
    # by the time the fallback would matter.
    destination = box.get_write_destination()
    submitted = box.get_path() or Path(path)
    converts = destination != submitted
    return WriteDestination(
        path=submitted,
        destination=destination,
        converts=converts,
        occupied=converts and destination.is_file(),
    )