Skip to content

varset_datasource

freecad.datamanager_wb.varsets.varset_datasource

VarSet-backed TabDataSource for the VarSets tab.

Adapts varset document-model operations to the generic TabController.

VarsetDataSource(*, ctx=None)

Adapter that exposes VarSets through the TabDataSource protocol.

This class maps generic tab operations (parents, children, expression queries, remove-unused) onto the VarSet-specific document model.

Source code in freecad/datamanager_wb/varsets/varset_datasource.py
def __init__(self, *, ctx: FreeCadContext | None = None) -> None:
    self._ctx = ctx

get_child_refs(selected_parents)

Return variable refs for the selected VarSets.

Source code in freecad/datamanager_wb/varsets/varset_datasource.py
def get_child_refs(self, selected_parents: list[str]) -> list[ParentChildRef]:
    """Return variable refs for the selected VarSets."""
    variable_items: list[str] = []
    for parent in selected_parents:
        varset_name, var_names = self._get_var_names_for_parent(parent)

        for var_name in var_names:
            variable_items.append(f"{varset_name}.{var_name}")

    variable_items.sort()
    refs: list[ParentChildRef] = []
    for text in variable_items:
        parsed = parse_varset_variable_item(text)
        if parsed is None:
            continue
        parent, child = parsed
        refs.append(ParentChildRef(parent=parent, child=child))
    return refs

get_expression_items(selected_children)

Return expression items referencing the selected variables.

Source code in freecad/datamanager_wb/varsets/varset_datasource.py
def get_expression_items(
    self, selected_children: list[ParentChildRef] | list[str]
) -> tuple[list[ExpressionItem], dict[str, int]]:
    """Return expression items referencing the selected variables."""
    expression_items: list[ExpressionItem] = []
    counts: dict[str, int] = {}

    for text in normalize_parent_child_items(selected_children):
        parsed = parse_varset_variable_item(text)
        if parsed is None:
            continue
        varset_name, variable_name = parsed
        refs = getVarsetReferences(varset_name, variable_name, ctx=self._ctx)
        counts[text] = len(refs)
        for k, v in refs.items():
            object_name = k.split(".", 1)[0].strip()
            expression_items.append(ExpressionItem(object_name=object_name, lhs=k, rhs=v))

    expression_items.sort(key=lambda item: item.display_text)
    return expression_items, counts

get_expression_reference_counts(selected_children)

Return expression reference counts for the selected variables.

Source code in freecad/datamanager_wb/varsets/varset_datasource.py
def get_expression_reference_counts(
    self, selected_children: list[ParentChildRef] | list[str]
) -> dict[str, int]:
    """Return expression reference counts for the selected variables."""
    counts: dict[str, int] = {}

    for text in normalize_parent_child_items(selected_children):
        parsed = parse_varset_variable_item(text)
        if parsed is None:
            continue
        varset_name, variable_name = parsed
        refs = getVarsetReferences(varset_name, variable_name, ctx=self._ctx)
        counts[text] = len(refs)

    return counts

get_sorted_parents(*, exclude_copy_on_change=False)

Return sorted VarSet names.

If a VarSet contains variables in more than one group, also include virtual VarSet entries of the form "{varset}.{group}".

Source code in freecad/datamanager_wb/varsets/varset_datasource.py
def get_sorted_parents(self, *, exclude_copy_on_change: bool = False) -> list[str]:
    """Return sorted VarSet names.

    If a VarSet contains variables in more than one group, also include
    virtual VarSet entries of the form "{varset}.{group}".
    """

    parents: list[str] = []
    for varset_name in sorted(
        getVarsets(exclude_copy_on_change=exclude_copy_on_change, ctx=self._ctx)
    ):
        parents.append(varset_name)

        groups = set(getVarsetVariableGroups(varset_name, ctx=self._ctx).values())
        if len(groups) <= 1:
            continue
        for group in sorted(groups):
            parents.append(f"{varset_name}.{group}")
    return parents

remove_unused_children(selected_children)

Remove variables that have no expression references.

Source code in freecad/datamanager_wb/varsets/varset_datasource.py
def remove_unused_children(
    self, selected_children: list[ParentChildRef] | list[str]
) -> RemoveUnusedResult:
    """Remove variables that have no expression references."""
    removed: list[str] = []
    still_used: list[str] = []
    failed: list[str] = []

    for text in normalize_parent_child_items(selected_children):
        parsed = parse_varset_variable_item(text)
        if parsed is None:
            failed.append(text)
            continue
        varset_name, variable_name = parsed
        refs = getVarsetReferences(varset_name, variable_name, ctx=self._ctx)
        if refs:
            still_used.append(text)
            continue
        ok = removeVarsetVariable(varset_name, variable_name, ctx=self._ctx)
        if ok:
            removed.append(text)
        else:
            failed.append(text)

    return RemoveUnusedResult(removed=removed, still_used=still_used, failed=failed)