mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-10-10 17:19:51 +00:00
Fix serious bug in merge_dicts where the original dict would be modified
This commit is contained in:
parent
70c282e804
commit
8bb34c604b
1 changed files with 17 additions and 5 deletions
|
@ -29,9 +29,21 @@ def merge_manifest_data(standard_partials=[], custom=[]):
|
||||||
|
|
||||||
# Snatched from here: http://stackoverflow.com/a/7205107
|
# Snatched from here: http://stackoverflow.com/a/7205107
|
||||||
def merge_dicts(*args):
|
def merge_dicts(*args):
|
||||||
def merge(a, b, path=None):
|
def clone(obj):
|
||||||
if path is None:
|
copy = obj
|
||||||
path = []
|
if isinstance(obj, dict):
|
||||||
|
copy = {}
|
||||||
|
for key, value in obj.iteritems():
|
||||||
|
copy[key] = clone(value)
|
||||||
|
if isinstance(obj, list):
|
||||||
|
copy = []
|
||||||
|
copy.extend(obj)
|
||||||
|
if isinstance(obj, set):
|
||||||
|
copy = set()
|
||||||
|
copy.update(obj)
|
||||||
|
return copy
|
||||||
|
|
||||||
|
def merge(a, b, path=[]):
|
||||||
for key in b:
|
for key in b:
|
||||||
if key in a:
|
if key in a:
|
||||||
if isinstance(a[key], dict) and isinstance(b[key], dict):
|
if isinstance(a[key], dict) and isinstance(b[key], dict):
|
||||||
|
@ -39,8 +51,8 @@ def merge_dicts(*args):
|
||||||
elif a[key] == b[key]:
|
elif a[key] == b[key]:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
|
raise Exception('Conflict at `{path}\''.format(path='.'.join(path + [str(key)])))
|
||||||
else:
|
else:
|
||||||
a[key] = b[key]
|
a[key] = clone(b[key])
|
||||||
return a
|
return a
|
||||||
return reduce(merge, args, {})
|
return reduce(merge, args, {})
|
||||||
|
|
Loading…
Add table
Reference in a new issue