def _world_descendants(self, return_type, include_implied=False): if include_implied and return_type != LayerReturnTypes.Path: raise ValueError('When including implied, can only return path. ' 'Nothing else exists for implicit nodes.') if return_type == LayerReturnTypes.Path: paths = list(self._nodes_path_as_key.keys()) if nxt_path.WORLD in paths: paths.remove(nxt_path.WORLD) if not include_implied: return paths implied_paths = set() for real_path in paths: ancest_paths = nxt_path.all_ancestor_paths(real_path) implied_paths = implied_paths.union(ancest_paths) if nxt_path.WORLD in implied_paths: implied_paths.remove(nxt_path.WORLD) return implied_paths.union(paths) if return_type == LayerReturnTypes.Node: nodes = list(self._nodes_node_as_key.keys()) world_node = self._nodes_path_as_key.get(nxt_path.WORLD) if world_node: nodes.remove(world_node) return nodes if return_type == LayerReturnTypes.NodeTable: node_table = [] for path, node in self._nodes_path_as_key.items(): if path is nxt_path.WORLD: continue node_table += [[path, node]] return node_table raise TypeError('Unsupported return type {}'.format(return_type))
def ancestors(self, node_path, return_type=LayerReturnTypes.Node, include_implied=False): if include_implied and return_type != LayerReturnTypes.Path: raise TypeError('When including implied nodes, {} is an ' 'unsupported return type'.format(return_type)) if include_implied: return nxt_path.all_ancestor_paths(node_path) ancestor_nodes = [] ancestor_paths = [] node_table = [] name_dict = {} if sys.version_info[0] == 2: node_path_is_str = isinstance(node_path, basestring) else: node_path_is_str = isinstance(node_path, str) if not node_path_is_str: node_path = self.get_node_path(node_path) node = self.lookup(node_path) parent_path = getattr(node, nxt_node.INTERNAL_ATTRS.PARENT_PATH) if node: while parent_path != nxt_path.WORLD: _node = self.lookup(parent_path) if _node is None: parent_path = nxt_path.get_parent_path(parent_path) continue if return_type == LayerReturnTypes.Boolean: return True elif return_type == LayerReturnTypes.Node: ancestor_nodes += [_node] elif return_type == LayerReturnTypes.Path: ancestor_paths += [parent_path] elif return_type == LayerReturnTypes.NodeTable: node_table += [[parent_path, _node]] elif return_type == LayerReturnTypes.NameDict: key = getattr(node, nxt_node.INTERNAL_ATTRS.NAME) name_dict[key] = _node node = _node ppath_attr = nxt_node.INTERNAL_ATTRS.PARENT_PATH parent_path = getattr(node, ppath_attr) if return_type == LayerReturnTypes.Node: return ancestor_nodes elif return_type == LayerReturnTypes.Path: return ancestor_paths elif return_type == LayerReturnTypes.NodeTable: return node_table elif return_type == LayerReturnTypes.NameDict: return name_dict elif return_type == LayerReturnTypes.Boolean: return False