Merge pull request 'refactor(persist/utils): rename apply_patch to 'set_value_by_path'' (#3780) from rename-1 into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3780
This commit is contained in:
hsjobeki
2025-05-28 08:19:10 +00:00
8 changed files with 22 additions and 22 deletions

View File

@@ -4,7 +4,7 @@ from clan_lib.api import API
from clan_lib.flake import Flake
from clan_lib.nix_models.inventory import Inventory, Meta
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch
from clan_lib.persist.util import set_value_by_path
@dataclass
@@ -17,7 +17,7 @@ class UpdateOptions:
def update_clan_meta(options: UpdateOptions) -> Inventory:
inventory_store = InventoryStore(options.flake)
inventory = inventory_store.read()
apply_patch(inventory, "meta", options.meta)
set_value_by_path(inventory, "meta", options.meta)
inventory_store.write(inventory, message="Update clan metadata")
return inventory

View File

@@ -16,7 +16,7 @@ from clan_lib.nix_models.inventory import (
MachineDeploy,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch
from clan_lib.persist.util import set_value_by_path
from clan_lib.templates import (
InputPrio,
TemplateName,
@@ -130,7 +130,7 @@ def create_machine(
)
raise ClanError(msg, description=description)
apply_patch(
set_value_by_path(
inventory,
f"machines.{machine_name}",
new_machine,

View File

@@ -13,7 +13,7 @@ from clan_lib.nix_models.inventory import (
MachineDeploy,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch
from clan_lib.persist.util import set_value_by_path
if TYPE_CHECKING:
from .age_keys import KeyPair
@@ -75,7 +75,7 @@ def test_add_module_to_inventory(
inventory_store = InventoryStore(Flake(str(test_flake_with_core.path)))
inventory = inventory_store.read()
apply_patch(
set_value_by_path(
inventory,
"services",
{

View File

@@ -5,7 +5,7 @@ from clan_lib.nix_models.inventory import (
Machine as InventoryMachine,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch
from clan_lib.persist.util import set_value_by_path
@API.register
@@ -27,7 +27,7 @@ def update_machine(machine: Machine, update: InventoryMachine) -> None:
inventory_store = InventoryStore(flake=machine.flake)
inventory = inventory_store.read()
apply_patch(inventory, f"machines.{machine.name}", update)
set_value_by_path(inventory, f"machines.{machine.name}", update)
inventory_store.write(
inventory, message=f"Update information about machine {machine.name}"
)

View File

@@ -8,11 +8,11 @@ from clan_lib.git import commit_file
from clan_lib.nix_models.inventory import Inventory
from .util import (
apply_patch,
calc_patches,
delete_by_path,
determine_writeability,
path_match,
set_value_by_path,
)
@@ -239,7 +239,7 @@ class InventoryStore:
persisted = dict(write_info.data_disk)
for patch_path, data in patchset.items():
apply_patch(persisted, patch_path, data)
set_value_by_path(persisted, patch_path, data)
for delete_path in delete_set:
delete_by_path(persisted, delete_path)

View File

@@ -11,7 +11,7 @@ import pytest
from clan_lib.errors import ClanError
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch, delete_by_path
from clan_lib.persist.util import delete_by_path, set_value_by_path
class MockFlake:
@@ -94,7 +94,7 @@ def test_simple_read_write() -> None:
data: dict = store.read() # type: ignore
assert data == {"foo": "bar", "protected": "protected"}
apply_patch(data, "foo", "foo") # type: ignore
set_value_by_path(data, "foo", "foo") # type: ignore
store.write(data, "test", commit=False) # type: ignore
# Default method to access the inventory
assert store.read() == {"foo": "foo", "protected": "protected"}
@@ -144,7 +144,7 @@ def test_read_deferred() -> None:
assert data == {"foo": {"a": {}, "b": {}}}
# Create a new "deferredModule" "C"
apply_patch(data, "foo.c", {})
set_value_by_path(data, "foo.c", {})
store.write(data, "test", commit=False) # type: ignore
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}}
@@ -155,7 +155,7 @@ def test_read_deferred() -> None:
assert store.read() == {"foo": {"a": {}, "b": {}}}
# Write settings into a new "deferredModule" "C" and read them back
apply_patch(data, "foo.c", {"timeout": "1s"})
set_value_by_path(data, "foo.c", {"timeout": "1s"})
store.write(data, "test", commit=False) # type: ignore
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {"timeout": "1s"}}}

View File

@@ -368,7 +368,7 @@ def delete_by_path(d: dict[str, Any], path: str) -> Any:
type DictLike = dict[str, Any] | Any
def apply_patch(d: DictLike, path: str, content: Any) -> None:
def set_value_by_path(d: DictLike, path: str, content: Any) -> None:
"""
Update the value at a specific dot-separated path in a nested dictionary.

View File

@@ -6,11 +6,11 @@ import pytest
from clan_lib.errors import ClanError
from clan_lib.persist.util import (
apply_patch,
calc_patches,
delete_by_path,
determine_writeability,
path_match,
set_value_by_path,
unmerge_lists,
)
@@ -66,7 +66,7 @@ def test_path_match(
def test_patch_nested() -> None:
orig = {"a": 1, "b": {"a": 2.1, "b": 2.2}, "c": 3}
apply_patch(orig, "b.b", "foo")
set_value_by_path(orig, "b.b", "foo")
# Should only update the nested value
assert orig == {"a": 1, "b": {"a": 2.1, "b": "foo"}, "c": 3}
@@ -77,7 +77,7 @@ def test_patch_nested_dict() -> None:
# This should update the whole "b" dict
# Which also removes all other keys
apply_patch(orig, "b", {"b": "foo"})
set_value_by_path(orig, "b", {"b": "foo"})
# Should only update the nested value
assert orig == {"a": 1, "b": {"b": "foo"}, "c": 3}
@@ -86,13 +86,13 @@ def test_patch_nested_dict() -> None:
def test_create_missing_paths() -> None:
orig = {"a": 1}
apply_patch(orig, "b.c", "foo")
set_value_by_path(orig, "b.c", "foo")
# Should only update the nested value
assert orig == {"a": 1, "b": {"c": "foo"}}
orig = {}
apply_patch(orig, "a.b.c", "foo")
set_value_by_path(orig, "a.b.c", "foo")
assert orig == {"a": {"b": {"c": "foo"}}}
@@ -270,7 +270,7 @@ def test_update_add_empty_dict() -> None:
update = deepcopy(data_eval)
apply_patch(update, "foo.mimi", {})
set_value_by_path(update, "foo.mimi", {})
patchset, _ = calc_patches(
data_disk, update, all_values=data_eval, writeables=writeables
@@ -452,7 +452,7 @@ def test_dont_persist_defaults() -> None:
assert writeables == {"writeable": {"config", "enabled"}, "non_writeable": set()}
update = deepcopy(data_eval)
apply_patch(update, "config.foo", "foo")
set_value_by_path(update, "config.foo", "foo")
patchset, delete_set = calc_patches(
data_disk, update, all_values=data_eval, writeables=writeables