feat(config): Config loading and merging
This commit is contained in:
+28
-2
@@ -1,3 +1,4 @@
|
||||
from typing import cast
|
||||
from config.dtype import ArchMgrConfig
|
||||
|
||||
|
||||
@@ -5,5 +6,30 @@ def merge_configs(config: ArchMgrConfig, new_config: ArchMgrConfig) -> ArchMgrCo
|
||||
if len(new_config) == 0 or len(config) == 0:
|
||||
return config
|
||||
|
||||
# Merge configs
|
||||
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)))
|
||||
|
||||
Reference in New Issue
Block a user