geomotif.io.spec
¶
A design's recipe rather than its points: the motif and its parameters.
:func:~geomotif.io.points.save_points writes what a design is; this module
writes what would produce it. The file is a few hundred bytes instead of a few
hundred kilobytes, it survives a change of point count or spacing curve, and it
is what the gallery manifest and the CLI's --spec flag are built on.
The on-disk shape is the same nested object at every depth::
{
"geomotif": "1.1.0",
"motif": "polygon.star",
"params": {"points": 7, "step": 3, "center": [0.0, 0.0]}
}
A parameter that is itself a motif -- the composers in :mod:geomotif.compose
take one -- is written as that same {"motif": ..., "params": ...} object, so
a spec nests without needing a second notation. A parameter that is a value
dataclass (:class:~geomotif.Bounds, Ring, IFSMap) becomes a
{"$type": ...} object naming its class.
Between them those two cases cover every builtin motif except the two whose parameter is a Python function: a motif defined by code cannot be rebuilt from data, and asking for its spec says so rather than writing a file that will not load.
An optional top-level :data:ANIMATION_KEY ("animation") carries the
recipe for a moving picture -- the same JSON the CLI's --animation flag
reads and the web explorer encodes into a share URL. A spec without it is a
still, and :func:from_spec ignores the key when building the motif, so an
old spec keeps loading unchanged.
Functions:
| Name | Description |
|---|---|
to_spec |
Return the JSON-ready recipe for a motif, or for the design it built. |
from_spec |
Rebuild the motif a spec describes. |
save_spec |
Write a motif's recipe to a JSON file and return the path written. |
load_spec |
Read a spec file and return the motif it describes. |
to_spec
¶
to_spec(source: SupportsBuild | Design, *, animation: Mapping[str, object] | None = None) -> dict[str, object]
Return the JSON-ready recipe for a motif, or for the design it built.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Motif or Design
|
A motif, or any design whose :attr: |
required |
animation
|
mapping
|
An animation recipe to carry alongside the still, so a moving picture
round-trips through the same file the CLI's |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If a parameter cannot be written as data -- see the module docstring. |
Examples:
Source code in src/geomotif/io/spec.py
from_spec
¶
from_spec(data: Mapping[str, object]) -> Motif
Rebuild the motif a spec describes.
The version stamp is not consulted; a spec is data, and data that loaded once should keep loading.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the mapping names no motif, or names a value type this library will not import. |
KeyError
|
If the motif name is not registered -- including when it belongs to a plugin that is not installed. |
Source code in src/geomotif/io/spec.py
save_spec
¶
save_spec(source: SupportsBuild | Design, path: str | PathLike[str], *, indent: int | None = 2) -> Path
Write a motif's recipe to a JSON file and return the path written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Motif or Design
|
What to describe; see :func: |
required |
path
|
str or path - like
|
Destination file. |
required |
indent
|
int
|
Passed through to :func: |
2
|
Returns:
| Type | Description |
|---|---|
Path
|
The file that was written. |
Source code in src/geomotif/io/spec.py
load_spec
¶
load_spec(path: str | PathLike[str]) -> Motif
Read a spec file and return the motif it describes.
Returns:
| Type | Description |
|---|---|
Motif
|
Ready to :meth: |