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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 | class ComicboxMerge(ComicboxOnlineLookup):
"""Merge Metadata Methods."""
@staticmethod
def _order_normalized_md_by_format(
source: MetadataSources, normalized_md_list: tuple
) -> list:
"""
Order one source's normalized metadatas by format precedence.
Declared-first wins, so the buckets are seeded in reverse and the
highest-precedence format is merged last. A format the source
doesn't declare gets a bucket of its own at the end: the public
`add_metadata(md, fmt=...)` accepts any format, including the
online ones no source lists, and a fixed-key lookup raised
KeyError on all of them. Same for the `fmt is None` bucket, which
used to be dropped outright.
"""
format_dict: dict[MetadataFormats | None, list] = {
fmt: [] for fmt in reversed(source.value.formats)
}
for loaded in normalized_md_list:
format_dict.setdefault(loaded.fmt, []).append(loaded.metadata)
return [md for md_list in format_dict.values() for md in md_list]
def _merge_metadata_by_source(
self,
source: MetadataSources,
merged_sub_md: dict,
merger: type[Merger],
dropped_ids: frozenset[str],
) -> None:
"""Merge one source's metadatas into the accumulator, format order."""
normalized_md_list = self.get_normalized_metadata(source)
if not normalized_md_list:
return
for normalized_md in self._order_normalized_md_by_format(
source, normalized_md_list
):
if sub_md := normalized_md.get(ComicboxSchemaMixin.ROOT_TAG):
merge_source(merged_sub_md, _without_ids(sub_md, dropped_ids), merger)
def _self_identified_online_sources(self) -> frozenset[str]:
"""Name the online sources whose own answer carries their own id."""
found: set[str] = set()
for source, own in _OWN_ID_KEY.items():
for loaded in self.get_normalized_metadata(source) or ():
sub_md = loaded.metadata.get(ComicboxSchemaMixin.ROOT_TAG) or {}
if own in (sub_md.get(IDENTIFIERS_KEY) or {}):
found.add(own)
return frozenset(found)
def _merger_for_source(self, source: MetadataSources) -> type[Merger]:
"""
Cross-source reads are additive; only a patch honors ``merge_mode``.
Selecting every source's merger from ``write.merge_mode`` made a
plain read depend on write settings — `to_dict()` returned
different metadata under a non-default mode — and under `update`
it was lossy: `dict.update` at the root meant the last source
carrying a key dropped every earlier source's contribution to it
wholesale.
"""
if source in _PATCH_SOURCES:
return _MERGER_BY_MODE[self._config.write.merge_mode]
return AdditiveMerger
def _set_merged_metadata(self) -> None:
"""Overlay the metadatas in precedence order."""
# An explicit read.merge_order is the whole order. Unset, it is
# the MetadataSources enum order with the online sources ranked
# by run order, so the first-listed online source wins.
merged_sub_md: dict = {}
sources = self._config.read.merge_order or _rank_online_sources(
self._online_run_order(self._config.online)
)
# Whatever the order, an online source is the authority on its own
# id: Metron's cv_id, a cross-reference that can go stale, gives
# way to Comic Vine's own id when Comic Vine answered too, and
# still fills the gap when it did not. Patches are untouched.
self_identified = self._self_identified_online_sources()
for source in sources:
self._merge_metadata_by_source(
source,
merged_sub_md,
self._merger_for_source(source),
_cross_references(source, self_identified),
)
self._merged_metadata = MappingProxyType(
{ComicboxSchemaMixin.ROOT_TAG: merged_sub_md}
)
def get_merged_metadata(self) -> MappingProxyType:
"""Get merged normalized metadata."""
if not self._merged_metadata:
self.run_online_lookup()
self._set_merged_metadata()
return self._merged_metadata
|