vibe.nested_value¶
Nested Dictionary with Attribute Access.
Defines the NestedValue class, a dictionary subclass allowing nested attribute access (e.g., context.a.b.c) with optional per-instance access tracking. Includes helper functions for creation and manipulation. This is independent of Flask and used by both web UI state and the core engine.
NOTE: This module uses object for values because NestedValue is a transparent
wrapper around arbitrary JSON-compatible values (str, int, float, bool, list, dict, None).
SupportsGetItem ¶
Protocol for values supporting item access via getitem.
NestedValue ¶
A dictionary subclass supporting attribute access and primary values.
Supports optional access tracking via a per-instance tracker callback. Assumes non-JSON session serialization (e.g., pickle).
__init__ ¶
__init__(initial_data: dict[str, Any] | None = None, _value: object | None = None, _path_segment: str | None = None, _parent: NestedValue | None = None, _source: ValueSource = UNKNOWN, _definition: dict[str, Any] | None = None, _tracker: Callable[[str], None] | None = None) -> None
Initialize the NestedValue.
| Parameters: |
|
|---|
recursive_update ¶
recursive_update(source: Mapping[str, Any], valuesource: ValueSource | None = None) -> None
Recursively updates this NestedValue with values from 'source'.
If a key exists in both and both values are NestedValue instances (or the source value is a mapping type compatible with NestedValue), it recursively updates the nested target instance. Otherwise, it replaces/adds the value in this instance using standard setitem behavior (which handles dict->NestedValue conversion).
| Parameters: |
|
|---|
setitem_noconversion ¶
setitem_noconversion(key: str, value: object) -> None
Set an item directly without NestedValue conversion.
Use this for internal/context keys (like _alias, _host_template, meta, etc.) that should be stored as plain values, not wrapped in NestedValue nodes.
Note: These ARE stored in the dict (required for Jinja context access), but bypass NestedValue wrapping. They still need to be filtered out during iteration using _INTERNAL_KEYS.
delitem_noconversion ¶
delitem_noconversion(key: str) -> None
Delete an item set via setitem_noconversion.
__call__ ¶
__call__(*args: object, **kwargs: object) -> object
Invoke underlying callable value, raising TypeError if not callable.
iter_nested_nodes ¶
iter_nested_nodes(root: NestedValue, include_internal: bool = False) -> Iterator[tuple[str, NestedValue]]
Depth-first traversal of a NestedValue tree yielding (path, node) pairs.
| Parameters: |
|
|---|
| Yields: |
|
|---|
iter_leaf_nodes ¶
iter_leaf_nodes(root: NestedValue, include_internal: bool = False, include_placeholders: bool = True) -> Iterator[tuple[str, NestedValue]]
Iterate only the leaf NestedValue nodes (or probe placeholders) in depth-first order.
flatten_nested_leaves ¶
flatten_nested_leaves(root: NestedValue, include_internal: bool = False, include_placeholders: bool = True) -> dict[str, Any]
Return a flat mapping from dot-notated paths to stored values for the leaf nodes.
iter_nodes_by_source ¶
iter_nodes_by_source(root: NestedValue, sources: Iterable[ValueSource], include_internal: bool = False) -> Iterator[tuple[str, NestedValue]]
Yield nodes whose ValueSource is in the provided iterable.
remove_probe_placeholders ¶
remove_probe_placeholders(root: NestedValue) -> None
Remove keys whose values are probe placeholders from the NestedValue tree.
remove_internal_key ¶
remove_internal_key(root: NestedValue, key: str) -> None
Remove the specified key from all NestedValue nodes to keep metadata out of stored data.
has_actual_value ¶
has_actual_value(node: NestedValue | None, sources: Iterable[ValueSource] = _DEFAULT_ACTUAL_VALUE_SOURCES, allow_empty: bool = False) -> bool
Return True if the NestedValue node has a user/computed value (not a placeholder).
When allow_empty is True we treat empty strings or None as intentional answers,
as long as the node comes from a tracked source.
has_source ¶
has_source(node: NestedValue | None, sources: Iterable[ValueSource]) -> bool
Return True if the node exists and its _source is in the provided collection.
clone_nested_value ¶
clone_nested_value(node: NestedValue, parent: NestedValue | None = None, path_segment: str | None = None, tracker: Callable[[str], None] | None = None) -> NestedValue
Deeply clone a NestedValue tree, preserving metadata.
clear_nested_value_trackers ¶
clear_nested_value_trackers(node: NestedValue, _visited: set[int] | None = None) -> None
Recursively remove tracker references from a NestedValue tree.
remove_unpicklable_probe_artifacts ¶
remove_unpicklable_probe_artifacts(node: object, _visited: set[int] | None = None) -> None
Remove unpicklable objects from NestedValue tree after probing.
This includes TrackingUndefined instances and contaminated _definition dicts. Should be called on current_state after probing completes.
remove_unpicklable_rendering_artifacts ¶
remove_unpicklable_rendering_artifacts(node: NestedValue, _visited: set[int] | None = None) -> None
Remove unpicklable rendering artifacts from NestedValue tree after rendering.
These artifacts are added during DOCX/final document rendering and include: - _docxtpl: DocxTemplate instance with Jinja2 environment - _vfs: VFS instance - meta: MetadataNamespace instance - _host_template: TemplateData with Jinja environment - _current_state: Self-reference (circular) - causes JSON serialization errors - _is_probing: Boolean flag for probe mode - _is_web_preview: Boolean flag for web preview mode
Should be called on current_state after render_final_document completes.
| Parameters: |
|
|---|
create_nested_structure ¶
create_nested_structure(data_dict: dict[str, Any], default_source: ValueSource = UNKNOWN) -> NestedValue
Recursively converts a standard Python dict into a NestedValue structure.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
set_nested_value ¶
set_nested_value(root: NestedValue, path_str: str, value: object, source: ValueSource = UNKNOWN, definition: dict[Any, Any] | None = None) -> None
Set a value in a NestedValue structure using dot notation path.
Create intermediate NestedValue nodes if they don't exist. Set the _source and _definition attributes on the leaf node.
| Parameters: |
|
|---|
| Raises: |
|
|---|
get_nested_node ¶
get_nested_node(root: NestedValue, path: str, default: NestedValue | None = None) -> NestedValue | None
Get the NestedValue node instance from a structure using dot notation path.
| Parameters: |
|
|---|
| Returns: |
|
|---|
get_nested_value ¶
get_nested_value(root: NestedValue, path: str, default: object | None = None) -> object
Get a value from a NestedValue structure using dot notation path.
| Parameters: |
|
|---|
| Returns: |
|
|---|
nested_to_dict ¶
nested_to_dict(node: NestedValue | object) -> dict[str, Any] | object
Recursively converts a NestedValue structure back to a standard Python dict.
| Parameters: |
|
|---|
| Returns: |
|
|---|
unbox ¶
unbox(node: NestedValue) -> object
Access the primitive value stored in a NestedValue.
| Parameters: |
|
|---|
| Returns: |
|
|---|
pretty_print_nested_value ¶
pretty_print_nested_value(root: NestedValue, context: str | None = None) -> str
Generate a human-readable depth-first string representation of a NestedValue.
validate_nested_value_structure ¶
validate_nested_value_structure(root: NestedValue) -> bool
Validate that the NestedValue structure only stores primitives in _value.
Also verify that dictionary children are always NestedValue instances.