deep_merge implemented

recursively merges dicts in profiles

patch bump
This commit is contained in:
onyx-and-iris 2023-07-12 04:52:42 +01:00
parent b0acde6a52
commit 278566c2e0
3 changed files with 25 additions and 5 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "voicemeeter-api" name = "voicemeeter-api"
version = "2.3.0" version = "2.3.1"
description = "A Python wrapper for the Voiceemeter API" description = "A Python wrapper for the Voiceemeter API"
authors = ["onyx-and-iris <code@onyxandiris.online>"] authors = ["onyx-and-iris <code@onyxandiris.online>"]
license = "MIT" license = "MIT"

View File

@ -1,3 +1,4 @@
import copy
import ctypes as ct import ctypes as ct
import logging import logging
import time import time
@ -13,7 +14,7 @@ from .kinds import KindId
from .misc import Midi, VmGui from .misc import Midi, VmGui
from .subject import Subject from .subject import Subject
from .updater import Producer, Updater from .updater import Producer, Updater
from .util import grouper, polling, script from .util import deep_merge, grouper, polling, script
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -306,14 +307,18 @@ class Remote(CBindings):
f"Known configs: {list(self.configs.keys())}", f"Known configs: {list(self.configs.keys())}",
) )
try: try:
config = self.configs[name].copy() config = self.configs[name]
except KeyError as e: except KeyError as e:
self.logger.error(("\n").join(ERR_MSG)) self.logger.error(("\n").join(ERR_MSG))
raise VMError(("\n").join(ERR_MSG)) from e raise VMError(("\n").join(ERR_MSG)) from e
if "extends" in config: if "extends" in config:
extended = config.pop("extends") extended = config["extends"]
config = self.configs[extended] | config config = {
k: v
for k, v in deep_merge(self.configs[extended], config)
if k not in ("extends")
}
self.logger.debug( self.logger.debug(
f"profile '{name}' extends '{extended}', profiles merged.." f"profile '{name}' extends '{extended}', profiles merged.."
) )

View File

@ -1,3 +1,4 @@
import copy
import functools import functools
from itertools import zip_longest from itertools import zip_longest
from typing import Iterator from typing import Iterator
@ -70,3 +71,17 @@ def grouper(n, iterable, fillvalue=None):
""" """
args = [iter(iterable)] * n args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args) return zip_longest(fillvalue=fillvalue, *args)
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]