⚡️ in_place option for dict_merge

Thanks for the idea, @amrsoll
This commit is contained in:
Gina Häußge committed 2020-09-28 15:05:37 +02:00
1 parent b218cbab92
commit e92e69b35b
5 files changed
+20 -12

No files matched your search

+2 -2
View File
@@ -78,7 +78,7 @@ def full_extension_tree():
plugin_result = plugin.get_slicer_extension_tree()
if plugin_result is None or not isinstance(plugin_result, dict):
continue
result = octoprint.util.dict_merge(result, plugin_result, leaf_merger=leaf_merger)
octoprint.util.dict_merge(result, plugin_result, leaf_merger=leaf_merger, in_place=True)
except Exception:
logging.getLogger(__name__).exception("Exception while retrieving additional extension "
"tree entries from SlicerPlugin {name}".format(name=plugin._identifier),
@@ -90,7 +90,7 @@ def full_extension_tree():
hook_result = hook()
if hook_result is None or not isinstance(hook_result, dict):
continue
result = octoprint.util.dict_merge(result, hook_result, leaf_merger=leaf_merger)
result = octoprint.util.dict_merge(result, hook_result, leaf_merger=leaf_merger, in_place=True)
except Exception:
logging.getLogger(__name__).exception("Exception while retrieving additional extension "
"tree entries from hook {name}".format(name=name),
+1 -4
View File
@@ -909,11 +909,8 @@ class LocalFileStorage(StorageInterface):
metadata[name][key] = data
metadata_dirty = True
elif key in metadata[name] and isinstance(metadata[name][key], dict) and isinstance(data, dict) and merge:
current_data = metadata[name][key]
import octoprint.util
new_data = octoprint.util.dict_merge(current_data, data)
metadata[name][key] = new_data
metadata[name][key] = octoprint.util.dict_merge(metadata[name][key], data, in_place=True)
metadata_dirty = True
if metadata_dirty:
@@ -196,7 +196,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
if key in self._configured_checks:
yaml_config = self._configured_checks[key]
effective_config = dict_merge(effective_config, yaml_config)
effective_config = dict_merge(effective_config, yaml_config, in_place=True)
# Make sure there's nothing persisted in that check that shouldn't be persisted
#
+1 -1
View File
@@ -1140,6 +1140,6 @@ def _get_translations(locale, domain):
continue
po_messages, plural_expr = messages_from_po(po_file, locale, domain)
if po_messages is not None:
messages = dict_merge(messages, po_messages)
messages = dict_merge(messages, po_messages, in_place=True)
return messages, plural_expr
+15 -4
View File
@@ -669,11 +669,11 @@ def is_running_from_source():
return os.path.isdir(os.path.join(root, "src")) and os.path.isfile(os.path.join(root, "setup.py"))
def dict_merge(a, b, leaf_merger=None):
def dict_merge(a, b, leaf_merger=None, in_place=False):
"""
Recursively deep-merges two dictionaries.
Taken from https://www.xormedia.com/recursively-merge-dictionaries-in-python/
Based on https://www.xormedia.com/recursively-merge-dictionaries-in-python/
Example::
@@ -701,11 +701,17 @@ def dict_merge(a, b, leaf_merger=None):
True
>>> result.get("b") == "b"
True
>>> c = dict(foo="foo")
>>> dict_merge(c, {"bar": "bar"}) is c
False
>>> dict_merge(c, {"bar": "bar"}, in_place=True) is c
True
Arguments:
a (dict): The dictionary to merge ``b`` into
b (dict): The dictionary to merge into ``a``
leaf_merger (callable): An optional callable to use to merge leaves (non-dict values)
in_place (boolean): If set to True, a will be merged with b in place, meaning a will be modified
Returns:
dict: ``b`` deep-merged into ``a``
@@ -720,10 +726,15 @@ def dict_merge(a, b, leaf_merger=None):
if not isinstance(b, dict):
return b
result = deepcopy(a)
if in_place:
result = a
else:
result = deepcopy(a)
for k, v in b.items():
if k in result and isinstance(result[k], dict):
result[k] = dict_merge(result[k], v, leaf_merger=leaf_merger)
result[k] = dict_merge(result[k], v, leaf_merger=leaf_merger, in_place=in_place)
else:
merged = None
if k in result and callable(leaf_merger):