Skip to content

comicbox.box.computed.stamp ΒΆ

Comicbox Computed tagger, updated_at and notes stamps.

ComicboxComputedStamp ΒΆ

Bases: ComicboxComputedPages

Comicbox Computed tagger, updated_at and notes stamps.

Source code in comicbox/box/computed/stamp.py
 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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class ComicboxComputedStamp(ComicboxComputedPages):
    """Comicbox Computed tagger, updated_at and notes stamps."""

    def _get_unparsed_comictagger_style_notes(self, sub_data: dict[str, Any]) -> str:
        """Build notes from other tags."""
        notes = ""
        if sub_data and (tagger := sub_data.get(TAGGER_KEY)):
            notes += f"Tagged with {tagger}"

        if (
            sub_data
            and (updated_at := sub_data.get(UPDATED_AT_KEY))
            and (ts := _DATETIME_FIELD._serialize(updated_at))  # noqa: SLF001
        ):
            notes += f" on {ts}"

        if sub_data and (
            comicvine_id := sub_data.get(IDENTIFIERS_KEY, {})
            .get(IdSources.COMICVINE.value, {})
            .get(ID_KEY_KEY)
        ):
            notes += f" [Issue ID {comicvine_id}]"
        return notes

    def _get_unparsed_urns_for_notes(self, sub_data: dict[str, Any]) -> str:
        """Unparse all types."""
        notes = ""
        if not sub_data:
            return notes
        identifiers = sub_data.get(IDENTIFIERS_KEY)
        if not identifiers:
            return notes
        urn_strs = set()
        for id_source, identifier in identifiers.items():
            id_key = identifier.get(ID_KEY_KEY)
            if not id_key:
                continue
            # Issue is the top level type, so an id_type only appears here
            # when the key overrode it.
            id_type = identifier.get(ID_TYPE_KEY, "")
            if urn_str := to_urn_string(id_source, id_type, id_key):
                urn_strs.add(urn_str)
        notes += " ".join(sorted(urn_strs))
        return notes

    def _get_computed_notes_stamp(
        self, sub_data: dict[str, Any], stamp_md: dict[str, Any]
    ) -> str | None:
        """Write comicbox notes to notes field if present."""
        if (
            not self._config.write.stamp_notes
            or NOTES_KEY in self._config.general.delete_keys
        ):
            return None
        if identifiers := sub_data.get(IDENTIFIERS_KEY):
            stamp_md[IDENTIFIERS_KEY] = identifiers

        comictagger_style_notes = self._get_unparsed_comictagger_style_notes(stamp_md)
        urn_notes = self._get_unparsed_urns_for_notes(stamp_md)
        notes = f"{comictagger_style_notes} {urn_notes}"
        return notes.strip()

    def _get_tagger_stamp(self, sub_data: dict[str, Any]) -> dict | None:
        """Stamp when writing or explicitly told to."""
        if not (
            self._config.write.stamp
            or self._config.all_write_formats
            or self._config.is_write_keep
            or self._config.convert.cbz
            or self._config.convert.export_formats
        ):
            return None

        delete_keys = self._config.general.delete_keys
        stamp_md = {}
        if TAGGER_KEY not in delete_keys:
            stamp_md[TAGGER_KEY] = self._config.general.tagger

        if UPDATED_AT_KEY not in delete_keys:
            stamp_md[UPDATED_AT_KEY] = datetime.now(tz=UTC)

        if notes := self._get_computed_notes_stamp(sub_data, stamp_md):
            stamp_md[NOTES_KEY] = notes

        if not stamp_md:
            return None

        return stamp_md