Skip to content

comicbox.readinglists.library ¶

Read a directory of comics, to match a list against them and tag them.

Each comic is read in a pool of worker processes. Besides its tags, a read records the tag formats the comic carries, so a write can name the ones it removes, and the tag files comicbox could not parse: a write would replace them with what was read from the rest, so such a comic is never written.

The workers never take Ctrl-C: it goes to this process alone, which stops the read. Before, every worker printed its own traceback.

Comic dataclass ¶

One comic’s tags, and its tag files.

formats are the tag formats it carries that comicbox read, and unreadable names the tag files it carries that comicbox could not parse.

Source code in comicbox/readinglists/library.py
41
42
43
44
45
46
47
48
49
50
51
52
53
@dataclass(frozen=True, slots=True)
class Comic:
    """
    One comic's tags, and its tag files.

    ``formats`` are the tag formats it carries that comicbox read, and
    ``unreadable`` names the tag files it carries that comicbox could not
    parse.
    """

    tags: dict[str, Any]
    formats: frozenset[MetadataFormats] = frozenset()
    unreadable: tuple[str, ...] = ()

Library dataclass ¶

The comics under a directory, by path, and why any directory went unread.

Source code in comicbox/readinglists/library.py
56
57
58
59
60
61
@dataclass(frozen=True, slots=True)
class Library:
    """The comics under a directory, by path, and why any directory went unread."""

    comics: dict[Path, Comic] = field(default_factory=dict)
    unread_directories: dict[Path, OSError] = field(default_factory=dict)

comic_paths(directory, on_error) ¶

List the comic archives under a directory, in path order.

A linked file is listed; a linked directory is not searched, as with comicbox’s --recurse. A directory that cannot be read goes to on_error.

Source code in comicbox/readinglists/library.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def comic_paths(
    directory: Path, on_error: Callable[[Path, OSError], None]
) -> list[Path]:
    """
    List the comic archives under a directory, in path order.

    A linked file is listed; a linked directory is not searched, as with
    comicbox's ``--recurse``. A directory that cannot be read goes to
    ``on_error``.
    """

    def report(error: OSError) -> None:
        on_error(Path(error.filename) if error.filename else directory, error)

    paths = [
        Path(root) / name
        for root, _directories, names in os.walk(directory, onerror=report)
        for name in names
        if Path(name).suffix.lower() in _COMIC_SUFFIXES
    ]
    return sorted(path for path in paths if path.is_file())

read_comics(directory, config=None, on_error=None, *, log_level='INFO') ¶

Read every comic under a directory, in path order.

A comic that cannot be read goes to on_error and the rest are read; a directory that cannot be read is in unread_directories. A comic with no tags is left out: there is nothing to match it by. log_level is the workers’ logging level.

Source code in comicbox/readinglists/library.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def read_comics(
    directory: Path,
    config: "ComicboxSettings | None" = None,
    on_error: OnError | None = None,
    *,
    log_level: str = "INFO",
) -> Library:
    """
    Read every comic under a directory, in path order.

    A comic that cannot be read goes to ``on_error`` and the rest are read;
    a directory that cannot be read is in ``unread_directories``. A comic
    with no tags is left out: there is nothing to match it by.
    ``log_level`` is the workers' logging level.
    """
    unread: dict[Path, OSError] = {}
    comics: dict[Path, Comic] = {}
    if paths := comic_paths(directory, unread.__setitem__):
        for path, read in _read_all(paths, config, log_level):
            if isinstance(read, BaseException):
                if on_error is not None:
                    on_error(path, read)
            elif read.tags:
                comics[path] = read
    return Library(
        {path: comics[path] for path in sorted(comics)},
        {path: unread[path] for path in sorted(unread)},
    )