Skip to content

comicbox.formats.base.fields.comicbox

Comicbox Fields.

PagesField

Bases: DictField

ComicInfo Pages.

Source code in comicbox/formats/base/fields/comicbox.py
127
128
129
130
131
132
133
134
135
136
137
138
class PagesField(DictField):  # CIX ONLY, CT
    """ComicInfo Pages."""

    def __init__(self, *args: Any, keys_as_string: bool = False, **kwargs: Any) -> None:
        """ComicInfo Pages with keys_as_string option."""
        super().__init__(
            *args,
            keys=IntegerField(minimum=0, as_string=keys_as_string),
            values=Nested(PageInfoSchema),
            case_insensitive=False,
            **kwargs,
        )

__init__(*args, keys_as_string=False, **kwargs)

ComicInfo Pages with keys_as_string option.

Source code in comicbox/formats/base/fields/comicbox.py
130
131
132
133
134
135
136
137
138
def __init__(self, *args: Any, keys_as_string: bool = False, **kwargs: Any) -> None:
    """ComicInfo Pages with keys_as_string option."""
    super().__init__(
        *args,
        keys=IntegerField(minimum=0, as_string=keys_as_string),
        values=Nested(PageInfoSchema),
        case_insensitive=False,
        **kwargs,
    )

RoleField

Bases: PrettifiedStringField

Prettified Role Field.

Source code in comicbox/formats/base/fields/comicbox.py
121
122
123
124
class RoleField(PrettifiedStringField):
    """Prettified Role Field."""

    ENUM_ALIAS_MAP = COMICBOX_ROLE_ALIAS_MAP

SimpleNamedDictField

Bases: Union

A dict that also accepts a simple string set and builds a dict from that.

Source code in comicbox/formats/base/fields/comicbox.py
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
class SimpleNamedDictField(Union):
    """A dict that also accepts a simple string set and builds a dict from that."""

    def __init__(
        self,
        *args: Any,
        keys: Field | type[Field] = StringField,
        values: Field | type[Field] | None = None,
        allow_empty_values: bool = True,
        sort: bool = True,
        **kwargs: Any,
    ) -> None:
        """Create the union."""
        if values is None:
            values = Nested(IdentifiedSchema)
        fields = [
            DictField(
                keys=keys,
                values=values,
                allow_empty_values=allow_empty_values,
                sort=sort,
            ),
            StringSetField(sort=sort),
        ]
        super().__init__(fields, *args, **kwargs)  # pyright: ignore[reportArgumentType], # ty: ignore[invalid-argument-type]

    @override
    def _deserialize(
        self,
        value: dict[str, Any],
        *args: Any,
        **kwargs: Any,
    ) -> dict:
        result: dict = super()._deserialize(value, *args, **kwargs)
        if isinstance(result, set | frozenset):
            dict_value = {}
            for key in result:
                dict_value[key] = {}
            result = dict(super()._deserialize(dict_value, *args, **kwargs))
        return result

__init__(*args, keys=StringField, values=None, allow_empty_values=True, sort=True, **kwargs)

Create the union.

Source code in comicbox/formats/base/fields/comicbox.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def __init__(
    self,
    *args: Any,
    keys: Field | type[Field] = StringField,
    values: Field | type[Field] | None = None,
    allow_empty_values: bool = True,
    sort: bool = True,
    **kwargs: Any,
) -> None:
    """Create the union."""
    if values is None:
        values = Nested(IdentifiedSchema)
    fields = [
        DictField(
            keys=keys,
            values=values,
            allow_empty_values=allow_empty_values,
            sort=sort,
        ),
        StringSetField(sort=sort),
    ]
    super().__init__(fields, *args, **kwargs)  # pyright: ignore[reportArgumentType], # ty: ignore[invalid-argument-type]

SimpleNamedNestedField

Bases: Union

A nested schema that also reads a plain name or number.

series: Crimson Tide is read as series: {name: Crimson Tide}, and volume: 2006 as volume: {number: 2006}.

Source code in comicbox/formats/base/fields/comicbox.py
 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
class SimpleNamedNestedField(Union):
    """
    A nested schema that also reads a plain name or number.

    ``series: Crimson Tide`` is read as ``series: {name: Crimson Tide}``, and
    ``volume: 2006`` as ``volume: {number: 2006}``.
    """

    def __init__(
        self,
        *args: Any,
        schema: type[BaseSubSchema] = IdentifiedNameSchema,
        field: Field | type[Field] = StringField,
        name_key: str = NAME_KEY,
        **kwargs: Any,
    ) -> None:
        """Create the union."""
        self._name_key = name_key
        if isclass(field):
            field = field()
        self._plain_field: Field = field
        fields = [Nested(schema(ignore_errors=UNION_SCHEMA_IGNORE_ERRORS)), field]
        super().__init__(fields, *args, **kwargs)

    @override
    def _deserialize(
        self,
        value: Any,
        attr: str | None = None,
        data: Mapping[str, Any] | None = None,
        **kwargs: Any,
    ) -> dict:
        if not isinstance(value, Mapping):
            # The nested schema reads a value that is not a mapping as empty,
            # without an error, so the union never reached the plain field
            # and a plain name was dropped unlogged. Read it here: an invalid
            # one raises, and its schema logs the drop.
            plain = self._plain_field.deserialize(value, attr, data, **kwargs)
            value = {self._name_key: plain}
        return super()._deserialize(value, attr, data, **kwargs)

__init__(*args, schema=IdentifiedNameSchema, field=StringField, name_key=NAME_KEY, **kwargs)

Create the union.

Source code in comicbox/formats/base/fields/comicbox.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def __init__(
    self,
    *args: Any,
    schema: type[BaseSubSchema] = IdentifiedNameSchema,
    field: Field | type[Field] = StringField,
    name_key: str = NAME_KEY,
    **kwargs: Any,
) -> None:
    """Create the union."""
    self._name_key = name_key
    if isclass(field):
        field = field()
    self._plain_field: Field = field
    fields = [Nested(schema(ignore_errors=UNION_SCHEMA_IGNORE_ERRORS)), field]
    super().__init__(fields, *args, **kwargs)