Skip to content

comicbox.box.computed.pages ΒΆ

Comicbox Computed Pages.

ComicboxComputedPages ΒΆ

Bases: ComicboxComputedUrls

Comicbox Computed Pages.

Source code in comicbox/box/computed/pages.py
 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class ComicboxComputedPages(ComicboxComputedUrls):
    """Comicbox Computed Pages."""

    def _enable_page_compute_attribute(self, key: str, sub_md: Mapping) -> bool:
        """Determine if we should compute this attribute."""
        if key in self._config.general.delete_keys or not sub_md or not self._path:
            return False
        formats = frozenset(self._config.all_write_formats | self._dict_formats)
        if self._config.is_write_keep:
            # The formats a keep write writes are known only per comic.
            formats |= self.get_carried_formats().formats
        compute_attr, schema_attr = _ENABLE_PAGE_COMPUTE_ATTRS[key]
        return getattr(self._config.compute, compute_attr, False) and (
            any(getattr(fmt.value.schema_class, schema_attr, False) for fmt in formats)
        )

    def _get_computed_page_count_metadata(
        self, sub_md: dict[str, Any]
    ) -> dict[str, Any] | None:
        """
        Compute page_count from page_filenames.

        Allow for extra images in the archive that are not pages.
        """
        if not self._enable_page_compute_attribute(PAGE_COUNT_KEY, sub_md):
            return None
        md_page_count = sub_md.get(PAGE_COUNT_KEY)
        real_page_count = self.get_page_count()
        if md_page_count != real_page_count:
            return {PAGE_COUNT_KEY: real_page_count}
        return None

    @staticmethod
    def _ensure_pages_front_cover_metadata(
        pages: MutableMapping[int, dict[str, Any]],
    ) -> None:
        """Ensure there is a FrontCover page type in pages."""
        if not pages:
            return
        for page in pages.values():
            if page.get(PAGE_TYPE_KEY) == ComicInfoPageTypeEnum.FRONT_COVER:
                return

        # Index 0 is the front cover when it's there, but it need not be:
        # a page whose archive entry reported no size gets no entry at all,
        # and `pages[0]` then raised a KeyError that aborted the entire
        # computed pass. The lowest page present is the cover.
        pages[min(pages)][PAGE_TYPE_KEY] = ComicInfoPageTypeEnum.FRONT_COVER

    def _get_max_page_index(self) -> int:
        if self._path:
            max_page_index = self.get_page_count() - 1
        else:
            # don't strip pages if no path given
            logger.debug("No path given, not computing real pages.")
            max_page_index = maxsize
        return max_page_index

    def _get_computed_merged_pages_metadata(
        self, md: dict[str, Any], pages: dict[int, dict[str, Any]]
    ) -> MutableMapping[int, dict[str, Any]]:
        old_pages: dict[int, dict[str, Any]] = md.get(PAGES_KEY, {})
        max_page_index = self._get_max_page_index()
        trimmed_old_pages = {k: v for k, v in old_pages.items() if k <= max_page_index}
        computed_pages: MutableMapping[int, dict[str, Any]] = AdditiveMerger.merge(
            trimmed_old_pages, pages
        )
        self._ensure_pages_front_cover_metadata(computed_pages)
        return computed_pages

    def _get_computed_pages_metadata(
        self, sub_md: dict[str, Any]
    ) -> dict[str, MutableMapping] | None:
        """Recompute the tag image sizes for the ComicRack PageInfo list."""
        if not self._enable_page_compute_attribute(PAGES_KEY, sub_md):
            return None
        pages = {}
        bookmark = sub_md.get(BOOKMARK_KEY)
        try:
            index = 0
            for info in self.infolist():
                filename = self._get_info_fn(info)
                if self.IMAGE_EXT_RE.search(filename) is None:
                    continue
                size = self._get_info_size(info)
                # height & width could go here.
                if size is not None:
                    computed_page = {}
                    if index == bookmark:
                        computed_page[PAGE_BOOKMARK_KEY] = True
                    computed_page[PAGE_SIZE_KEY] = size
                    pages[index] = computed_page
                index += 1
            # Inside the guard with the scan that feeds it: merging consults
            # the archive too, and warn-and-skip is what every sibling action
            # does with a bad archive.
            if pages:
                pages = self._get_computed_merged_pages_metadata(sub_md, pages)
        except Exception as exc:
            logger.warning(f"{self._path}: Compute pages metadata: {exc}")
        return {PAGES_KEY: pages}