[tools] Fix merge_dicts for d2 immutability

This commit is contained in:
Kuba Szczodrzyński
2022-06-24 12:18:36 +02:00
parent c8fccdbb47
commit b371fd3468
3 changed files with 23 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
# Copyright (c) Kuba Szczodrzyński 2022-04-20.
import json
import sys
from os.path import dirname, join
from typing import Dict
@@ -13,6 +14,11 @@ from platformio.package.manager.base import BasePackageManager
from platformio.package.meta import PackageItem, PackageSpec
from platformio.platform.board import PlatformBoardConfig
# Make tools available
sys.path.insert(0, dirname(__file__))
from tools.util.platform import get_board_manifest
libretuya_packages = None
manifest_default = {"version": "0.0.0", "description": "", "keywords": []}
@@ -174,25 +180,7 @@ class LibretuyaPlatform(PlatformBase):
def update_board(self, board: PlatformBoardConfig):
if "_base" in board:
base = board.get("_base")
if not isinstance(base, list):
base = [base]
result = None
for base_name in base:
if base_name not in self.boards_base:
file = join(
dirname(__file__), "boards", "_base", f"{base_name}.json"
)
with open(file, encoding="utf-8") as f:
self.boards_base[base_name] = json.load(f)
if not result:
result = self.boards_base[base_name]
else:
util.merge_dicts(result, self.boards_base[base_name])
util.merge_dicts(result, board._manifest)
board._manifest = result
board._manifest = get_board_manifest(board._manifest)
# add "arduino" framework
has_arduino = any("arduino" in fw for fw in board.manifest["frameworks"])

View File

@@ -6,14 +6,20 @@ from typing import Tuple, Union
SliceLike = Union[slice, str, int]
def merge_dicts(d1, d2, path=None):
if path is None:
path = []
for key in d2:
if key in d1 and isinstance(d1[key], dict) and isinstance(d2[key], dict):
merge_dicts(d1[key], d2[key], path + [str(key)])
else:
d1[key] = d2[key]
def merge_dicts(d1, d2):
if d1 is not None and type(d1) != type(d2):
raise TypeError("d1 and d2 are different types")
if isinstance(d2, list):
if d1 is None:
d1 = []
d1.extend(merge_dicts(None, item) for item in d2)
elif isinstance(d2, dict):
if d1 is None:
d1 = {}
for key in d2:
d1[key] = merge_dicts(d1.get(key, None), d2[key])
else:
d1 = d2
return d1

View File

@@ -26,15 +26,12 @@ def get_board_manifest(board: Union[str, dict]) -> dict:
base = board["_base"]
if not isinstance(base, list):
base = [base]
result = None
result = {}
for base_name in base:
if base_name not in boards_base:
file = join(boards_dir, "_base", f"{base_name}.json")
boards_base[base_name] = load_json(file)
if not result:
result = boards_base[base_name]
else:
merge_dicts(result, boards_base[base_name])
merge_dicts(result, boards_base[base_name])
merge_dicts(result, board)
board = result
return board