Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

NodeAttributes.to_dict now does a recursive merge #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion chef/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,27 @@ def set_dotted(self, key, value):
def to_dict(self):
merged = {}
for d in reversed(self.search_path):
merged.update(d)
self._dict_recursive_merge(merged, d)
return merged

def _dict_recursive_merge(self, a, b, path=None):
"""merges b into a, when a key already exists for an exact
path, it is overwritten
"""
if path is None:
path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
self._dict_recursive_merge(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass
else:
a[key] = b[key]
else:
a[key] = b[key]
return a


class Node(ChefObject):
"""A Chef node object.
Expand Down