summaryrefslogtreecommitdiff
path: root/cloudinit/mergers/m_dict.py
blob: 274ccafc3b92237234c19299add096b555b8956a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (C) 2012 Yahoo! Inc.
#
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

DEF_MERGE_TYPE = "no_replace"
MERGE_TYPES = (
    "replace",
    DEF_MERGE_TYPE,
)


def _has_any(what, *keys):
    for k in keys:
        if k in what:
            return True
    return False


class Merger(object):
    def __init__(self, merger, opts):
        self._merger = merger
        # Affects merging behavior...
        self._method = DEF_MERGE_TYPE
        for m in MERGE_TYPES:
            if m in opts:
                self._method = m
                break
        # Affect how recursive merging is done on other primitives.
        self._recurse_str = "recurse_str" in opts
        self._recurse_array = _has_any(opts, "recurse_array", "recurse_list")
        self._allow_delete = "allow_delete" in opts
        # Backwards compat require this to be on.
        self._recurse_dict = True

    def __str__(self):
        s = (
            "DictMerger: (method=%s,recurse_str=%s,"
            "recurse_dict=%s,recurse_array=%s,allow_delete=%s)"
        )
        s = s % (
            self._method,
            self._recurse_str,
            self._recurse_dict,
            self._recurse_array,
            self._allow_delete,
        )
        return s

    def _do_dict_replace(self, value, merge_with, do_replace):
        def merge_same_key(old_v, new_v):
            if do_replace:
                return new_v
            if isinstance(new_v, (list, tuple)) and self._recurse_array:
                return self._merger.merge(old_v, new_v)
            if isinstance(new_v, str) and self._recurse_str:
                return self._merger.merge(old_v, new_v)
            if isinstance(new_v, (dict)) and self._recurse_dict:
                return self._merger.merge(old_v, new_v)
            # Otherwise leave it be...
            return old_v

        for (k, v) in merge_with.items():
            if k in value:
                if v is None and self._allow_delete:
                    value.pop(k)
                else:
                    value[k] = merge_same_key(value[k], v)
            else:
                value[k] = v
        return value

    def _on_dict(self, value, merge_with):
        if not isinstance(merge_with, (dict)):
            return value
        if self._method == "replace":
            merged = self._do_dict_replace(dict(value), merge_with, True)
        elif self._method == "no_replace":
            merged = self._do_dict_replace(dict(value), merge_with, False)
        else:
            raise NotImplementedError("Unknown merge type %s" % (self._method))
        return merged


# vi: ts=4 expandtab