Skip to content

comicbox.readinglists.registry ¶

Reading-list format registry and content detection.

A registry of its own, apart from MetadataFormats: that enum’s order is the masking order for comic metadata and its keys are collision-checked against every comic format, where cbl, json and yaml are taken. Within reading lists those tokens are free.

ReadingListFormats ¶

Bases: Enum

Reading-list file formats.

Source code in comicbox/readinglists/registry.py
35
36
37
38
39
40
41
class ReadingListFormats(Enum):
    """Reading-list file formats."""

    YAML = YAML_REGISTRATION
    JSON = JSON_REGISTRATION
    CBL = CBL_REGISTRATION
    JSON_CBL = JSON_CBL_REGISTRATION

as_text(data) ¶

Decode bytes, dropping a byte order mark.

Source code in comicbox/readinglists/registry.py
90
91
92
93
94
def as_text(data: str | bytes | bytearray) -> str:
    """Decode bytes, dropping a byte order mark."""
    if isinstance(data, bytes | bytearray):
        return bytes(data).decode("utf-8-sig", errors="replace")
    return data.removeprefix("")

detect_format(data) ¶

Detect a reading list’s format by its content, not its file name.

XML by its root element; JSON and YAML by their top-level keys. JSON is tried before YAML, since every JSON document is also YAML.

Source code in comicbox/readinglists/registry.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def detect_format(data: str | bytes | bytearray) -> ReadingListFormats | None:
    """
    Detect a reading list's format by its content, not its file name.

    XML by its root element; JSON and YAML by their top-level keys. JSON is
    tried before YAML, since every JSON document is also YAML.
    """
    text = as_text(data)
    if not text.strip():
        return None
    if (root := xml_root(text)) is not None:
        return _XML_ROOTS.get(root)
    if (document := _parse_json(text)) is not None:
        return _match_signature(document, _JSON_SIGNATURES)
    return _match_signature(_parse_yaml(text), _YAML_SIGNATURES)

get_format(fmt) ¶

Resolve a format token such as yaml, or pass a member through.

Source code in comicbox/readinglists/registry.py
62
63
64
65
66
67
68
69
70
def get_format(fmt: ReadingListFormats | str | None) -> ReadingListFormats:
    """Resolve a format token such as ``yaml``, or pass a member through."""
    if isinstance(fmt, ReadingListFormats):
        return fmt
    if isinstance(fmt, str) and (member := _TOKEN_MAP.get(fmt.strip().lower())):
        return member
    tokens = ", ".join(sorted(_TOKEN_MAP))
    reason = f"Unknown reading-list format {fmt!r}. Known formats: {tokens}."
    raise ReadingListFormatError(reason)

xml_root(text) ¶

Name an XML document’s first element, past its declaration and comments.

A scan, not one pattern: each part of the prolog is skipped by finding its end, so the time is linear in the text however it is built.

Source code in comicbox/readinglists/registry.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def xml_root(text: str) -> str | None:
    """
    Name an XML document's first element, past its declaration and comments.

    A scan, not one pattern: each part of the prolog is skipped by finding
    its end, so the time is linear in the text however it is built.
    """
    position, size, skipped = 0, len(text), True
    while skipped:
        while position < size and text[position].isspace():
            position += 1
        skipped = False
        for opener, closer in _XML_PROLOG:
            if text.startswith(opener, position):
                end = text.find(closer, position + len(opener))
                if end < 0:
                    return None
                position, skipped = end + len(closer), True
                break
    match = _XML_NAME_RE.match(text, position)
    return match.group(1) if match else None