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: check_name_validity(childname) childnode._g_check_name(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.get_node`` 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_max_group_width): self._g_width_warning() # 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
def _g_add_children_names(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. (group_names, leaf_names, link_names, unknown_names) = \ self._g_list_group(self._v_parent) # Separate groups into visible groups and hidden nodes, # and leaves into visible leaves and hidden nodes. for (childnames, childdict) in ((group_names, groups), (leaf_names, leaves), (link_names, links), (unknown_names, 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