Source code for pyvcad_attribute_resolver.adapt
import pyvcad as pv
from .registry import _global_graph
from .solver import solve
[docs]
def adapt(design, target_attributes,
allowed_conversions=None, tags=None, max_depth=10):
"""Wrap a design node with AttributeModifiers to produce target attributes.
For each target attribute not already present on the design, the
solver finds a conversion path through the registered graph and
wraps the design in the corresponding ``AttributeModifier`` chain.
Targets are processed sequentially so that attributes produced by
earlier conversions are available for later ones.
Arguments:
design: ``pyvcad.Node``, the root of the design tree.
target_attributes: List of attribute name strings the caller
needs on the output.
allowed_conversions: Optional list of ``ConversionEntry`` names
to restrict the solver.
tags: Optional list of tags to filter conversions.
max_depth: Maximum chain length per target (default 10).
Returns:
``pyvcad.Node`` — the original design wrapped in zero or more
``AttributeModifier`` nodes. Returns the design unchanged if
all targets are already present.
Raises:
NoPathError: If any target attribute cannot be reached.
AmbiguousPathError: If disambiguation fails for any target.
"""
available = design.attribute_list()
result = design
for target in target_attributes:
if target in available:
continue
path = solve(_global_graph, available, target,
allowed_conversions=allowed_conversions,
tags=tags, max_depth=max_depth)
for entry in path:
converter = entry.converter_factory()
result = pv.AttributeModifier(converter, result)
available = result.attribute_list()
return result