3
# Copyright (C) 2012 Yahoo! Inc.
5
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 3, as
9
# published by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
DEF_MERGE_TYPE = 'no_replace'
20
MERGE_TYPES = ('replace', DEF_MERGE_TYPE,)
23
def _has_any(what, *keys):
31
def __init__(self, merger, opts):
33
# Affects merging behavior...
34
self._method = DEF_MERGE_TYPE
39
# Affect how recursive merging is done on other primitives.
40
self._recurse_str = 'recurse_str' in opts
41
self._recurse_array = _has_any(opts, 'recurse_array', 'recurse_list')
42
self._allow_delete = 'allow_delete' in opts
43
# Backwards compat require this to be on.
44
self._recurse_dict = True
47
s = ('DictMerger: (method=%s,recurse_str=%s,'
48
'recurse_dict=%s,recurse_array=%s,allow_delete=%s)')
49
s = s % (self._method, self._recurse_str,
50
self._recurse_dict, self._recurse_array, self._allow_delete)
53
def _do_dict_replace(self, value, merge_with, do_replace):
55
def merge_same_key(old_v, new_v):
58
if isinstance(new_v, (list, tuple)) and self._recurse_array:
59
return self._merger.merge(old_v, new_v)
60
if isinstance(new_v, (basestring)) and self._recurse_str:
61
return self._merger.merge(old_v, new_v)
62
if isinstance(new_v, (dict)) and self._recurse_dict:
63
return self._merger.merge(old_v, new_v)
64
# Otherwise leave it be...
67
for (k, v) in merge_with.items():
69
if v is None and self._allow_delete:
72
value[k] = merge_same_key(value[k], v)
77
def _on_dict(self, value, merge_with):
78
if not isinstance(merge_with, (dict)):
80
if self._method == 'replace':
81
merged = self._do_dict_replace(dict(value), merge_with, True)
82
elif self._method == 'no_replace':
83
merged = self._do_dict_replace(dict(value), merge_with, False)
85
raise NotImplementedError("Unknown merge type %s" % (self._method))