Files
comictagger/comicapi/merge.py
Timmy Welch 9137cee3cd Fix edgecases with filerenaming
Add warnings in gui for sub-optimal rename templates
Fix using lists when renaming
Explicitly disallow fields starting with `_`
Mark credit roles with prefix `credit_` so that values will properly
  return None for unkown roles
Add `credit_item_` prefix to allow direct usage of the credit class

Make filename tests more readable
2025-08-10 19:42:40 -07:00

58 lines
1.5 KiB
Python

from __future__ import annotations
from collections.abc import Collection
from enum import auto
from typing import Any, Callable
from comicapi.utils import DefaultDict, StrEnum, norm_fold
class Mode(StrEnum):
OVERLAY = auto()
ADD_MISSING = auto()
def merge_lists(old: Collection[Any], new: Collection[Any]) -> list[Any] | set[Any]:
"""Dedupes normalised (NFKD), casefolded values using 'new' values on collisions"""
if not new:
return old if isinstance(old, set) else list(old)
if not old:
return new if isinstance(new, set) else list(new)
# Create dict to preserve case
new_dict = {norm_fold(str(n)): n for n in new}
old_dict = {norm_fold(str(c)): c for c in old}
old_dict.update(new_dict)
if isinstance(old, set):
return set(old_dict.values())
return list(old_dict.values())
def overlay(old: Any, new: Any) -> Any:
"""overlay - When the `new` object is not empty, replace `old` with `new`."""
if new is None or (isinstance(new, Collection) and len(new) == 0):
return old
return new
attribute: DefaultDict[Mode, Callable[[Any, Any], Any]] = DefaultDict(
{
Mode.OVERLAY: overlay,
Mode.ADD_MISSING: lambda old, new: overlay(new, old),
},
default=lambda x: overlay,
)
lists: DefaultDict[Mode, Callable[[Collection[Any], Collection[Any]], list[Any] | set[Any]]] = DefaultDict(
{
Mode.OVERLAY: merge_lists,
Mode.ADD_MISSING: lambda old, new: merge_lists(new, old),
},
default=lambda x: overlay,
)