Collect web urls and the ids they contain.
Two actions at the head of the computed pipeline, in this order.
Urls hide in the notes field, because most formats have nowhere else to put one and taggers write them into their notes text. A url there is still a url, so it is collected into the urls list before anything else looks at it. That also saves it: the tagger stamp rewrites notes, and a url that lived only in the prose it replaces would be gone.
Then the ids are read out of every url, before the weaker places an id can hide. A url naming a database’s own id path states that id more plainly than notes prose or a tag does, so it is filled in first and those sources only fill what it left. An id the file states outright still beats all of them.
The other direction, synthesizing a url for an identifier that lacks one, runs at the far end of the pipeline in urls.
ComicboxComputedUrlIdentifiers
Bases: ComicboxMerge
Collect urls written into notes and the ids that urls contain.
Source code in comicbox/box/computed/url_identifiers.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 | class ComicboxComputedUrlIdentifiers(ComicboxMerge):
"""Collect urls written into notes and the ids that urls contain."""
def _get_computed_urls_from_notes(
self, sub_data: dict[str, Any], **_kwargs: Any
) -> dict[str, list] | None:
"""Collect the web urls a tagger wrote into the notes text."""
if not sub_data:
return None
notes = sub_data.get(NOTES_KEY)
if not notes:
return None
old_urls = frozenset(str(url) for url in sub_data.get(URLS_KEY) or ())
new_urls: dict[str, None] = {}
for match in _NOTES_URL_RE.finditer(notes):
url = match.group().rstrip(_URL_TRAILING_CHARS)
if url and url not in old_urls:
new_urls[url] = None
if not new_urls:
return None
return {URLS_KEY: list(new_urls)}
@staticmethod
def _url_holds_its_key(
id_source_str: str, identifier: dict[str, str], url: str
) -> bool:
"""
Say whether a url contains its database's id rather than a slug.
A url that rebuilds itself from the key it parsed to is the canonical
form for that key. When a comic carries both a slug url and a
canonical one for the same database, the canonical one names the id.
"""
return get_url_from_identifier(id_source_str, identifier) == url
def _get_computed_identifiers_from_urls(
self, sub_data: dict[str, Any], **_kwargs: Any
) -> dict[str, dict] | None:
"""
Recognize database urls and record the ids they contain.
An id the file states outright always wins: for several databases
the url path is a slug, not the id, so it must not overwrite a real
key. Among a database's urls, one that holds its id beats a slug,
and between equals the first in the file wins, so a comic tagged
twice from one database reads back the same id every time.
"""
urls = sub_data.get(URLS_KEY) if sub_data else None
if not urls:
return None
old_identifiers = sub_data.get(IDENTIFIERS_KEY) or {}
found: list[tuple[bool, str, dict]] = []
for url in map(str, urls):
id_source_str, identifier = identifier_from_url(url)
if id_source_str and identifier and id_source_str not in old_identifiers:
is_slug = not self._url_holds_its_key(id_source_str, identifier, url)
found.append((is_slug, id_source_str, identifier))
new_identifiers: dict[str, dict] = {}
# A stable sort: urls that hold their id first, file order within.
for _is_slug, id_source_str, identifier in sorted(found, key=itemgetter(0)):
new_identifiers.setdefault(id_source_str, identifier)
if not new_identifiers:
return None
return {IDENTIFIERS_KEY: new_identifiers}
|