Skip to content

comicbox.readinglists.combine.fields ¶

Merge the fields of the rows in a group, and the lists’ headers.

First wins, in rank order. The model drops empty values on load, so the first value present is the first non-empty one. mergedeep has no first-wins strategy, and a reversed replace would recurse past the one level these rules stop at, mixing two sources’ identifier values.

is_ordered(reading_list) ¶

Whether a list’s order is authoritative: unless it says it is not.

Source code in comicbox/readinglists/combine/fields.py
60
61
62
def is_ordered(reading_list: Mapping[str, Any]) -> bool:
    """Whether a list's order is authoritative: unless it says it is not."""
    return reading_list.get(ORDERED_KEY) is not False

merge_entry(entries) ¶

Merge the rows of one group, in rank order.

Source code in comicbox/readinglists/combine/fields.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def merge_entry(entries: Sequence[Mapping[str, Any]]) -> dict[str, Any]:
    """Merge the rows of one group, in rank order."""
    merged: dict[str, Any] = {}
    for entry in entries:
        for key, value in entry.items():
            if key == IDENTIFIERS_KEY:
                merged[key] = _union_ids(merged.get(key), value)
            elif key == DATE_KEY and isinstance(value, Mapping):
                merged[key] = _merge_dates(merged.get(key) or {}, value)
            elif key in _NESTED_KEYS and isinstance(value, Mapping):
                merged[key] = _merge_one_level(merged.get(key) or {}, value)
            else:
                merged.setdefault(key, value)
    return merged

merge_header(reading_lists, backbone) ¶

Merge the lists’ headers, in rank order. The name is the backbone’s.

Source code in comicbox/readinglists/combine/fields.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def merge_header(
    reading_lists: Sequence[Mapping[str, Any]], backbone: int | None
) -> dict[str, Any]:
    """Merge the lists' headers, in rank order. The name is the backbone's."""
    by_name = list(reading_lists)
    if backbone is not None:
        by_name.insert(0, by_name.pop(backbone))
    values = {NAME_KEY: _first(by_name, NAME_KEY)}
    values.update({key: _first(reading_lists, key) for key in _FIRST_HEADER_KEYS})
    if any(ORDERED_KEY in reading_list for reading_list in reading_lists):
        values[ORDERED_KEY] = any(map(is_ordered, reading_lists))
    values[SOURCES_KEY] = merge_sources(reading_lists)
    values[TAGS_KEY] = set().union(*(rl.get(TAGS_KEY) or () for rl in reading_lists))
    return {key: value for key, value in values.items() if not is_empty(value)}

merge_sources(reading_lists) ¶

Concatenate the lists’ sources in rank order.

Two with the same kind and key are one, first value per field, so a pointer to an arc and the arc fetched from there collapse into the fetched one. Sources without a key are kept apart unless equal: two CBL files with different curators are two sources.

Source code in comicbox/readinglists/combine/fields.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def merge_sources(reading_lists: Sequence[Mapping[str, Any]]) -> list[dict]:
    """
    Concatenate the lists' sources in rank order.

    Two with the same kind and key are one, first value per field, so a
    pointer to an arc and the arc fetched from there collapse into the
    fetched one. Sources without a key are kept apart unless equal: two CBL
    files with different curators are two sources.
    """
    merged: list[dict] = []
    keyed: dict[tuple[Any, Any], int] = {}
    for reading_list in reading_lists:
        for source in reading_list.get(SOURCES_KEY) or ():
            if not (key := source.get(SOURCE_KEY_KEY)):
                if source not in merged:
                    merged.append(dict(source))
                continue
            identity = (source.get(SOURCE_KIND_KEY), key)
            if (index := keyed.get(identity)) is None:
                keyed[identity] = len(merged)
                merged.append(dict(source))
            else:
                merged[index] = {**source, **merged[index]}
    return merged