Skip to content

comicbox.readinglists.match ¶

Match a reading list against the comics in a directory.

Each comic becomes an entry with entry_from_comic. Matching goes in two passes. First every entry takes each comic it shares a strict key with (entry_keys: an issue id, the series name, start year or volume and issue, or, weakest, the series name, cover year and issue). Then each entry that found none tries its name variants in order, among the comics no entry took strictly. Either way a comic is refused when it carries an id from a database the entry has a different id from, or a cover year more than a year off the entry’s, and a match on the volume key is refused for another year. A comic that shares only the cover-year key is refused for another series year than the entry’s or its strong matches’, and is ambiguous beside cover-year matches of another volume: listed, never tagged. An entry with no key matches nothing. A missing entry reports the comics refused that a person may want to see (Refused). The matching is pure; reading the directory is separate.

ArcAction ¶

Bases: StrEnum

What tagging one comic with a list’s arc would do.

Source code in comicbox/readinglists/match.py
464
465
466
467
468
469
class ArcAction(StrEnum):
    """What tagging one comic with a list's arc would do."""

    ADD = "add"
    RENUMBER = "renumber"
    TAGGED = "tagged"

ArcChange dataclass ¶

The arc one comic should carry, and what it carries now.

arc is the name to write under: the comic’s own spelling when it already has the arc in another case, so it never gets the arc twice. number is None for an unordered list. current is the number the comic has, when it has the arc.

Source code in comicbox/readinglists/match.py
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
@dataclass(frozen=True, slots=True)
class ArcChange:
    """
    The arc one comic should carry, and what it carries now.

    ``arc`` is the name to write under: the comic's own spelling when it
    already has the arc in another case, so it never gets the arc twice.
    ``number`` is None for an unordered list. ``current`` is the number the
    comic has, when it has the arc.
    """

    path: Path
    arc: str
    number: int | None
    action: ArcAction
    current: int | None = None

Found dataclass ¶

The comics that are entry position (from 0), in path order.

volumes holds the discriminators its ambiguous cover-year matches carry, start years or volume numbers, when it has any.

Source code in comicbox/readinglists/match.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@dataclass(frozen=True, slots=True)
class Found:
    """
    The comics that are entry ``position`` (from 0), in path order.

    ``volumes`` holds the discriminators its ambiguous cover-year matches
    carry, start years or volume numbers, when it has any.
    """

    position: int
    comics: tuple[FoundComic, ...]
    volumes: tuple[int, ...] = ()

    @property
    def ambiguous(self) -> bool:
        """Whether some of the entry's comics are of volumes it cannot tell apart."""
        return bool(self.volumes)

    @property
    def paths(self) -> tuple[Path, ...]:
        """Get the paths of the comics found."""
        return tuple(comic.path for comic in self.comics)

ambiguous property ¶

Whether some of the entry’s comics are of volumes it cannot tell apart.

paths property ¶

Get the paths of the comics found.

FoundBy ¶

Bases: StrEnum

How a comic was found: the strength of the key that matched.

Source code in comicbox/readinglists/match.py
50
51
52
53
54
55
56
57
58
59
60
61
class FoundBy(StrEnum):
    """How a comic was found: the strength of the key that matched."""

    ID = "id"
    NAME = "name"
    NAME_YEAR = "name-year"
    VARIANT = "variant"

    @property
    def strict(self) -> bool:
        """Whether this is an ``entry_keys`` match, not a name variant."""
        return self is not FoundBy.VARIANT

strict property ¶

Whether this is an entry_keys match, not a name variant.

FoundComic dataclass ¶

One comic an entry was found in, and how.

ambiguous marks a cover-year match beside others of another volume: match lists it, and does not choose between them.

Source code in comicbox/readinglists/match.py
64
65
66
67
68
69
70
71
72
73
74
75
@dataclass(frozen=True, slots=True)
class FoundComic:
    """
    One comic an entry was found in, and how.

    ``ambiguous`` marks a cover-year match beside others of another
    volume: ``match`` lists it, and does not choose between them.
    """

    path: Path
    by: FoundBy
    ambiguous: bool = False

MatchResult dataclass ¶

What a directory has of a list, and what it is missing, by position.

refused holds, for missing entries only, the comics refused for another volume or cover year that no entry found, by position, then path.

Source code in comicbox/readinglists/match.py
120
121
122
123
124
125
126
127
128
129
130
131
132
@dataclass(frozen=True, slots=True)
class MatchResult:
    """
    What a directory has of a list, and what it is missing, by position.

    ``refused`` holds, for missing entries only, the comics refused for
    another volume or cover year that no entry found, by position, then
    path.
    """

    found: tuple[Found, ...]
    missing: tuple[int, ...]
    refused: tuple[Refused, ...] = ()

Refused dataclass ¶

A comic refused for missing entry position (from 0), and why.

Source code in comicbox/readinglists/match.py
111
112
113
114
115
116
117
@dataclass(frozen=True, slots=True)
class Refused:
    """A comic refused for missing entry ``position`` (from 0), and why."""

    position: int
    path: Path
    reason: RefusedReason

RefusedReason ¶

Bases: StrEnum

Why a comic that looked like a missing entry was refused.

Source code in comicbox/readinglists/match.py
102
103
104
105
106
107
108
class RefusedReason(StrEnum):
    """Why a comic that looked like a missing entry was refused."""

    # It shares the entry's series, issue and cover year, of another volume.
    OTHER_VOLUME = "other-volume"
    # It shares a name key, and its cover year is more than a year off.
    COVER_YEAR = "cover-year"

find_arc(tags, name) ¶

Find a comic’s arc of this name, in any case.

Source code in comicbox/readinglists/match.py
490
491
492
493
494
495
496
497
498
499
def find_arc(
    tags: Mapping[str, Any], name: str
) -> tuple[str, Mapping[str, Any]] | None:
    """Find a comic's arc of this name, in any case."""
    arcs = tags.get(ARCS_KEY) or {}
    folded = name.casefold()
    for arc_name, arc in arcs.items():
        if str(arc_name).casefold() == folded:
            return str(arc_name), arc if isinstance(arc, Mapping) else {}
    return None

match_comics(reading_list, comics) ¶

Find each of a list’s entries among some comics’ tags.

comics maps each comic’s path to its tags, as a ReadResult holds them. Strict matches are settled for the whole list first, so a comic one entry is strictly is never another entry’s variant. A comic of one issue in several files is found in all, and an issue the list holds twice is found in the same comics both times. A cover-year match is refused beside strong matches of another volume, and is ambiguous beside cover-year matches of another volume; see _Index. Each missing entry reports the comics _Index.refused finds for it that no entry found.

Source code in comicbox/readinglists/match.py
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
def match_comics(
    reading_list: Mapping[str, Any], comics: Mapping[Path, Mapping[str, Any]]
) -> MatchResult:
    """
    Find each of a list's entries among some comics' tags.

    ``comics`` maps each comic's path to its tags, as a ``ReadResult``
    holds them. Strict matches are settled for the whole list first, so a
    comic one entry is strictly is never another entry's variant. A comic
    of one issue in several files is found in all, and an issue the list
    holds twice is found in the same comics both times. A cover-year match
    is refused beside strong matches of another volume, and is ambiguous
    beside cover-year matches of another volume; see ``_Index``. Each
    missing entry reports the comics ``_Index.refused`` finds for it that
    no entry found.
    """
    index = _Index(comics)
    entries = tuple(reading_list.get(ENTRIES_KEY) or ())
    strict = [index.find_strict(entry) for entry in entries]
    taken = frozenset(path for found in strict if found for path in found.paths)
    found: list[Found] = []
    missing: list[int] = []
    for position, (entry, strong) in enumerate(zip(entries, strict, strict=True)):
        if strong is not None:
            found.append(replace(strong, position=position))
        elif variants := index.find_variant(entry, taken):
            found.append(Found(position, variants))
        else:
            missing.append(position)
    refused = _refusals(index, entries, missing, found)
    return MatchResult(tuple(found), tuple(missing), refused)

plan_arcs(reading_list, comics, result, name, *, variants=False) ¶

Plan the arc each found comic should carry, in path order.

The arc is named name; its number is the entry’s place in an ordered list, the first place for an issue the list reads twice, in every copy of it. Only strict matches are planned, unless variants, and never an ambiguous one.

Source code in comicbox/readinglists/match.py
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
def plan_arcs(
    reading_list: Mapping[str, Any],
    comics: Mapping[Path, Mapping[str, Any]],
    result: MatchResult,
    name: str,
    *,
    variants: bool = False,
) -> tuple[ArcChange, ...]:
    """
    Plan the arc each found comic should carry, in path order.

    The arc is named ``name``; its number is the entry's place in an
    ordered list, the first place for an issue the list reads twice, in
    every copy of it. Only strict matches are planned, unless ``variants``,
    and never an ambiguous one.
    """
    ordered = is_ordered(reading_list)
    numbers: dict[Path, int | None] = {}
    for found in result.found:
        for comic in found.comics:
            if (variants or comic.by.strict) and not comic.ambiguous:
                numbers.setdefault(comic.path, found.position + 1 if ordered else None)
    return tuple(
        _change(path, _tags(comics[path]), name, numbers[path])
        for path in sorted(numbers)
    )