36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
from typing import cast
|
|
from config.dtype import ArchMgrConfig
|
|
|
|
|
|
def merge_configs(config: ArchMgrConfig, new_config: ArchMgrConfig) -> ArchMgrConfig:
|
|
if len(new_config) == 0 or len(config) == 0:
|
|
return config
|
|
|
|
def combine(a: dict, b: dict):
|
|
combined = {}
|
|
for key in b:
|
|
val = b[key]
|
|
try:
|
|
a[key]
|
|
if isinstance(val, dict):
|
|
combined[key] = combine(val, a[key])
|
|
elif isinstance(val, list):
|
|
combined[key] = val
|
|
for v in a[key]:
|
|
combined[key].append(v)
|
|
else:
|
|
combined[key] = val
|
|
except KeyError:
|
|
combined[key] = val
|
|
|
|
for key in a:
|
|
try:
|
|
b[key]
|
|
except KeyError:
|
|
combined[key] = a[key]
|
|
|
|
return combined
|
|
|
|
# Merge configs (using nasty casts)
|
|
return cast(ArchMgrConfig, combine(cast(dict, config), cast(dict, new_config)))
|