Skip to content

define.param.param_definition

ParamDefinition

A tree structure that represents how the parameters to a command look. This represents the definition of a command's paramaters, and not a particular execution of that comamnd with particular values

Source code in /home/runner/work/arc/arc/arc/define/param/param_definition.py
class ParamDefinition:
    """A tree structure that represents how the parameters to a command look.
    This represents the definition of a command's paramaters, and not a particular
    execution of that comamnd with particular values"""

    def __init__(
        self,
        name: str,
        cls: type | None = None,
        *args: t.Any,
        **kwargs: t.Any,
    ) -> None:
        super().__init__(*args, **kwargs)
        self.name: str = name
        self.cls: type | None = cls
        self.params = collections.deque[Param[t.Any]]()
        self.children: list[ParamDefinition] = []

    __repr__ = api.display("name", "cls", "params", "children")

    def all_params(self) -> t.Generator[Param[t.Any], None, None]:
        """Generator that yields all params in the tree"""
        yield from self.params

        if self.children:
            for child in self.children:
                yield from child.all_params()

    def create_instance(self) -> ParamInstanceTree[type[dict[str, t.Any]]]:
        root = self.__create_tree(self)
        return ParamInstanceTree(root)

    def __create_tree(
        self, definition: ParamDefinition
    ) -> ParamInstanceInteriorNode[t.Any]:

        # Create Param instances for all the params of the current group
        values: list[ParamInstanceInteriorNode[t.Any] | ParamInstanceLeafNode] = [
            ParamInstanceLeafNode(param.argument_name, MISSING, param)
            for param in definition.params
        ]

        # Recursively create Param instances for all the children of the current group
        # (so this would be any sub-groups of the current group)
        values.extend(self.__create_tree(child) for child in definition.children)

        return ParamInstanceInteriorNode(
            definition.name,
            definition.cls or dict,
            values,
        )

all_params()

Generator that yields all params in the tree

Source code in /home/runner/work/arc/arc/arc/define/param/param_definition.py
def all_params(self) -> t.Generator[Param[t.Any], None, None]:
    """Generator that yields all params in the tree"""
    yield from self.params

    if self.children:
        for child in self.children:
            yield from child.all_params()