Skip to content

comicbox.box.computed.date ΒΆ

Comicbox Computed Date tags.

ComicboxComputedDate ΒΆ

Bases: ComicboxComputedIssue

Comicbox Computed Date tags.

Source code in comicbox/box/computed/date.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
class ComicboxComputedDate(ComicboxComputedIssue):
    """Comicbox Computed Date tags."""

    def _set_computed_from_date_cover_date(
        self, old_date: dict, computed_date: dict
    ) -> str:
        year: int | None = old_date.get(YEAR_KEY)
        month: int | None = old_date.get(MONTH_KEY)
        day: int | None = old_date.get(DAY_KEY)
        msg = ""
        if all(
            (
                year,
                month,
                day,
            )
        ):
            try:
                dt = date(year, month, day)  # pyright: ignore[reportArgumentType], # ty: ignore[invalid-argument-type]
                computed_date[COVER_DATE_KEY] = dt
            except ValueError as exc:
                msg = str(exc)
                reason = f"{self._path}: {msg}"
                logger.warning(reason)
        return msg

    @staticmethod
    def _add_date_part(
        cover_date: date, key: str, computed_date: dict, attr: str
    ) -> None:
        computed_date[key] = getattr(cover_date, attr)

    def _set_computed_from_date_parts(
        self, old_date: dict, computed_date: dict, msg: str
    ) -> None:
        if COVER_DATE_KEY not in computed_date and (
            cover_date := old_date.get(COVER_DATE_KEY)
        ):
            for key in _DATE_PART_KEYS:
                self._add_date_part(cover_date, key, computed_date, key)
        elif msg:
            # Delete bogus parts if we can't make a date out of them
            delete_keypath = ""
            if msg.startswith("month"):
                delete_keypath = "date.month"
            elif msg.startswith("day"):
                delete_keypath = "date.day"
            if delete_keypath:
                self._extra_delete_keys.add(delete_keypath)

    def _get_computed_from_date(
        self, sub_data: dict[str, Any], **_kwargs: Any
    ) -> dict[str, dict] | None:
        """Synchronize date parts and cover_date."""
        old_date = sub_data.get(DATE_KEY)
        if not old_date:
            return None
        computed_date = {}

        msg = self._set_computed_from_date_cover_date(old_date, computed_date)
        self._set_computed_from_date_parts(old_date, computed_date, msg)

        if not computed_date:
            return None
        return {DATE_KEY: computed_date}