From 025d76fb4b1641c821cd071144f12d4904634278 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Thu, 18 Jun 2020 18:54:18 +0200 Subject: vyos: configdict: add dict_merge function Merge two dictionaries. Only keys which are not present in destination will be copied from source, anything else will be kept untouched. Function will return a new dict which has the merged key/value pairs. Before: {'device': {'usb0b2.4p1.0': {'speed': '9600'}, 'usb0b2.4p1.1': {'data-bits': '8', 'parity': 'none', 'speed': '115200', 'stop-bits': '2'}}} After: {'device': {'usb0b2.4p1.0': {'data-bits': '8', 'parity': 'none', 'speed': '9600', 'stop-bits': '1'}, 'usb0b2.4p1.1': {'data-bits': '8', 'parity': 'none', 'speed': '115200', 'stop-bits': '2'}}} --- python/vyos/configdict.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'python/vyos/configdict.py') diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py index 4708d3b50..973fbdd8b 100644 --- a/python/vyos/configdict.py +++ b/python/vyos/configdict.py @@ -87,6 +87,19 @@ def retrieve_config(path_hash, base_path, config): return config_hash +def dict_merge(source, destination): + """ Merge two dictionaries. Only keys which are not present in destination + will be copied from source, anything else will be kept untouched. Function + will return a new dict which has the merged key/value pairs. """ + from copy import deepcopy + tmp = deepcopy(destination) + + for key, value in source.items(): + if key not in tmp.keys(): + tmp[key] = value + + return tmp + def list_diff(first, second): """ Diff two dictionaries and return only unique items -- cgit v1.2.3