Skip to content

comicbox.formats.base.transforms.spec ¶

Transform maps.

A MetaSpec maps destination keypaths to source keypaths. _create_specs compiles a set of them into one glom spec in two phases, and refuses to compile a set whose declarations contradict each other.

Destination keyspace: a compile owns its destinations. Two metaspecs claiming the same keypath, or one claiming a keypath nested inside another’s, raise at import. glom’s assign used to resolve those silently – the later mapping won the value at the earlier one’s slot, and a nested pair either lost the whole subtree or was set as an attribute of the enclosing spec and never reached the output.

Scope channel: a metaspec with assign_global publishes its spec function’s result into glom’s per call globals, and other metaspecs read it back by naming S.globals.comicbox.<key> among their sources. glom evaluates a compiled dict in key insertion order, so phase one compiles the producers and phase two everything else. Declaration order no longer decides whether a read resolves. Only one producer per compile: a second publish replaces the whole scope mapping. Readers still supply their own default, because publishing happens at runtime over data that may hold nothing; the compile only proves a producer exists.

MetaSpec dataclass ¶

Define a key mapping and transform functions.

Source code in comicbox/formats/base/transforms/spec.py
40
41
42
43
44
45
46
47
48
@dataclass
class MetaSpec:
    """Define a key mapping and transform functions."""

    key_map: Mapping[str, str | tuple[str, ...]]
    spec: Callable | tuple | None = None
    inherit_root_keypath: bool = True
    # Publish this mapping's result on the scope channel. See the module docstring.
    assign_global: bool = False

create_specs_from_comicbox(*metaspecs, format_root_keypath='', comicbox_root_keypath=ROOT_KEYPATH) ¶

Create from comicbox specs.

Source code in comicbox/formats/base/transforms/spec.py
347
348
349
350
351
352
353
354
355
356
357
358
359
def create_specs_from_comicbox(
    *metaspecs: MetaSpec,
    format_root_keypath: str = "",
    comicbox_root_keypath: str = ROOT_KEYPATH,
) -> MappingProxyType[
    str, dict[str, dict[str, Coalesce]] | dict[str, Coalesce] | Coalesce
]:
    """Create from comicbox specs."""
    return _create_specs(
        *metaspecs,
        dest_root_keypath=format_root_keypath,
        source_root_keypath=comicbox_root_keypath,
    )

create_specs_to_comicbox(*metaspecs, format_root_keypath='', comicbox_root_keypath=ROOT_KEYPATH) ¶

Create to comicbox specs.

Source code in comicbox/formats/base/transforms/spec.py
334
335
336
337
338
339
340
341
342
343
344
def create_specs_to_comicbox(
    *metaspecs: MetaSpec,
    format_root_keypath: str = "",
    comicbox_root_keypath: str = ROOT_KEYPATH,
) -> MappingProxyType[str, dict[str, Coalesce] | Coalesce]:
    """Create to comicbox specs."""
    return _create_specs(
        *metaspecs,
        dest_root_keypath=comicbox_root_keypath,
        source_root_keypath=format_root_keypath,
    )