From a4b91bf5c69f74c2b5f322402fc1beab660050f4 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 12 Jul 2023 04:52:50 +0100 Subject: [PATCH] deep_merge implemented recursively merges dicts in profiles patch bump --- pyproject.toml | 2 +- vban_cmd/util.py | 14 ++++++++++++++ vban_cmd/vbancmd.py | 14 +++++++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 981b1cd..363e43b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "vban-cmd" -version = "2.3.0" +version = "2.3.1" description = "Python interface for the VBAN RT Packet Service (Sendtext)" authors = ["onyx-and-iris "] license = "MIT" diff --git a/vban_cmd/util.py b/vban_cmd/util.py index 9e6e739..924c49c 100644 --- a/vban_cmd/util.py +++ b/vban_cmd/util.py @@ -73,4 +73,18 @@ def comp(t0: tuple, t1: tuple) -> Iterator[bool]: yield True +def deep_merge(dict1, dict2): + """Generator function for deep merging two dicts""" + for k in set(dict1) | set(dict2): + if k in dict1 and k in dict2: + if isinstance(dict1[k], dict) and isinstance(dict2[k], dict): + yield k, dict(deep_merge(dict1[k], dict2[k])) + else: + yield k, dict2[k] + elif k in dict1: + yield k, dict1[k] + else: + yield k, dict2[k] + + Socket = IntEnum("Socket", "register request response", start=0) diff --git a/vban_cmd/vbancmd.py b/vban_cmd/vbancmd.py index 8f79b64..77e14c6 100644 --- a/vban_cmd/vbancmd.py +++ b/vban_cmd/vbancmd.py @@ -4,13 +4,13 @@ import time from abc import ABCMeta, abstractmethod from pathlib import Path from queue import Queue -from typing import Iterable, Optional, Union +from typing import Iterable, Union from .error import VBANCMDError from .event import Event from .packet import RequestHeader from .subject import Subject -from .util import Socket, script +from .util import Socket, deep_merge, script from .worker import Producer, Subscriber, Updater logger = logging.getLogger(__name__) @@ -194,14 +194,18 @@ class VbanCmd(metaclass=ABCMeta): f"Known configs: {list(self.configs.keys())}", ) try: - config = self.configs[name].copy() + config = self.configs[name] except KeyError as e: self.logger.error(("\n").join(ERR_MSG)) raise VBANCMDError(("\n").join(ERR_MSG)) from e if "extends" in config: - extended = config.pop("extends") - config = self.configs[extended] | config + extended = config["extends"] + config = { + k: v + for k, v in deep_merge(self.configs[extended], config) + if k not in ("extends") + } self.logger.debug( f"profile '{name}' extends '{extended}', profiles merged.." )