From dac30c4dd7b9b790e26ca614693eb5812e2f0a69 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 27 May 2025 19:16:06 +0200 Subject: [PATCH] refactor(persist/utils): rename apply_patch to 'set_value_by_path' --- pkgs/clan-cli/clan_cli/clan/update.py | 4 ++-- pkgs/clan-cli/clan_cli/machines/create.py | 4 ++-- pkgs/clan-cli/clan_cli/tests/test_modules.py | 4 ++-- pkgs/clan-cli/clan_lib/machines/actions.py | 4 ++-- pkgs/clan-cli/clan_lib/persist/inventory_store.py | 4 ++-- .../clan_lib/persist/inventory_store_test.py | 8 ++++---- pkgs/clan-cli/clan_lib/persist/util.py | 2 +- pkgs/clan-cli/clan_lib/persist/util_test.py | 14 +++++++------- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/clan/update.py b/pkgs/clan-cli/clan_cli/clan/update.py index 77dcbed00..e47b002cb 100644 --- a/pkgs/clan-cli/clan_cli/clan/update.py +++ b/pkgs/clan-cli/clan_cli/clan/update.py @@ -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 diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index 7a6f98a96..76a358070 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -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, diff --git a/pkgs/clan-cli/clan_cli/tests/test_modules.py b/pkgs/clan-cli/clan_cli/tests/test_modules.py index dd7f2a20c..ca6d2df1b 100644 --- a/pkgs/clan-cli/clan_cli/tests/test_modules.py +++ b/pkgs/clan-cli/clan_cli/tests/test_modules.py @@ -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", { diff --git a/pkgs/clan-cli/clan_lib/machines/actions.py b/pkgs/clan-cli/clan_lib/machines/actions.py index 1e3ca85cc..e34a34a08 100644 --- a/pkgs/clan-cli/clan_lib/machines/actions.py +++ b/pkgs/clan-cli/clan_lib/machines/actions.py @@ -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}" ) diff --git a/pkgs/clan-cli/clan_lib/persist/inventory_store.py b/pkgs/clan-cli/clan_lib/persist/inventory_store.py index a4c63ca54..f46767baa 100644 --- a/pkgs/clan-cli/clan_lib/persist/inventory_store.py +++ b/pkgs/clan-cli/clan_lib/persist/inventory_store.py @@ -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) diff --git a/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py b/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py index 01da9a63e..581651a11 100644 --- a/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py +++ b/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py @@ -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"}}} diff --git a/pkgs/clan-cli/clan_lib/persist/util.py b/pkgs/clan-cli/clan_lib/persist/util.py index 8716cb0bd..3c389630e 100644 --- a/pkgs/clan-cli/clan_lib/persist/util.py +++ b/pkgs/clan-cli/clan_lib/persist/util.py @@ -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. diff --git a/pkgs/clan-cli/clan_lib/persist/util_test.py b/pkgs/clan-cli/clan_lib/persist/util_test.py index fe6ab2d4d..b6d90275a 100644 --- a/pkgs/clan-cli/clan_lib/persist/util_test.py +++ b/pkgs/clan-cli/clan_lib/persist/util_test.py @@ -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