From 35ff477d771ae981d1f5e3c370453ae96130ba00 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 1 Jan 2023 19:20:04 +0000 Subject: [PATCH] dedupe merge_ordered_dicts --- lib/python/qmk/xap/common.py | 41 ++++-------------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) diff --git a/lib/python/qmk/xap/common.py b/lib/python/qmk/xap/common.py index fcbb4be4300..c7176fe69b4 100755 --- a/lib/python/qmk/xap/common.py +++ b/lib/python/qmk/xap/common.py @@ -10,7 +10,7 @@ from jinja2 import Environment, FileSystemLoader, select_autoescape import qmk.constants from qmk.git import git_get_version -from qmk.json_schema import json_load, validate +from qmk.json_schema import json_load, validate, merge_ordered_dicts from qmk.decorators import lru_cache from qmk.keymap import locate_keymap from qmk.path import keyboard @@ -94,39 +94,6 @@ def _find_km_spec(kb, km): return locate_keymap(kb, km).parent / XAP_SPEC -def _merge_ordered_dicts(dicts): - """Merges nested OrderedDict objects resulting from reading a hjson file. - - Later input dicts overrides earlier dicts for plain values. - Arrays will be appended. If the first entry of an array is "!reset!", the contents of the array will be cleared and replaced with RHS. - Dictionaries will be recursively merged. If any entry is "!reset!", the contents of the dictionary will be cleared and replaced with RHS. - """ - - result = OrderedDict() - - def add_entry(target, k, v): - if k in target and isinstance(v, (OrderedDict, dict)): - if "!reset!" in v: - target[k] = v - else: - target[k] = _merge_ordered_dicts([target[k], v]) - if "!reset!" in target[k]: - del target[k]["!reset!"] - elif k in target and isinstance(v, list): - if v[0] == '!reset!': - target[k] = v[1:] - else: - target[k] = target[k] + v - else: - target[k] = v - - for d in dicts: - for (k, v) in d.items(): - add_entry(result, k, v) - - return result - - def get_xap_definition_files(): """Get the sorted list of XAP definition files, from /data/xap. """ @@ -144,7 +111,7 @@ def update_xap_definitions(original, new): """ if original is None: original = OrderedDict() - return _merge_ordered_dicts([original, new]) + return merge_ordered_dicts([original, new]) @lru_cache(timeout=5) @@ -159,7 +126,7 @@ def get_xap_defs(version): files = files[:(index + 1)] definitions = [hjson.load(file.open(encoding='utf-8')) for file in files] - return _merge_ordered_dicts(definitions) + return merge_ordered_dicts(definitions) def latest_xap_defs(): @@ -181,7 +148,7 @@ def merge_xap_defs(kb, km): if km_xap.exists(): definitions.append({'routes': {'0x03': hjson.load(km_xap.open(encoding='utf-8'))}}) - defs = _merge_ordered_dicts(definitions) + defs = merge_ordered_dicts(definitions) try: validate(defs, 'qmk.xap.v1')