Exemplo n.º 1
0
 def load_from_layer_data(cls, layer_data):
     new_layer = cls()
     result_nodes = {}
     name_attr = nxt_node.INTERNAL_ATTRS.NAME
     name_key = nxt_node.INTERNAL_ATTRS.as_save_key(name_attr)
     for node_path, node_data in layer_data.get('nodes', {}).items():
         parent_path = nxt_path.get_parent_path(node_path)
         node_name = nxt_path.node_name_from_node_path(node_path)
         node_data[name_key] = node_name
         spec_node = nxt_node.create_spec_node(node_data, new_layer,
                                               parent_path)
         code_lines = getattr(spec_node, nxt_node.INTERNAL_ATTRS.COMPUTE)
         code = '\n'.join(code_lines)
         setattr(spec_node, nxt_node.INTERNAL_ATTRS.CACHED_CODE, code)
         result_nodes[node_path] = spec_node
     new_layer.nodes = result_nodes
     return new_layer
Exemplo n.º 2
0
 def _construct_node_specs(self, layer_data):
     self.spec_list = []
     self._nodes_path_as_key = {}
     self._nodes_node_as_key = {}
     nodes = order_nodes_dict(layer_data.get(SAVE_KEY.NODES, {}))
     for node_path, node_data in nodes.items():
         parent_path = nxt_path.get_parent_path(node_path)
         root_name = nxt_path.node_name_from_node_path(node_path)
         node_data['name'] = root_name
         node_data[nxt_node.INTERNAL_ATTRS.SOURCE_LAYER] = self.real_path
         root_spec_node = nxt_node.create_spec_node(node_data, self,
                                                    parent_path=parent_path)
         root_parent_path = getattr(root_spec_node,
                                    nxt_node.INTERNAL_ATTRS.PARENT_PATH)
         root_node_path = nxt_path.join_node_paths(root_parent_path,
                                                   root_name)
         self.spec_list += [root_spec_node]
         self._nodes_path_as_key[root_node_path] = root_spec_node
         self._nodes_node_as_key[root_spec_node] = root_node_path
         self.clear_node_child_cache(root_node_path)
Exemplo n.º 3
0
    def children(self,
                 node_path=nxt_path.WORLD,
                 return_type=LayerReturnTypes.Node,
                 ordered=False,
                 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.')
        children_nodes = []
        children_paths = []
        node_table = []
        name_dict = {}
        if not node_path:
            if return_type in (LayerReturnTypes.Node, LayerReturnTypes.Path,
                               LayerReturnTypes.NodeTable):
                return []
            elif return_type == LayerReturnTypes.NameDict:
                return {}
            elif return_type == LayerReturnTypes.Boolean:
                return False
            else:
                logger.error('Invalid return type provided')
                return None
        child_order = []
        implied_children = []
        # Look up real children cache
        children_cache = self._cached_children.get(node_path)
        if node_path == nxt_path.WORLD:
            children_cache = None
        if children_cache is not None:
            children_nodes = children_cache[LayerReturnTypes.Node][:]
            children_paths = children_cache[LayerReturnTypes.Path][:]
            node_table = children_cache[LayerReturnTypes.NodeTable][:]
            name_dict = copy.copy(children_cache[LayerReturnTypes.NameDict])
            cache_real = False
        else:
            self._cached_children[node_path] = {
                LayerReturnTypes.Node: children_nodes,
                LayerReturnTypes.Path: children_paths,
                LayerReturnTypes.NodeTable: node_table,
                LayerReturnTypes.NameDict: name_dict
            }
            cache_real = True
        # Lookup implied cache
        cache_implied = False
        if include_implied:
            implied_c_cache = self._cached_implied_children.get(node_path)
            if node_path == nxt_path.WORLD:
                implied_c_cache = None
            if implied_c_cache is not None:
                implied_children = implied_c_cache[LayerReturnTypes.Path][:]
                if return_type == LayerReturnTypes.Boolean and not cache_real:
                    return bool(implied_children + children_paths)
            else:
                path_implied = {LayerReturnTypes.Path: implied_children}
                self._cached_implied_children[node_path] = path_implied
                cache_implied = True
        re_cache = cache_implied or cache_real
        if re_cache:
            for path, node in self._nodes_path_as_key.items():
                if cache_real:
                    parent_path = getattr(node,
                                          nxt_node.INTERNAL_ATTRS.PARENT_PATH)
                    if parent_path == node_path:
                        children_nodes += [node]
                        children_paths += [path]
                        node_table += [[path, node]]
                        key = getattr(node, nxt_node.INTERNAL_ATTRS.NAME)
                        name_dict[key] = node
                if not include_implied or not cache_implied:
                    continue
                if nxt_path.is_ancestor(path, node_path):
                    trim_depth = nxt_path.get_path_depth(node_path) + 1
                    trimmed = nxt_path.trim_to_depth(path, trim_depth)
                    if trimmed not in implied_children:
                        implied_children += [trimmed]
        if cache_real:
            k = LayerReturnTypes.Path
            self._cached_children[node_path][k] = children_paths[:]
        if include_implied:
            for imp in implied_children:
                if imp not in children_paths:
                    children_paths += [imp]
        if return_type == LayerReturnTypes.Boolean:
            return bool(children_paths)
        if ordered:
            node = self.lookup(node_path)
            if include_implied and not node:
                child_order = []
            else:
                co_attr = nxt_node.INTERNAL_ATTRS.CHILD_ORDER
                child_order = getattr(node, co_attr)
        if child_order:
            ordered_child_nodes = []
            ordered_child_paths = []
            ordered_node_table = []
            for child_name in child_order:
                # return type NODE
                for n in children_nodes:
                    c_name = getattr(n, nxt_node.INTERNAL_ATTRS.NAME)
                    if c_name == child_name:
                        ordered_child_nodes += [n]
                # return type PATH
                for p in children_paths:
                    if nxt_path.node_name_from_node_path(p) == child_name:
                        ordered_child_paths += [p]
                # return type TABLE
                for item in node_table:
                    p, n = item
                    if nxt_path.node_name_from_node_path(p) == child_name:
                        ordered_node_table += [item]
            # return type NODE
            for n in children_nodes:
                if n not in ordered_child_nodes:
                    ordered_child_nodes += [n]
            children_nodes = ordered_child_nodes
            # return type PATH
            for p in children_paths:
                if p not in ordered_child_paths:
                    ordered_child_paths += [p]
            children_paths = ordered_child_paths
            # return type TABLE
            for item in node_table:
                p, n = item
                if p not in ordered_node_table:
                    ordered_node_table += [item]
            node_table = ordered_node_table

        if return_type == LayerReturnTypes.Node:
            return children_nodes
        elif return_type == LayerReturnTypes.Path:
            return children_paths
        elif return_type == LayerReturnTypes.NodeTable:
            return node_table
        elif return_type == LayerReturnTypes.NameDict:
            return name_dict
        elif return_type == LayerReturnTypes.Boolean:
            return False
        else:
            logger.error('Invalid return type provided')
            return None