Skip to content

graflo.cli.io

Loading and writing manifests, shared by the verbs that do both.

Extracted from graflo.cli.commit once check and merge needed the same two lines. The load is not merely FileHandle.load -- it runs finish_init(), which is where GraFlo validates cross-block references, so a verb that skips it operates on a manifest that has not been checked.

Attributes

DECLARED_KEYS = ('edge.directed', 'vertex.identity') module-attribute

__all__ = ['DECLARED_KEYS', 'dump_manifest', 'load_manifest', 'load_mapping', 'manifest_to_dict'] module-attribute

Classes

Functions:

dump_manifest(manifest, path, *, declare=())

Write manifest to path, announcing it. None writes nothing.

Source code in graflo/cli/io.py
def dump_manifest(
    manifest: GraphManifest, path: Path | None, *, declare: Collection[str] = ()
) -> None:
    """Write *manifest* to *path*, announcing it. ``None`` writes nothing."""
    if path is None:
        return
    Path(path).parent.mkdir(parents=True, exist_ok=True)
    FileHandle.dump(manifest_to_dict(manifest, declare=declare), path)
    click.echo(f"written: {path}")

load_manifest(path)

Read and fully initialise the manifest at path.

Source code in graflo/cli/io.py
def load_manifest(path: str | Path) -> GraphManifest:
    """Read and fully initialise the manifest at *path*."""
    manifest = GraphManifest.from_config(FileHandle.load(path))
    manifest.finish_init()
    return manifest

load_mapping(path)

Read a YAML/JSON document that must be a mapping.

Raises:

Type Description
ClickException

the document is a list or a scalar. Pydantic's own error for that names a model the user never wrote.

Source code in graflo/cli/io.py
def load_mapping(path: str | Path) -> dict[str, Any]:
    """Read a YAML/JSON document that must be a mapping.

    Raises:
        click.ClickException: the document is a list or a scalar. Pydantic's
            own error for that names a model the user never wrote.
    """
    data = FileHandle.load(path)
    if not isinstance(data, dict):
        raise click.ClickException(
            f"{path}: expected a mapping at the top level, got {type(data).__name__}"
        )
    return data

manifest_to_dict(manifest, *, declare=())

A compact dump of manifest, with declare keys stated explicitly.

Source code in graflo/cli/io.py
def manifest_to_dict(
    manifest: GraphManifest, *, declare: Collection[str] = ()
) -> dict[str, Any]:
    """A compact dump of *manifest*, with *declare* keys stated explicitly."""
    payload = manifest.to_dict(skip_defaults=True)
    if declare:
        _force_declared(payload, manifest, declare)
    return payload