56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 | class ComicboxComputedIdentifiers(ComicboxComputedNotes):
"""Comicbox computed identifiers."""
@staticmethod
def _add_identifier_from_tag(tag: str, identifiers: dict) -> None:
# Silently fail because most tags are not identifiers
id_source, id_type, id_key = parse_urn_identifier(tag)
if not (id_source and id_key):
id_source, id_type, id_key = parse_identifier_other_str(tag)
if id_source and id_key:
identifiers[id_source.value] = create_identifier(
id_source.value, id_key, id_type=id_type
)
def _get_computed_from_tags(
self, sub_data: dict[str, Any]
) -> dict[str, dict] | None:
# only look for ids in tags and genres if the format has tags but no designated id field.
if (
not sub_data
or self._config.is_skip_computed_from_tags
or _PARSE_AS_IDENTIFIERS.issubset(self._config.general.delete_keys)
):
return None
identifiers = {}
for key in _PARSE_AS_IDENTIFIERS:
if tags := sub_data.get(key):
for tag in tags:
self._add_identifier_from_tag(tag, identifiers)
if not identifiers:
return None
return {IDENTIFIERS_KEY: identifiers}
@staticmethod
def _key_deltas_for_tag_identifiers(
id_type: str, identifiers: Mapping | None
) -> dict[str, dict[str, str]]:
"""
Clean hand-tagged identifier keys for one identifiers map.
Returns ``{source: {"key": ...}}`` deltas instead of writing into the
input: computed actions must not mutate the merged metadata they
derive from (it's cached); the action's return value is merged by its
declared merger like every other action.
Hand-tagged keys often mirror the urn form comicbox writes to notes,
carrying a type prefix ("series:178012") or a comicvine long code.
Keys read through a format transform were cleaned on the way in; these
are the ones written by hand.
"""
deltas: dict[str, dict[str, str]] = {}
if not identifiers:
return deltas
for id_source_str, identifier in identifiers.items():
if not (id_key := identifier.get(ID_KEY_KEY)):
continue
old_id_type = identifier.get(ID_TYPE_KEY) or id_type
norm_id_type, norm_id_key = normalize_key(
id_source_str, old_id_type, id_key
)
delta = {}
if norm_id_key != id_key:
delta[ID_KEY_KEY] = norm_id_key
if norm_id_type != old_id_type:
# The prefix in the key named a type. Keeping it is what
# decides which url the key builds.
delta[ID_TYPE_KEY] = norm_id_type
if delta:
deltas[id_source_str] = delta
return deltas
@classmethod
def _key_deltas_for_tag(cls, id_type: str, tag: Mapping | None) -> dict | None:
"""Delta subtree for one identified tag, or None when clean."""
if not tag:
return None
deltas = cls._key_deltas_for_tag_identifiers(id_type, tag.get(IDENTIFIERS_KEY))
return {IDENTIFIERS_KEY: deltas} if deltas else None
@classmethod
def _key_deltas_for_multiple_tags(cls, key: str, all_tags: Mapping) -> dict:
"""Delta subtrees for a name-keyed tag map (credits incl. roles)."""
id_type = _IRREGULAR_SINGULAR_ID_TYPES.get(key, key[:-1])
tag_deltas: dict[str, Any] = {}
for name, tag in all_tags.items():
delta = cls._key_deltas_for_tag(id_type, tag) or {}
if key == CREDITS_KEY and tag and (roles := tag.get(ROLES_KEY)):
role_deltas = {
role_name: role_delta
for role_name, role in roles.items()
if (role_delta := cls._key_deltas_for_tag(id_type, role))
}
if role_deltas:
delta = {**delta, ROLES_KEY: role_deltas}
if delta:
tag_deltas[name] = delta
return tag_deltas
def _normalize_all_identifier_keys(self, sub_data: dict[str, Any]) -> dict | None:
"""Clean hand-tagged identifier keys as a mergeable delta tree."""
tree: dict[str, Any] = {}
if deltas := self._key_deltas_for_tag_identifiers(
"issue", sub_data.get(IDENTIFIERS_KEY)
):
tree[IDENTIFIERS_KEY] = deltas
for key in _IDENTIFIED_KEYS:
if delta := self._key_deltas_for_tag(key, sub_data.get(key)):
tree[key] = delta
for key in _IDENTIFIED_TAG_KEYS:
all_tags = sub_data.get(key)
if not all_tags:
continue
if tag_deltas := self._key_deltas_for_multiple_tags(key, all_tags):
tree[key] = tag_deltas
return tree or None
|