Skip to content

comicbox.formats.base.transforms.identifiers

Identifier Fields.

identifier_from_url(url_str)

Parse an identifier out of a url comicbox recognizes.

Returns an empty result for a url from a database comicbox doesn’t know. Such a url is still kept verbatim in urls; inventing an identifier source from its hostname only produced keys nothing could look up.

Source code in comicbox/formats/base/transforms/identifiers.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def identifier_from_url(url_str: str) -> tuple[str, dict]:
    """
    Parse an identifier out of a url comicbox recognizes.

    Returns an empty result for a url from a database comicbox doesn't know.
    Such a url is still kept verbatim in ``urls``; inventing an identifier
    source from its hostname only produced keys nothing could look up.
    """
    if not url_str:
        return "", {}
    id_source_str = get_id_source_from_url(url_str)
    if not id_source_str:
        return "", {}
    id_parts = IDENTIFIER_PARTS_MAP.get(IdSources(id_source_str))
    if not id_parts:
        return "", {}
    id_type, id_key = id_parts.parse_url_path(url_str)
    identifier = create_identifier(id_source_str, id_key, id_type=id_type)
    if not identifier.get(ID_KEY_KEY):
        return "", {}
    return id_source_str, identifier

identifier_transform_from_cb(identifier_tag)

Transform comicbox identifiers to one identifier tag.

For a format like CoMet that allows a single identifier, the best ranked source wins.

Source code in comicbox/formats/base/transforms/identifiers.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def identifier_transform_from_cb(identifier_tag: str) -> MetaSpec:
    """
    Transform comicbox identifiers to one identifier tag.

    For a format like CoMet that allows a single identifier, the best ranked
    source wins.
    """
    return MetaSpec(
        key_map={identifier_tag: IDENTIFIERS_KEY},
        spec=_identifier_from_cb,
    )

identifiers_to_cb(native_identifiers, naked_id_source)

Parse identifier struct from a string or sequence.

Source code in comicbox/formats/base/transforms/identifiers.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def identifiers_to_cb(native_identifiers: Any, naked_id_source: Any) -> dict:
    """Parse identifier struct from a string or sequence."""
    comicbox_identifiers = {}
    if isinstance(native_identifiers, str):
        # A single-valued tag, like CoMet's <identifier>. Iterating the
        # string itself would parse it one character at a time.
        native_identifiers = (native_identifiers,)
    if native_identifiers:
        for native_identifier in native_identifiers:
            try:
                id_source_str, identifier = _identifier_to_cb(
                    native_identifier, naked_id_source
                )
                comicbox_identifiers[id_source_str] = identifier
            except Exception as exc:
                logger.warning(f"Parsing identifier {native_identifier}: {exc}")
    return comicbox_identifiers

identifiers_transform_to_cb(identifiers_tag, naked_id_source)

Transform identifier tags to comicbox identifiers.

Source code in comicbox/formats/base/transforms/identifiers.py
60
61
62
63
64
65
66
67
68
69
70
71
def identifiers_transform_to_cb(
    identifiers_tag: str, naked_id_source: IdSources
) -> MetaSpec:
    """Transform identifier tags to comicbox identifiers."""

    def to_cb(native_identifiers: Any) -> dict[str, dict[str, str]]:
        return identifiers_to_cb(native_identifiers, naked_id_source)

    return MetaSpec(
        key_map={IDENTIFIERS_KEY: identifiers_tag},
        spec=to_cb,
    )

urls_to_cb(native_urls)

Collect url tags verbatim, in order, without duplicates.

Source code in comicbox/formats/base/transforms/identifiers.py
126
127
128
129
130
131
132
133
134
135
def urls_to_cb(native_urls: Any) -> list[str]:
    """Collect url tags verbatim, in order, without duplicates."""
    urls: dict[str, None] = {}
    if isinstance(native_urls, str):
        native_urls = (native_urls,)
    if native_urls:
        for native_url in native_urls:
            if url_str := get_cdata(native_url):
                urls[str(url_str)] = None
    return list(urls)

urls_transform_from_cb(urls_tag)

Transform comicbox urls to url tags.

Source code in comicbox/formats/base/transforms/identifiers.py
143
144
145
def urls_transform_from_cb(urls_tag: str) -> MetaSpec:
    """Transform comicbox urls to url tags."""
    return MetaSpec(key_map={urls_tag: URLS_KEY}, spec=urls_to_cb)

urls_transform_to_cb(urls_tag)

Transform url tags to comicbox urls.

Source code in comicbox/formats/base/transforms/identifiers.py
138
139
140
def urls_transform_to_cb(urls_tag: str) -> MetaSpec:
    """Transform url tags to comicbox urls."""
    return MetaSpec(key_map={URLS_KEY: urls_tag}, spec=urls_to_cb)