Presets¶
Store reusable styles as presets, and resolve the values an object inherits from them.
Preset¶
-
class creator_studio_doc_format.core.Preset(identifier, properties, *, display_name=
None, parent_identifier=None)[source]¶ A preset, as one entry in the presets file stores it.
PresetRegistryresolves inheritance alongparent_identifier.- Parameters:
- identifier : str¶
The name that an adopting object’s
preset_identifierrefers to.- properties : dict[str, Any]¶
The properties that adopting objects inherit, as raw JSON.
- display_name : str | None¶
The name that Keynote shows.
- parent_identifier : str | None¶
The identifier of the preset this one inherits from.
PresetRegistry¶
- class creator_studio_doc_format.core.PresetRegistry(presets)[source]¶
A resolver of preset inheritance for decoding and encoding.
The registry walks the
parent_identifierchain and merges each child’s properties over its parent’s. A cycle raisesValueError. A missing preset or parent adds an entry towarningsand contributes no properties. A model takes the registry as itscontext["wire_presets"]resolver.- Parameters:
>>> from creator_studio_doc_format.core import ( ... CellProperties, ... Preset, ... PresetRegistry, ... ) >>> registry = PresetRegistry( ... [ ... Preset("base", {"text_wrap": True, "vertical_alignment": "top"}), ... Preset( ... "wrapped", ... {"vertical_alignment": "middle"}, ... parent_identifier="base", ... ), ... ] ... ) >>> registry.resolved_properties("wrapped") {'text_wrap': True, 'vertical_alignment': 'middle'}When the JSON omits a field, decoding takes its value from the preset. Encoding with the same registry leaves out a value that still matches the preset, so the value stays linked to it. A value that differs is written:
>>> context = {"wire_presets": registry} >>> cell = CellProperties.model_validate( ... {"preset_identifier": "wrapped"}, context=context ... ) >>> cell.text_wrap True >>> "text_wrap" in cell.model_dump(mode="json", context=context) False >>> cell.text_wrap = False >>> cell.model_dump(mode="json", context=context)["text_wrap"] False- resolved_properties(identifier)[source]¶
Returns the properties of a preset, merged over its ancestors’ properties.
Every call for an identifier returns the same dictionary, so don’t mutate it.
- Parameters:
- identifier : str¶
The identifier of the preset to resolve.
- Returns:
The merged properties, or an empty dictionary when the preset is missing.
- Return type:
dict[str, Any]- Raises:
ValueError– theparent_identifierchain has a cycle.
See also
Declare presets and Write nulls only where you mean them in the format’s authoring requirements, and Turn off an inherited value for what a null means.