Skip to content

comicbox.box.metadata

Get Metadata mixin.

ComicboxMetadata

Bases: ComicboxComputed

Get Metadata mixin.

Source code in comicbox/box/metadata.py
 17
 18
 19
 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
 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
class ComicboxMetadata(ComicboxComputed):
    """Get Metadata mixin."""

    def _set_computed_merged_metadata_delete(self, merged_md: dict[str, Any]) -> None:
        """Delete keys with glom."""
        sub_data = merged_md.get(ComicboxSchemaMixin.ROOT_TAG)
        # The computed pass earns delete keys of its own — date parts that
        # cannot form a real date. Deleting only the config's meant those
        # were flagged and then kept.
        for key_path in sorted(self._all_delete_keys()):
            try:
                delete = Delete(key_path, ignore_missing=True)
                glom(sub_data, delete)
            except Exception as exc:
                logger.warning(f"Could not delete key path {key_path}: {exc}")

    def _set_computed_merged_metadata(self) -> None:
        # The computed pass already merged each delta into its own snapshot,
        # in order, because every action has to see what the ones before it
        # produced. Replaying the same deltas onto a second copy of the
        # merged metadata here did that identical work twice.
        # Deep copy, not dict(): the delete below reaches into nested dicts,
        # and a shallow copy would mutate the ones the computed cache still
        # references.
        merged_md = deepcopy(dict(self.get_computed_merged_metadata()))
        self._set_computed_merged_metadata_delete(merged_md)
        self._metadata = MappingProxyType(merged_md)
        self._metadata_dict_formats = self._dict_formats

    def get_internal_metadata(self) -> MappingProxyType:
        """
        Return the internal metadata from the archive.

        Most external applications should use box.to_dict()
        """
        # Recompute when the dict-format context changed — without this,
        # the first to_dict() format's computed pages froze into the
        # cache for every later call under a different format. None is
        # the set_internal_metadata wildcard: pinned for every context.
        stale = (
            self._metadata_dict_formats is not None
            and self._metadata_dict_formats != self._dict_formats
        )
        if not self._metadata or stale:
            self._set_computed_merged_metadata()
        return self._metadata

    def set_internal_metadata(self, metadata: Mapping) -> None:
        """Programmatically set the raw metadata."""
        self._metadata = MappingProxyType(metadata)
        # Wildcard: pinned metadata is never recomputed for a different
        # dict-format context.
        self._metadata_dict_formats = None

    def _to_dict(
        self,
        fmt: MetadataFormats = MetadataFormats.COMICBOX_YAML,
    ) -> tuple:
        # Get schema instance.
        schema_class = fmt.value.schema_class
        schema = get_schema(schema_class, path=self._path)

        # Get transformed md
        transform = self._get_transform(fmt.value.transform_class)

        try:
            # dict_format is used to determine whether or not to compute some values
            # currently only pages & page_count
            self._dict_formats = frozenset({fmt})
            md = self.get_internal_metadata()
            md = transform.from_comicbox(md)
        finally:
            self._dict_formats = frozenset()

        return schema, MappingProxyType(md)

    def to_dict(
        self,
        fmt: MetadataFormats = MetadataFormats.COMICBOX_YAML,
        **kwargs: Any,
    ) -> dict:
        """Get merged metadata as a dict."""
        schema, md = self._to_dict(fmt)
        dump = schema.dump(md, **kwargs)
        return dict(dump)

    def to_string(
        self,
        fmt: MetadataFormats = MetadataFormats.COMICBOX_YAML,
        **kwargs: Any,
    ) -> str:
        """Get mergeesized metadata as a string."""
        schema, md = self._to_dict(fmt)
        return schema.dumps(md, **kwargs)

get_internal_metadata()

Return the internal metadata from the archive.

Most external applications should use box.to_dict()

Source code in comicbox/box/metadata.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def get_internal_metadata(self) -> MappingProxyType:
    """
    Return the internal metadata from the archive.

    Most external applications should use box.to_dict()
    """
    # Recompute when the dict-format context changed — without this,
    # the first to_dict() format's computed pages froze into the
    # cache for every later call under a different format. None is
    # the set_internal_metadata wildcard: pinned for every context.
    stale = (
        self._metadata_dict_formats is not None
        and self._metadata_dict_formats != self._dict_formats
    )
    if not self._metadata or stale:
        self._set_computed_merged_metadata()
    return self._metadata

set_internal_metadata(metadata)

Programmatically set the raw metadata.

Source code in comicbox/box/metadata.py
64
65
66
67
68
69
def set_internal_metadata(self, metadata: Mapping) -> None:
    """Programmatically set the raw metadata."""
    self._metadata = MappingProxyType(metadata)
    # Wildcard: pinned metadata is never recomputed for a different
    # dict-format context.
    self._metadata_dict_formats = None

to_dict(fmt=MetadataFormats.COMICBOX_YAML, **kwargs)

Get merged metadata as a dict.

Source code in comicbox/box/metadata.py
 93
 94
 95
 96
 97
 98
 99
100
101
def to_dict(
    self,
    fmt: MetadataFormats = MetadataFormats.COMICBOX_YAML,
    **kwargs: Any,
) -> dict:
    """Get merged metadata as a dict."""
    schema, md = self._to_dict(fmt)
    dump = schema.dump(md, **kwargs)
    return dict(dump)

to_string(fmt=MetadataFormats.COMICBOX_YAML, **kwargs)

Get mergeesized metadata as a string.

Source code in comicbox/box/metadata.py
103
104
105
106
107
108
109
110
def to_string(
    self,
    fmt: MetadataFormats = MetadataFormats.COMICBOX_YAML,
    **kwargs: Any,
) -> str:
    """Get mergeesized metadata as a string."""
    schema, md = self._to_dict(fmt)
    return schema.dumps(md, **kwargs)