Skip to content

cgtype

cgtype #

The root of every CaptureGraph type: the class is the type, instances are values.

A type owns two things: its wire form (schema) and its on-disk form (load/store under a base path). Everything above — hashing, caching, staging — works on the directory tree a value stores as, so a new type joins the whole system by implementing those alone.

CGType #

Base of every CaptureGraph type.

Source code in capturegraph-lib/capturegraph/types/cgtype.py
class CGType:
    """Base of every CaptureGraph type."""

    __slots__ = ()

    # False only for ``Path``/``Void``, which are addresses and side effects.
    storable: ClassVar[bool] = True

    # Usable in recipes but banned from procedure/target schemas (no platform mirror).
    analysis_only: ClassVar[bool] = False

    @classmethod
    def schema(cls) -> dict[str, JSONValue]:
        """This type's structural wire form."""
        raise NotImplementedError(f"{cls.__name__} has no wire form")

    @classmethod
    def load(cls, base: Path) -> Self:
        """The value stored at ``base``.

        Raises:
            FileNotFoundError: If nothing is stored there.
        """
        raise TypeError(f"{cls.__name__} has no on-disk form to load")

    @classmethod
    def store(cls, base: Path, value: object) -> None:
        """Write ``value`` at ``base``, touching only leaves whose bytes change.

        Raises:
            TypeError: If ``value`` cannot be stored as this type.
        """
        raise TypeError(f"{cls.__name__} has no on-disk form to store")

    @classmethod
    def _children(cls) -> list[tuple[str, type[CGType]]]:
        """The child types this one declares, each labelled by its position.

        A struct field is ``.name``, a container element ``[*]``, and a ``Path``
        target shares its address (``""``); a leaf declares none.
        """
        return []

    @classmethod
    def _descend(cls, component: str) -> type[CGType]:
        """The child type one on-disk ``component`` reaches from this type.

        Raises:
            SchemaPathError: If this type is a leaf, or ``component`` is not one
                of its declared entries.
        """
        raise SchemaPathError(f"cannot descend into {cls.__name__!r} at {component!r}")

load(base) classmethod #

The value stored at base.

Raises:

Type Description
FileNotFoundError

If nothing is stored there.

Source code in capturegraph-lib/capturegraph/types/cgtype.py
@classmethod
def load(cls, base: Path) -> Self:
    """The value stored at ``base``.

    Raises:
        FileNotFoundError: If nothing is stored there.
    """
    raise TypeError(f"{cls.__name__} has no on-disk form to load")

schema() classmethod #

This type's structural wire form.

Source code in capturegraph-lib/capturegraph/types/cgtype.py
@classmethod
def schema(cls) -> dict[str, JSONValue]:
    """This type's structural wire form."""
    raise NotImplementedError(f"{cls.__name__} has no wire form")

store(base, value) classmethod #

Write value at base, touching only leaves whose bytes change.

Raises:

Type Description
TypeError

If value cannot be stored as this type.

Source code in capturegraph-lib/capturegraph/types/cgtype.py
@classmethod
def store(cls, base: Path, value: object) -> None:
    """Write ``value`` at ``base``, touching only leaves whose bytes change.

    Raises:
        TypeError: If ``value`` cannot be stored as this type.
    """
    raise TypeError(f"{cls.__name__} has no on-disk form to store")

SchemaPathError #

Bases: ValueError

A path component is not a declared entry of the schema at its position.

Source code in capturegraph-lib/capturegraph/types/cgtype.py
class SchemaPathError(ValueError):
    """A path component is not a declared entry of the schema at its position."""