Пример #1
0
    def _g_refNode(self, childNode, childName, validate=True):
        """
        Insert references to a `childNode` via a `childName`.

        Checks that the `childName` is valid and does not exist, then
        creates references to the given `childNode` by that `childName`.
        The validation of the name can be omitted by setting `validate`
        to a false value (this may be useful for adding already existing
        nodes to the tree).
        """

        # Check for name validity.
        if validate:
            checkNameValidity(childName)
            childNode._g_checkName(childName)

        # Check if there is already a child with the same name.
        # This can be triggered because of the user
        # (via node construction or renaming/movement).
        # Links are not checked here because they are copied and referenced
        # using ``File.getNode`` so they already exist in `self`.
        if (not isinstance(childNode, Link)) and childName in self:
            raise NodeError(
                "group ``%s`` already has a child node named ``%s``"
                % (self._v_pathname, childName))

        # Show a warning if there is an object attribute with that name.
        if childName in self.__dict__:
            warnings.warn(
                "group ``%s`` already has an attribute named ``%s``; "
                "you will not be able to use natural naming "
                "to access the child node"
                % (self._v_pathname, childName), NaturalNameWarning)

        # Check group width limits.
        if len(self._v_children) + len(self._v_hidden) >= self._v_maxGroupWidth:
            self._g_widthWarning()

        # Update members information.
        # Insert references to the new child.
        # (Assigned values are entirely irrelevant.)
        if isVisibleName(childName):
            # Visible node.
            self.__members__.insert(0, childName)  # enable completion
            self._v_children[childName] = None  # insert node
            if isinstance(childNode, Unknown):
                self._v_unknown[childName] = None
            elif isinstance(childNode, Link):
                self._v_links[childName] = None
            elif isinstance(childNode, Leaf):
                self._v_leaves[childName] = None
            elif isinstance(childNode, Group):
                self._v_groups[childName] = None
        else:
            # Hidden node.
            self._v_hidden[childName] = None  # insert node
Пример #2
0
    def _g_addChildrenNames(self):
        """
        Add children names to this group taking into account their
        visibility and kind.
        """

        myDict = self.__dict__

        # The names of the lazy attributes
        myDict['__members__'] = members = []
        """The names of visible children nodes for readline-style completion."""
        myDict['_v_children'] = children = _ChildrenDict(self)
        """The number of children hanging from this group."""
        myDict['_v_groups'] = groups = _ChildrenDict(self)
        """Dictionary with all groups hanging from this group."""
        myDict['_v_leaves'] = leaves = _ChildrenDict(self)
        """Dictionary with all leaves hanging from this group."""
        myDict['_v_links'] = links = _ChildrenDict(self)
        """Dictionary with all links hanging from this group."""
        myDict['_v_unknown'] = unknown = _ChildrenDict(self)
        """Dictionary with all unknown nodes hanging from this group."""
        myDict['_v_hidden'] = hidden = _ChildrenDict(self)
        """Dictionary with all hidden nodes hanging from this group."""

        # Get the names of *all* child groups and leaves.
        (groupNames, leafNames, linkNames, unknownNames) = \
                     self._g_listGroup(self._v_parent)

        # Separate groups into visible groups and hidden nodes,
        # and leaves into visible leaves and hidden nodes.
        for (childNames, childDict) in (
            (groupNames, groups),
            (leafNames, leaves),
            (linkNames, links),
            (unknownNames, unknown)):

            for childName in childNames:
                # See whether the name implies that the node is hidden.
                # (Assigned values are entirely irrelevant.)
                if isVisibleName(childName):
                    # Visible node.
                    members.insert(0, childName)
                    children[childName] = None
                    childDict[childName] = None
                else:
                    # Hidden node.
                    hidden[childName] = None