Skip to content

comicbox.readinglists.formats.cbl.transform

Convert ComicRack .cbl reading lists to and from the model.

Tolerant on read: one bad row is skipped with a warning and the rest load, since rejecting a whole file for one row is a standing complaint about other readers.

CblTransform

Bases: ReadingListTransform

ComicRack .cbl.

Source code in comicbox/readinglists/formats/cbl/transform.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
class CblTransform(ReadingListTransform):
    """ComicRack ``.cbl``."""

    SCHEMA_CLASS = CblSchema
    IGNORED_KEYS = _IGNORED_KEYS

    # Read

    @override
    def parse_data(
        self, data: str | bytes | bytearray, text: str, *, replaced: bool
    ) -> dict[str, Any]:
        """Parse XML from its bytes, so the encoding it declares is honoured."""
        if isinstance(data, bytes | bytearray):
            try:
                document = xmltodict.parse(bytes(data))
            except ExpatError as exc:
                if not replaced:
                    raise ReadingListFormatError(describe_syntax_error(exc)) from exc
                # Bytes not in the encoding they declare, or undeclared and
                # not UTF-8: read the text they were replaced in, and say so.
            else:
                return self.from_document(document)
        return super().parse_data(data, text, replaced=replaced)

    @override
    def load_document(self, document: Mapping[str, Any]) -> dict[str, Any]:
        """Keep an empty ``<Book />`` in its place, so positions count it."""
        books = (document.get(self.root_tag) or {}).get(BOOKS_TAG)
        if isinstance(books, dict) and BOOK_TAG in books:
            items = books[BOOK_TAG]
            items = items if isinstance(items, list) else [items]
            books[BOOK_TAG] = [{} if item is None else item for item in items]
        return super().load_document(document)

    def _database_ids(
        self, position: int, databases: Sequence[Mapping[str, Any]]
    ) -> tuple[dict[str, Any], dict[str, Any]]:
        """Read a book's series and issue ids from its databases."""
        series_ids: dict[str, Any] = {}
        issue_ids: dict[str, Any] = {}
        for database in databases:
            name = str(database.get(NAME_ATTR) or "").strip()
            source = _DATABASE_SOURCES.get(name.lower())
            if source is None and (id_source := get_id_source_by_alias(name, None)):
                source = id_source.value
            if source is None:
                self.warnings.append(
                    f"Book {position}: unknown database {quote_value(name)};"
                    " its ids are dropped."
                )
                continue
            if source in series_ids or source in issue_ids:
                self.warnings.append(
                    f"Book {position}: a second {quote_value(name)} database;"
                    " its ids replace the first's."
                )
            if series_key := database.get(SERIES_ATTR):
                series_ids[source] = {ID_KEY_KEY: series_key}
            if issue_key := database.get(ISSUE_ATTR):
                issue_ids[source] = {ID_KEY_KEY: issue_key}
        return series_ids, issue_ids

    def _read_number(
        self, position: int, book: Mapping[str, Any], attr: str
    ) -> int | None:
        """
        Read a Volume or Year, or nothing.

        A negative number is no number: -1 is how ComicRack writes unknown,
        so it goes quietly, and any other is a warning, as is a non-number.
        """
        if (text := book.get(attr)) is None:
            return None
        number = _int(text)
        if number is not None and number >= 0:
            return number
        if number != UNKNOWN_NUMBER:
            self.warnings.append(
                f"Book {position}: {attr[1:]} {quote_value(text)} dropped."
            )
        return None

    def _read_numbers(
        self, position: int, book: Mapping[str, Any], entry: dict[str, Any]
    ) -> None:
        """Read Volume, also as a start year when it is one, and Year."""
        if (volume := self._read_number(position, book, VOLUME_ATTR)) is not None:
            entry[VOLUME_KEY] = {NUMBER_KEY: volume}
            if MIN_START_YEAR <= volume <= MAX_START_YEAR:
                entry[SERIES_KEY][SERIES_START_YEAR_KEY] = volume
        if (year := self._read_number(position, book, YEAR_ATTR)) is not None:
            entry[DATE_KEY] = {YEAR_KEY: year}

    def _read_book(self, position: int, book: Mapping[str, Any]) -> dict | None:
        """Read one book, or skip it with a warning."""
        series_name, number = book.get(SERIES_ATTR), book.get(NUMBER_ATTR)
        if not series_name or not number:
            self.warnings.append(f"Book {position}: no series or number; skipped.")
            return None
        entry: dict[str, Any] = {
            SERIES_KEY: {NAME_KEY: series_name},
            ISSUE_KEY: {NAME_KEY: number},
        }
        self._read_numbers(position, book, entry)
        databases = book.get(DATABASE_TAG) or ()
        series_ids, issue_ids = self._database_ids(position, databases)
        if series_ids:
            entry[SERIES_KEY][IDENTIFIERS_KEY] = series_ids
        if issue_ids:
            entry[IDENTIFIERS_KEY] = issue_ids
        return entry

    def _read_books(self, books: Sequence[Mapping[str, Any]]) -> Iterator[dict]:
        for position, book in enumerate(books, start=1):
            if (entry := self._read_book(position, book)) is not None:
                yield entry

    @override
    def to_reading_list(self, data: Mapping[str, Any]) -> dict[str, Any]:
        """Read a ``<ReadingList>``; a smart list raises."""
        root = data.get(self.root_tag) or {}
        if root.get(MATCHERS_TAG):
            reason = "A smart list: its <Matchers> select comics, they list none."
            raise SmartListError(reason)
        source = {SOURCE_KIND_KEY: SourceKind.FILE.value}
        if curator := root.get(SOURCE_TAG):
            source[SOURCE_CURATOR_KEY] = curator
        books = (root.get(BOOKS_TAG) or {}).get(BOOK_TAG) or ()
        reading_list = {
            NAME_KEY: root.get(NAME_TAG),
            START_YEAR_KEY: root.get(START_YEAR_TAG),
            SOURCES_KEY: [source],
            ENTRIES_KEY: list(self._read_books(books)),
        }
        return {key: value for key, value in reading_list.items() if value is not None}

    # Write

    @staticmethod
    def _databases(entry: Mapping[str, Any]) -> list[dict[str, str]]:
        databases = []
        for source, name in _DATABASE_NAMES.items():
            database = {NAME_ATTR: name}
            if series_key := series_id(entry, source):
                database[SERIES_ATTR] = series_key
            if issue_key := issue_id(entry, source):
                database[ISSUE_ATTR] = issue_key
            if len(database) > 1:
                databases.append(database)
        return databases

    @classmethod
    def _book(cls, entry: Mapping[str, Any]) -> dict[str, Any]:
        book: dict[str, Any] = {
            SERIES_ATTR: entry[SERIES_KEY][NAME_KEY],
            NUMBER_ATTR: entry_issue_name(entry),
        }
        if (volume := _volume(entry)) is not None:
            book[VOLUME_ATTR] = str(volume)
        if (year := _entry_year(entry)) is not None:
            book[YEAR_ATTR] = str(year)
        if databases := cls._databases(entry):
            book[DATABASE_TAG] = databases
        return book

    @override
    def from_reading_list(self, reading_list: Mapping[str, Any]) -> dict[str, Any]:
        """Write a ``<ReadingList>`` with a true ``<NumIssues>``."""
        entries = reading_list.get(ENTRIES_KEY) or ()
        books = [self._book(entry) for entry in entries if _writable(entry)]
        curator = next(
            (
                source[SOURCE_CURATOR_KEY]
                for source in reading_list.get(SOURCES_KEY) or ()
                if source.get(SOURCE_CURATOR_KEY)
            ),
            None,
        )
        root = {
            NAME_TAG: reading_list.get(NAME_KEY),
            NUM_ISSUES_TAG: len(books),
            START_YEAR_TAG: reading_list.get(START_YEAR_KEY),
            SOURCE_TAG: curator,
            BOOKS_TAG: {BOOK_TAG: books},
        }
        return {self.root_tag: root}

    # Losses

    @override
    def unwritable(self, reading_list: Mapping[str, Any]) -> tuple[int, ...]:
        """List the entries with no series name or issue: a Book needs both."""
        entries = reading_list.get(ENTRIES_KEY) or ()
        return tuple(
            index for index, entry in enumerate(entries) if not _writable(entry)
        )

from_reading_list(reading_list)

Write a <ReadingList> with a true <NumIssues>.

Source code in comicbox/readinglists/formats/cbl/transform.py
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
@override
def from_reading_list(self, reading_list: Mapping[str, Any]) -> dict[str, Any]:
    """Write a ``<ReadingList>`` with a true ``<NumIssues>``."""
    entries = reading_list.get(ENTRIES_KEY) or ()
    books = [self._book(entry) for entry in entries if _writable(entry)]
    curator = next(
        (
            source[SOURCE_CURATOR_KEY]
            for source in reading_list.get(SOURCES_KEY) or ()
            if source.get(SOURCE_CURATOR_KEY)
        ),
        None,
    )
    root = {
        NAME_TAG: reading_list.get(NAME_KEY),
        NUM_ISSUES_TAG: len(books),
        START_YEAR_TAG: reading_list.get(START_YEAR_KEY),
        SOURCE_TAG: curator,
        BOOKS_TAG: {BOOK_TAG: books},
    }
    return {self.root_tag: root}

load_document(document)

Keep an empty <Book /> in its place, so positions count it.

Source code in comicbox/readinglists/formats/cbl/transform.py
148
149
150
151
152
153
154
155
156
@override
def load_document(self, document: Mapping[str, Any]) -> dict[str, Any]:
    """Keep an empty ``<Book />`` in its place, so positions count it."""
    books = (document.get(self.root_tag) or {}).get(BOOKS_TAG)
    if isinstance(books, dict) and BOOK_TAG in books:
        items = books[BOOK_TAG]
        items = items if isinstance(items, list) else [items]
        books[BOOK_TAG] = [{} if item is None else item for item in items]
    return super().load_document(document)

parse_data(data, text, *, replaced)

Parse XML from its bytes, so the encoding it declares is honoured.

Source code in comicbox/readinglists/formats/cbl/transform.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
@override
def parse_data(
    self, data: str | bytes | bytearray, text: str, *, replaced: bool
) -> dict[str, Any]:
    """Parse XML from its bytes, so the encoding it declares is honoured."""
    if isinstance(data, bytes | bytearray):
        try:
            document = xmltodict.parse(bytes(data))
        except ExpatError as exc:
            if not replaced:
                raise ReadingListFormatError(describe_syntax_error(exc)) from exc
            # Bytes not in the encoding they declare, or undeclared and
            # not UTF-8: read the text they were replaced in, and say so.
        else:
            return self.from_document(document)
    return super().parse_data(data, text, replaced=replaced)

to_reading_list(data)

Read a <ReadingList>; a smart list raises.

Source code in comicbox/readinglists/formats/cbl/transform.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
@override
def to_reading_list(self, data: Mapping[str, Any]) -> dict[str, Any]:
    """Read a ``<ReadingList>``; a smart list raises."""
    root = data.get(self.root_tag) or {}
    if root.get(MATCHERS_TAG):
        reason = "A smart list: its <Matchers> select comics, they list none."
        raise SmartListError(reason)
    source = {SOURCE_KIND_KEY: SourceKind.FILE.value}
    if curator := root.get(SOURCE_TAG):
        source[SOURCE_CURATOR_KEY] = curator
    books = (root.get(BOOKS_TAG) or {}).get(BOOK_TAG) or ()
    reading_list = {
        NAME_KEY: root.get(NAME_TAG),
        START_YEAR_KEY: root.get(START_YEAR_TAG),
        SOURCES_KEY: [source],
        ENTRIES_KEY: list(self._read_books(books)),
    }
    return {key: value for key, value in reading_list.items() if value is not None}

unwritable(reading_list)

List the entries with no series name or issue: a Book needs both.

Source code in comicbox/readinglists/formats/cbl/transform.py
313
314
315
316
317
318
319
@override
def unwritable(self, reading_list: Mapping[str, Any]) -> tuple[int, ...]:
    """List the entries with no series name or issue: a Book needs both."""
    entries = reading_list.get(ENTRIES_KEY) or ()
    return tuple(
        index for index, entry in enumerate(entries) if not _writable(entry)
    )