Ejemplo n.º 1
0
    def _merge_simple_lists(self, lhs: CommentedSeq, rhs: CommentedSeq,
                            path: YAMLPath,
                            node_coord: NodeCoords) -> CommentedSeq:
        """
        Merge two CommentedSeq-wrapped lists of Scalars or CommentedSeqs.

        Parameters:
        1. lhs (CommentedSeq) The merge target.
        2. rhs (CommentedSeq) The merge source.
        3. path (YAMLPath) Location within the DOM where this merge is taking
           place.
        4. node_coord (NodeCoords) The RHS root node, its parent, and reference
           within its parent; used for config lookups.

        Returns: (list) The merged result.

        Raises:
        - `MergeException` when a clean merge is impossible.
        """
        if not isinstance(lhs, CommentedSeq):
            raise MergeException(
                "Impossible to add Array data to non-Array destination.", path)

        merge_mode = self.config.array_merge_mode(node_coord)
        if merge_mode is ArrayMergeOpts.LEFT:
            return lhs
        if merge_mode is ArrayMergeOpts.RIGHT:
            return rhs

        tagless_lhs = Nodes.tagless_elements(lhs)
        for idx, ele in enumerate(rhs):
            path_next = path + "[{}]".format(idx)
            self.logger.debug("Processing element {} at {}.".format(
                idx, path_next),
                              prefix="Merger::_merge_simple_lists:  ",
                              data=ele)

            if merge_mode is ArrayMergeOpts.UNIQUE:
                cmp_val = ele
                if isinstance(ele, TaggedScalar):
                    cmp_val = ele.value

                self.logger.debug(
                    "Looking for comparison value, {}, in:".format(cmp_val),
                    prefix="Merger::_merge_simple_lists:  ",
                    data=tagless_lhs)

                if cmp_val in tagless_lhs:
                    lhs = CommentedSeq([
                        ele if
                        (e == cmp_val or
                         (isinstance(e, TaggedScalar) and e.value == cmp_val))
                        else e for e in lhs
                    ])
                else:
                    lhs.append(ele)
                continue
            lhs.append(ele)
        return lhs
Ejemplo n.º 2
0
    def _merge_sets(
        self, lhs: CommentedSet, rhs: CommentedSet, path: YAMLPath,
        node_coord: NodeCoords
    ) -> CommentedSet:
        """
        Merge two YAML sets (CommentedSet-wrapped sets).

        Parameters:
        1. lhs (CommentedSet) The merge target.
        2. rhs (CommentedSet) The merge source.
        3. path (YAMLPath) Location within the DOM where this merge is taking
           place.
        4. node_coord (NodeCoords) The RHS root node, its parent, and reference
           within its parent; used for config lookups.

        Returns:  (CommentedSet) The merged result.

        Raises:
        - `MergeException` when a clean merge is impossible.
        """
        merge_mode = self.config.set_merge_mode(node_coord)
        if merge_mode is SetMergeOpts.LEFT:
            return lhs
        if merge_mode is SetMergeOpts.RIGHT:
            return rhs

        tagless_lhs = Nodes.tagless_elements(list(lhs))
        for ele in rhs:
            path_next = (path +
                YAMLPath.escape_path_section(ele, path.seperator))
            self.logger.debug(
                "Processing set element {} at {}.".format(ele, path_next),
                prefix="Merger::_merge_sets:  ", data=ele)

            cmp_val = ele
            if isinstance(ele, TaggedScalar):
                cmp_val = ele.value

            self.logger.debug(
                "Looking for comparison value, {}, in:".format(cmp_val),
                prefix="Merger::_merge_sets:  ", data=tagless_lhs)

            if cmp_val in tagless_lhs:
                continue
            lhs.add(ele)
        return lhs