From 24adecd9227931ebaec16115e05055e0b580d351 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 1 Jan 2023 19:16:38 +0000 Subject: Implement XAP style merge semantics for DD keycodes (#19397) --- lib/python/qmk/json_schema.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/json_schema.py') diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py index 934e2f841f..c886a0d868 100644 --- a/lib/python/qmk/json_schema.py +++ b/lib/python/qmk/json_schema.py @@ -1,12 +1,13 @@ """Functions that help us generate and use info.json files. """ import json +import hjson +import jsonschema from collections.abc import Mapping from functools import lru_cache +from typing import OrderedDict from pathlib import Path -import hjson -import jsonschema from milc import cli @@ -101,3 +102,34 @@ def deep_update(origdict, newdict): origdict[key] = value return origdict + + +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 -- cgit v1.2.3 From 9f2cd9119f18deb824ef7840c69f97c635b485cd Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Wed, 22 Feb 2023 22:50:09 +0000 Subject: Reallocate user/kb keycode ranges (#19907) --- lib/python/qmk/json_schema.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/python/qmk/json_schema.py') diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py index c886a0d868..b00df749cc 100644 --- a/lib/python/qmk/json_schema.py +++ b/lib/python/qmk/json_schema.py @@ -107,6 +107,7 @@ def deep_update(origdict, newdict): def merge_ordered_dicts(dicts): """Merges nested OrderedDict objects resulting from reading a hjson file. Later input dicts overrides earlier dicts for plain values. + If any value is "!delete!", the existing value will be removed from its parent. 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. """ @@ -125,6 +126,8 @@ def merge_ordered_dicts(dicts): target[k] = v[1:] else: target[k] = target[k] + v + elif v == "!delete!" and isinstance(target, (OrderedDict, dict)): + del target[k] else: target[k] = v -- cgit v1.2.3