refactor(config): add set_config_value for non-merge replacement
This commit is contained in:
1 parent
fb05409403
commit
d124a7c896
3 files changed
+78
-19
No files matched your search
@@ -63,6 +63,36 @@ def update_config(patch: dict[str, Any]) -> dict[str, Any]:
|
||||
return deepcopy(merged)
|
||||
|
||||
|
||||
def set_config_value(path: list[str], value: Any) -> dict[str, Any]:
|
||||
"""Set a value at a nested path in config, REPLACING the existing value
|
||||
(unlike update_config which deep-merges). Useful when you need to wholesale
|
||||
replace a dict (e.g., to remove keys not present in the new value).
|
||||
|
||||
Example:
|
||||
set_config_value(["pose_3d", "named_poses"], {"a": {...}, "b": {...}})
|
||||
|
||||
Walks the path creating intermediate dicts if missing. Persists to disk.
|
||||
Returns the new full config (deep-copied)."""
|
||||
if not path:
|
||||
raise ValueError("path must be a non-empty list of keys")
|
||||
with _LOCK:
|
||||
if _STATE["config"] is None:
|
||||
load_config()
|
||||
cfg = _STATE["config"]
|
||||
node = cfg
|
||||
for key in path[:-1]:
|
||||
child = node.get(key)
|
||||
if not isinstance(child, dict):
|
||||
child = {}
|
||||
node[key] = child
|
||||
node = child
|
||||
node[path[-1]] = value
|
||||
if _STATE["path"]:
|
||||
with open(_STATE["path"], "w") as f:
|
||||
yaml.safe_dump(cfg, f, sort_keys=False)
|
||||
return deepcopy(cfg)
|
||||
|
||||
|
||||
def _deep_merge(base: dict[str, Any], patch: dict[str, Any]) -> dict[str, Any]:
|
||||
out = deepcopy(base)
|
||||
for k, v in patch.items():
|
||||
|
||||
Reference in new issue
Block a user