Decide how write-arcs writes each comic, or why it does not.
A format that cannot hold the arc is refused before anything is read: the check writes a comic holding only the arc to the format, reads it back and compares. -w means what it means for comicbox: the formats named are written and every other tag file is removed, so each write names what it removes. A CBR, CBT or CB7 is converted to a CBZ, as comicbox converts it. A comic with a tag file comicbox could not parse is not written, nor one whose CBZ would land on a file already there, and a file found under several paths is written once.
ArcFit
Bases: StrEnum
What a tag format does with an arc written to it.
Source code in comicbox/readinglists/tagging.py
64
65
66
67
68
69
70
71
72 | class ArcFit(StrEnum):
"""What a tag format does with an arc written to it."""
HOLDS = "holds"
# The format has no arcs: the write leaves the arc out of it.
NONE = "none"
# It reads back as something else, such as a ComicInfo arc whose comma
# splits it in two: the write would put the wrong arcs in it.
MANGLES = "mangles"
|
ArcFits
arc_fit, tried once per format and arc name in a run.
Source code in comicbox/readinglists/tagging.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 | class ArcFits:
"""``arc_fit``, tried once per format and arc name in a run."""
def __init__(
self, *, ordered: bool, config: "ComicboxSettings | None" = None
) -> None:
"""Check numbered arcs for an ordered list, unnumbered otherwise."""
self._number = 1 if ordered else None
self._config = config
self._fits: dict[tuple[MetadataFormats, str], ArcFit] = {}
def __call__(self, fmt: MetadataFormats, name: str) -> ArcFit:
"""Say what a format does with this arc."""
key = (fmt, name)
if (fit := self._fits.get(key)) is None:
fit = arc_fit(fmt, name, self._number, self._config)
self._fits[key] = fit
return fit
|
__call__(fmt, name)
Say what a format does with this arc.
Source code in comicbox/readinglists/tagging.py
115
116
117
118
119
120
121 | def __call__(self, fmt: MetadataFormats, name: str) -> ArcFit:
"""Say what a format does with this arc."""
key = (fmt, name)
if (fit := self._fits.get(key)) is None:
fit = arc_fit(fmt, name, self._number, self._config)
self._fits[key] = fit
return fit
|
__init__(*, ordered, config=None)
Check numbered arcs for an ordered list, unnumbered otherwise.
Source code in comicbox/readinglists/tagging.py
107
108
109
110
111
112
113 | def __init__(
self, *, ordered: bool, config: "ComicboxSettings | None" = None
) -> None:
"""Check numbered arcs for an ordered list, unnumbered otherwise."""
self._number = 1 if ordered else None
self._config = config
self._fits: dict[tuple[MetadataFormats, str], ArcFit] = {}
|
ArcWrite dataclass
How one file gets its arc: the formats to write, or why it is not.
formats is empty when nothing is written, and reason then says why, unless the comic already has the arc. removes names the tag files the write takes out of the comic. converts_to is the CBZ a CBR, CBT or CB7 is converted to, and deletes_original whether the original then goes. same_file lists the other paths of this file: when the original goes, so do they.
Source code in comicbox/readinglists/tagging.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 | @dataclass(frozen=True, slots=True)
class ArcWrite:
"""
How one file gets its arc: the formats to write, or why it is not.
``formats`` is empty when nothing is written, and ``reason`` then says
why, unless the comic already has the arc. ``removes`` names the tag
files the write takes out of the comic. ``converts_to`` is the CBZ a CBR,
CBT or CB7 is converted to, and ``deletes_original`` whether the
original then goes. ``same_file`` lists the other paths of this file:
when the original goes, so do they.
"""
change: ArcChange
formats: frozenset[MetadataFormats] = frozenset()
reason: str | None = None
same_file: tuple[Path, ...] = ()
removes: tuple[str, ...] = ()
converts_to: Path | None = None
deletes_original: bool = False
@property
def loses(self) -> bool:
"""Whether the write takes a tag file away."""
return bool(self.removes)
@property
def path(self) -> Path:
"""Get the path written."""
return self.change.path
|
loses property
Whether the write takes a tag file away.
arc_fit(fmt, name, number, config=None)
Write a comic holding only this arc in a format, read it back, compare.
Source code in comicbox/readinglists/tagging.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 | def arc_fit(
fmt: MetadataFormats,
name: str,
number: int | None,
config: "ComicboxSettings | None" = None,
) -> ArcFit:
"""Write a comic holding only this arc in a format, read it back, compare."""
from comicbox.box import Comicbox
arc = {} if number is None else {NUMBER_KEY: number}
metadata = {
ROOT_TAG: {
SERIES_KEY: {NAME_KEY: "Arc check"},
ISSUE_KEY: {NAME_KEY: "1"},
ARCS_KEY: {name: arc},
}
}
try:
text = Comicbox(metadata=metadata, config=config).to_string(fmt)
read = Comicbox(metadata=text, fmt=fmt, config=config).to_dict()
except Exception:
# A format that cannot even write this arc cannot hold it.
return ArcFit.MANGLES
arcs = (read.get(ROOT_TAG) or {}).get(ARCS_KEY)
if not arcs:
return ArcFit.NONE
return ArcFit.HOLDS if arcs == {name: arc} else ArcFit.MANGLES
|
Name a tag format as a comic carries it: its file, or what it is.
Source code in comicbox/readinglists/tagging.py
| def format_name(fmt: MetadataFormats) -> str:
"""Name a tag format as a comic carries it: its file, or what it is."""
return _FORMAT_NAMES.get(fmt, fmt.value.filename)
|
Name some tag formats, in a stable order.
Source code in comicbox/readinglists/tagging.py
| def format_names(formats: Collection[MetadataFormats]) -> str:
"""Name some tag formats, in a stable order."""
return ", ".join(sorted(format_name(fmt) for fmt in formats))
|
group_files(paths, key)
Group paths by the file they are; each group’s first path names it.
The first is the first in path order that is not a symbolic link, or the first link when all are. Returns each first path with the other paths of its file.
Source code in comicbox/readinglists/tagging.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 | def group_files(
paths: Collection[Path], key: Callable[[Path], Any]
) -> Mapping[Path, tuple[Path, ...]]:
"""
Group paths by the file they are; each group's first path names it.
The first is the first in path order that is not a symbolic link, or
the first link when all are. Returns each first path with the other
paths of its file.
"""
groups: dict[Any, list[Path]] = {}
for path in sorted(paths):
groups.setdefault(key(path), []).append(path)
named = {}
for group in groups.values():
first = min(group, key=lambda path: (path.is_symlink(), path))
named[first] = tuple(path for path in group if path != first)
return named
|
plan_write(change, comic, formats, destination, *, delete_orig=False, keep=False, fits=None)
Decide how to write one comic’s arc change in formats.
destination is where comicbox would write it, as predict_write_destination says; None for a comic already tagged. With keep the comic is written in formats and every format it carries, and removes none; fits says what a kept one does with the arc. At least one written must hold it, and none kept may read it back as other arcs.
Source code in comicbox/readinglists/tagging.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 | def plan_write(
change: ArcChange,
comic: "Comic",
formats: frozenset[MetadataFormats],
destination: "WriteDestination | None",
*,
delete_orig: bool = False,
keep: bool = False,
fits: Callable[[MetadataFormats], ArcFit] | None = None,
) -> ArcWrite:
"""
Decide how to write one comic's arc change in ``formats``.
``destination`` is where comicbox would write it, as
``predict_write_destination`` says; None for a comic already tagged.
With ``keep`` the comic is written in ``formats`` and every format it
carries, and removes none; ``fits`` says what a kept one does with the
arc. At least one written must hold it, and none kept may read it back
as other arcs.
"""
if change.action is ArcAction.TAGGED:
return ArcWrite(change)
if reason := _unwritable(comic, destination):
return ArcWrite(change, reason=reason)
written, removed = _formats_written(comic, formats, keep=keep)
if keep and (reason := _keep_refusal(comic, formats, written, fits)):
return ArcWrite(change, reason=reason)
converts_to = _converts_to(destination)
return ArcWrite(
change,
written,
removes=tuple(sorted(format_name(fmt) for fmt in removed)),
converts_to=converts_to,
deletes_original=converts_to is not None and delete_orig,
)
|
Get the tag formats a write of written removes from a comic.
Source code in comicbox/readinglists/tagging.py
| def removed_formats(
carried: Collection[MetadataFormats], written: Collection[MetadataFormats]
) -> frozenset[MetadataFormats]:
"""Get the tag formats a write of ``written`` removes from a comic."""
return frozenset(carried) - frozenset(written) - _KEPT_UNLESS_WRITTEN
|