cottax.consumers.json.format module

A graph as JSON: write_json and read_json.

loaded(name: str)[source]

What module:qualname names, among the modules already imported: a file imports nothing unless a resolve handed in does.

Return type:

object

by_word[A: Spelling | PlaceCodec | LabelCodec](interface: type[A], word: str)[source]

Each registered interface answer by its word: what a file names it by.

Return type:

dict[str, A]

read_json(data: dict, resolve: Callable[[str], object] = loaded)[source]

The graph data spells, every rule asked of it: places and node names by the codecs registered under the file’s tags, each node by the spelling registered for its kind; a name resolved by resolve.

Return type:

NestingGraph

check_codec[N: Label, V: Place](graph: NestingGraph[N, V], implementations: bool)[source]

One registered codec writes every node name, and one every place. A graph with no node has no place either, so it is refused once.

Return type:

Iterator[Violation]

check_nodes_write[N: Label, V: Place](graph: NestingGraph[N, V], implementations: bool)[source]

Each node writes: the check is the writing, so the two cannot disagree.

Return type:

Iterator[Violation]

write_json[N: Label, V: Place](graph: NestingGraph[N, V], implementations: bool = True)[source]

A graph written as JSON, ready for json.dump: each binding its name and its definition, places by the registered codecs. With implementations=False, the structure alone. Each node is written twice: once by the check, once here.

Return type:

dict

class NodeWriter[N: Label, V: Place](place_codec: PlaceCodec[V], label_codec: LabelCodec[N], implementations: bool, name: N)[source]

Bases: Frozen, Generic[N, V]

A Writer: the node bound at name, through the codecs.

place_codec: PlaceCodec[V]
label_codec: LabelCodec[N]
implementations: bool
name: N
places(places: tuple[V, ...])[source]
Return type:

list

place(place: V)[source]

Through the codec; what it refuses is refused, anything else in a spelling raises.

Return type:

Json

label(name: N)[source]
Return type:

Json

refuse(what: str, reason: str)[source]
Return type:

_Refused

node(node: NodeDefinition[V])[source]

node by its final’s spelling; without implementations, a wrapped one as its node.

Return type:

dict

implementation(obj: object)[source]

By name, or a dataclass as a value.

Return type:

dict

value(obj: object)[source]

A dataclass as its class and fields, each a JSON scalar or such a value again.

Return type:

dict

class NodeReader[N: Label, V: Place](place_codec: PlaceCodec[V], label_codec: LabelCodec[N], resolve: Callable[[str], object], name: N)[source]

Bases: Frozen, Generic[N, V]

A Reader: the node bound at name, through the place codec.

place_codec: PlaceCodec[V]
label_codec: LabelCodec[N]
resolve: Callable[[str], object]
name: N
place(data: Json)[source]
Return type:

V

label(data: Json)[source]
Return type:

N

places(data: list)[source]
Return type:

tuple[V, ...]

node(data: dict)[source]
Return type:

NodeDefinition[V]

implementation(data: dict)[source]
Return type:

object

value(data: dict)[source]
Return type:

Any

reference(obj: object)[source]

module:qualname if that finds obj again, else None: a lambda, a local, anything in __main__ or bound under another name.

Return type:

str | None

class FunctionSpelling[source]

Bases: Spelling[Function]

kind: AbstractClassVar[str] = 'function'

How it is referred to in the file.

write[V: Place](node: Function[V], w: Writer[Any, V])[source]

The node’s fields; recursive through wrapped nodes (Driven/Implemented)

Return type:

dict

read[V: Place](data: dict, r: Reader[Any, V])[source]

The node data spells: recursive through wrapped nodes (Driven/Implemented)

Return type:

Function[V]

class DrivenSpelling[source]

Bases: Spelling[Driven]

The condition, the driver as a value, and each slot with its role’s name.

kind: AbstractClassVar[str] = 'driven'

How it is referred to in the file.

write[V: Place](node: Driven[V], w: Writer[Any, V])[source]

The node’s fields; recursive through wrapped nodes (Driven/Implemented)

Return type:

dict

read[V: Place](data: dict, r: Reader[Any, V])[source]

The node data spells: recursive through wrapped nodes (Driven/Implemented)

Return type:

Driven[V]

class ImplementedSpelling[source]

Bases: Spelling[Implemented]

The node and what runs it; the final is the node’s level’s (implemented_as).

kind: AbstractClassVar[str] = 'implemented'

How it is referred to in the file.

write[V: Place](node: Implemented[V], w: Writer[Any, V])[source]

The node’s fields; recursive through wrapped nodes (Driven/Implemented)

Return type:

dict

read[V: Place](data: dict, r: Reader[Any, V])[source]

The node data spells: recursive through wrapped nodes (Driven/Implemented)

Return type:

Implemented[V]