Exemplo n.º 1
0
def get_node_path(node):
    """Only to be used for inferred nodes who do not belong to a layer.
    :param node: Node object
    :return: string of node path
    """
    return nxt_path.join_node_paths(getattr(node, INTERNAL_ATTRS.PARENT_PATH),
                                    getattr(node, INTERNAL_ATTRS.NAME))
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 create_spec_node(node_data, layer, parent_path=nxt_path.WORLD,
                     is_proxy=False):
    attrs = {}
    node_name = node_data[INTERNAL_ATTRS.as_save_key(INTERNAL_ATTRS.NAME)]
    path = nxt_path.join_node_paths(parent_path, node_name)
    # Parse for attrs
    user_attrs = node_data.get(nxt_io.SAVE_KEY.ATTRS, {})
    for attr_name, data in user_attrs.items():
        attrs[attr_name] = None
        attrs[attr_name + META_ATTRS.SOURCE] = (layer.real_path, path)
        for sub_attr, value in data.items():
            if sub_attr == META_ATTRS.VALUE:
                attrs[attr_name] = value
                continue
            meta_attr = META_ATTRS._prefix + sub_attr + META_ATTRS._suffix
            if meta_attr not in META_ATTRS.ALL:
                logger.warning('Invalid meta attr "{}"'.format(meta_attr))
                logger.warning('Expected {}'.format(META_ATTRS.ALL))
                continue
            full_attr_name = (attr_name + meta_attr)
            attrs[full_attr_name] = value
    for attr in INTERNAL_ATTRS.ALL:
        key = INTERNAL_ATTRS.as_save_key(attr)
        val = node_data.get(key)
        if (not has_opinion(val) and attr not in
                INTERNAL_ATTRS.ALLOW_NO_OPINION):
            val = INTERNAL_ATTRS.DEFAULTS.get(attr)
            if callable(val):
                val = val()
        attrs[attr] = val
    # Remove duplicates in child order
    child_order = []
    for c in node_data.get('child_order', []):
        if c not in child_order:
            child_order += [c]
    attrs.update({INTERNAL_ATTRS.NAME: node_name,
                  INTERNAL_ATTRS.PROXY: is_proxy,
                  INTERNAL_ATTRS.CHILD_ORDER: child_order,
                  INTERNAL_ATTRS.SOURCE_LAYER: layer.real_path
                  })
    pp_key = INTERNAL_ATTRS.as_save_key(INTERNAL_ATTRS.PARENT_PATH)
    pp_attr = INTERNAL_ATTRS.PARENT_PATH
    if is_proxy:
        attrs[pp_attr] = node_data.get(pp_key) or parent_path
    else:
        attrs[pp_attr] = parent_path

    node_spec = SpecNode.new(attrs=attrs)
    parent_object = layer.lookup(parent_path)
    layer.clear_node_child_cache(path)
    layer.clear_node_child_cache(parent_path)
    if parent_object:
        proxies = (getattr(node_spec, INTERNAL_ATTRS.PROXY) or
                    getattr(parent_object, INTERNAL_ATTRS.PROXY))
        child_order = getattr(parent_object, INTERNAL_ATTRS.CHILD_ORDER)
        if node_name not in child_order and not proxies:
            new_co = list_merger(child_order, [node_name])
            setattr(parent_object, INTERNAL_ATTRS.CHILD_ORDER, new_co)
    for attr in INTERNAL_ATTRS.TRACKED:
        setattr(node_spec, attr + META_ATTRS.SOURCE,
                (layer.real_path, path))
    return node_spec