diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-06-18 18:54:18 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2020-06-18 18:55:37 +0200 |
commit | 025d76fb4b1641c821cd071144f12d4904634278 (patch) | |
tree | 2c70c392365b013be95d27bb880d514994360ece | |
parent | 6072d3b7dbd9040a55cddf6ee597ae461796f312 (diff) | |
download | vyos-1x-025d76fb4b1641c821cd071144f12d4904634278.tar.gz vyos-1x-025d76fb4b1641c821cd071144f12d4904634278.zip |
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'}}}
-rw-r--r-- | python/vyos/configdict.py | 13 |
1 files changed, 13 insertions, 0 deletions
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 |