comicbox.write ¶
Public write API: single-file write_metadata and batch bulk_write.
Wraps the existing Comicbox(...).dump() plumbing with a documented, Codex-facing surface:
merge_mode:"additive"/"update"/"replace"— picks the merge strategy applied to the patch dict against the comic’s existing metadata.None(the default) defers to the config’swrite.merge_mode, itselfadditiveunless a config file or--merge-modesays otherwise. See :class:comicbox.config.settings.MergeMode.formats: which on-archive formats to write back (ComicInfo.xml, comicbox.yaml, …). Names match theMetadataFormatsenum value.delete_keys: glom key paths removed from the final metadata before writing. Merging can only add or replace — an empty patch value is pruned by the schema, so this is the only way a write can clear an existing field.dry_run: return the would-be-written payload instead of touching the archive.
bulk_write orchestrates the same call over many files via a process- global ThreadPoolExecutor (writes are I/O bound — full archive repack per CBZ). A module-level semaphore caps concurrent writes so a big batch can’t starve other disk traffic.
BulkWriteItem dataclass ¶
One unit of work for bulk_write.
Source code in comicbox/write.py
119 120 121 122 123 124 125 126 127 128 | |
WriteResult dataclass ¶
Per-file outcome of a write.
Source code in comicbox/write.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
bulk_write(items, *, workers=None, on_event=None, stop_on_error=False, cancel=None, base_config=None, preflight=True) ¶
Write metadata to many files in parallel.
Returns an iterator that MUST be drained: writes are submitted in a bounded window as results are consumed, so discarding the iterator without iterating performs no writes. Input handling, the BatchStarted event, and pool creation happen eagerly at call time. Abandoning the iterator midway never blocks: queued writes are cancelled and in-flight ones finish in their worker threads.
Yields :class:WriteResult per file in completion order, not submission order. Caller may pass a cancel :class:threading.Event; once set, no new writes start: in-flight writes run to completion and every queued file is reported with cancelled=True. With stop_on_error=True the first failed write trips the same cancellation (an internal event is created if the caller didn’t pass one). on_event receives the shared :class:comicbox.events stream (BatchStarted / FileParsed / FileError / BatchFinished); cancelled files emit no per-file event.
preflight (default on) sniffs every destination before any archive is written, on the first next() rather than at call time. It refuses a batch’s collisions up front – a destination already on disk, and two archives converging on one name – so a batch fails fast instead of repacking an archive and refusing it at the end, and the loser of an in-batch collision is decided by submission order rather than by thread race. It also fails an unreadable or unsupported file here, with the sniff’s own error. preflight=False restores the pre-5.2.0 behavior. The write path’s own destination claim stays either way, as the backstop for concurrent callers: two bulk_write calls in one process see each other only through it.
Source code in comicbox/write.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
write_metadata(path, patch, *, merge_mode=None, formats=None, delete_keys=None, dry_run=False, base_config=None) ¶
Write metadata to one comic archive.
patch is a comicbox-shaped dict — either the contents under the "comicbox" root tag, or the root-wrapped form Comicbox.to_dict returns ({"comicbox": {...}}); the wrapper is detected and unwrapped so the natural round-trip works.
merge_mode selects how the patch overlays the comic’s existing tags: "additive" (dicts recurse, lists concatenate), "replace" (dicts recurse, lists overwrite) or "update" (top level keys replaced wholesale, siblings of a patched key dropped). None defers to the config’s write.merge_mode; an explicit value always wins. See :class:comicbox.config.settings.MergeMode.
delete_keys are glom key paths (relative to the root tag, e.g. "summary" or "series.name") removed from the final merged metadata before serialization — the only way a write can clear an existing field, since empty patch values are pruned on schema load and merging never removes. They are layered onto the config’s general.delete_keys; unknown keys are ignored. The patch may be empty when delete_keys is non-empty (a pure-clear write).
formats may include "keep": the write then removes no tag format the comic already carries, and writes the ones it read back beside those named, so ["keep"] alone rewrites what the comic has. A comic with nothing to keep and none named is not written, without an error.
With dry_run=True no archive is touched; the result’s dry_run_payload carries the serialized would-be-written content per format written (keyed by format name).
Source code in comicbox/write.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |