refactor(persist/utils): rename apply_patch to 'set_value_by_path'
This commit is contained in:
@@ -4,7 +4,7 @@ from clan_lib.api import API
|
|||||||
from clan_lib.flake import Flake
|
from clan_lib.flake import Flake
|
||||||
from clan_lib.nix_models.inventory import Inventory, Meta
|
from clan_lib.nix_models.inventory import Inventory, Meta
|
||||||
from clan_lib.persist.inventory_store import InventoryStore
|
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
|
@dataclass
|
||||||
@@ -17,7 +17,7 @@ class UpdateOptions:
|
|||||||
def update_clan_meta(options: UpdateOptions) -> Inventory:
|
def update_clan_meta(options: UpdateOptions) -> Inventory:
|
||||||
inventory_store = InventoryStore(options.flake)
|
inventory_store = InventoryStore(options.flake)
|
||||||
inventory = inventory_store.read()
|
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")
|
inventory_store.write(inventory, message="Update clan metadata")
|
||||||
|
|
||||||
return inventory
|
return inventory
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ from clan_lib.nix_models.inventory import (
|
|||||||
MachineDeploy,
|
MachineDeploy,
|
||||||
)
|
)
|
||||||
from clan_lib.persist.inventory_store import InventoryStore
|
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 (
|
from clan_lib.templates import (
|
||||||
InputPrio,
|
InputPrio,
|
||||||
TemplateName,
|
TemplateName,
|
||||||
@@ -130,7 +130,7 @@ def create_machine(
|
|||||||
)
|
)
|
||||||
raise ClanError(msg, description=description)
|
raise ClanError(msg, description=description)
|
||||||
|
|
||||||
apply_patch(
|
set_value_by_path(
|
||||||
inventory,
|
inventory,
|
||||||
f"machines.{machine_name}",
|
f"machines.{machine_name}",
|
||||||
new_machine,
|
new_machine,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from clan_lib.nix_models.inventory import (
|
|||||||
MachineDeploy,
|
MachineDeploy,
|
||||||
)
|
)
|
||||||
from clan_lib.persist.inventory_store import InventoryStore
|
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:
|
if TYPE_CHECKING:
|
||||||
from .age_keys import KeyPair
|
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_store = InventoryStore(Flake(str(test_flake_with_core.path)))
|
||||||
inventory = inventory_store.read()
|
inventory = inventory_store.read()
|
||||||
|
|
||||||
apply_patch(
|
set_value_by_path(
|
||||||
inventory,
|
inventory,
|
||||||
"services",
|
"services",
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from clan_lib.nix_models.inventory import (
|
|||||||
Machine as InventoryMachine,
|
Machine as InventoryMachine,
|
||||||
)
|
)
|
||||||
from clan_lib.persist.inventory_store import InventoryStore
|
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
|
@API.register
|
||||||
@@ -27,7 +27,7 @@ def update_machine(machine: Machine, update: InventoryMachine) -> None:
|
|||||||
inventory_store = InventoryStore(flake=machine.flake)
|
inventory_store = InventoryStore(flake=machine.flake)
|
||||||
inventory = inventory_store.read()
|
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_store.write(
|
||||||
inventory, message=f"Update information about machine {machine.name}"
|
inventory, message=f"Update information about machine {machine.name}"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ from clan_lib.git import commit_file
|
|||||||
from clan_lib.nix_models.inventory import Inventory
|
from clan_lib.nix_models.inventory import Inventory
|
||||||
|
|
||||||
from .util import (
|
from .util import (
|
||||||
apply_patch,
|
|
||||||
calc_patches,
|
calc_patches,
|
||||||
delete_by_path,
|
delete_by_path,
|
||||||
determine_writeability,
|
determine_writeability,
|
||||||
path_match,
|
path_match,
|
||||||
|
set_value_by_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -239,7 +239,7 @@ class InventoryStore:
|
|||||||
|
|
||||||
persisted = dict(write_info.data_disk)
|
persisted = dict(write_info.data_disk)
|
||||||
for patch_path, data in patchset.items():
|
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:
|
for delete_path in delete_set:
|
||||||
delete_by_path(persisted, delete_path)
|
delete_by_path(persisted, delete_path)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import pytest
|
|||||||
|
|
||||||
from clan_lib.errors import ClanError
|
from clan_lib.errors import ClanError
|
||||||
from clan_lib.persist.inventory_store import InventoryStore
|
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:
|
class MockFlake:
|
||||||
@@ -94,7 +94,7 @@ def test_simple_read_write() -> None:
|
|||||||
data: dict = store.read() # type: ignore
|
data: dict = store.read() # type: ignore
|
||||||
assert data == {"foo": "bar", "protected": "protected"}
|
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
|
store.write(data, "test", commit=False) # type: ignore
|
||||||
# Default method to access the inventory
|
# Default method to access the inventory
|
||||||
assert store.read() == {"foo": "foo", "protected": "protected"}
|
assert store.read() == {"foo": "foo", "protected": "protected"}
|
||||||
@@ -144,7 +144,7 @@ def test_read_deferred() -> None:
|
|||||||
assert data == {"foo": {"a": {}, "b": {}}}
|
assert data == {"foo": {"a": {}, "b": {}}}
|
||||||
|
|
||||||
# Create a new "deferredModule" "C"
|
# 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
|
store.write(data, "test", commit=False) # type: ignore
|
||||||
|
|
||||||
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}}
|
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}}
|
||||||
@@ -155,7 +155,7 @@ def test_read_deferred() -> None:
|
|||||||
assert store.read() == {"foo": {"a": {}, "b": {}}}
|
assert store.read() == {"foo": {"a": {}, "b": {}}}
|
||||||
|
|
||||||
# Write settings into a new "deferredModule" "C" and read them back
|
# 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
|
store.write(data, "test", commit=False) # type: ignore
|
||||||
|
|
||||||
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {"timeout": "1s"}}}
|
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {"timeout": "1s"}}}
|
||||||
|
|||||||
@@ -368,7 +368,7 @@ def delete_by_path(d: dict[str, Any], path: str) -> Any:
|
|||||||
type DictLike = dict[str, Any] | 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.
|
Update the value at a specific dot-separated path in a nested dictionary.
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ import pytest
|
|||||||
|
|
||||||
from clan_lib.errors import ClanError
|
from clan_lib.errors import ClanError
|
||||||
from clan_lib.persist.util import (
|
from clan_lib.persist.util import (
|
||||||
apply_patch,
|
|
||||||
calc_patches,
|
calc_patches,
|
||||||
delete_by_path,
|
delete_by_path,
|
||||||
determine_writeability,
|
determine_writeability,
|
||||||
path_match,
|
path_match,
|
||||||
|
set_value_by_path,
|
||||||
unmerge_lists,
|
unmerge_lists,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ def test_path_match(
|
|||||||
def test_patch_nested() -> None:
|
def test_patch_nested() -> None:
|
||||||
orig = {"a": 1, "b": {"a": 2.1, "b": 2.2}, "c": 3}
|
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
|
# Should only update the nested value
|
||||||
assert orig == {"a": 1, "b": {"a": 2.1, "b": "foo"}, "c": 3}
|
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
|
# This should update the whole "b" dict
|
||||||
# Which also removes all other keys
|
# 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
|
# Should only update the nested value
|
||||||
assert orig == {"a": 1, "b": {"b": "foo"}, "c": 3}
|
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:
|
def test_create_missing_paths() -> None:
|
||||||
orig = {"a": 1}
|
orig = {"a": 1}
|
||||||
|
|
||||||
apply_patch(orig, "b.c", "foo")
|
set_value_by_path(orig, "b.c", "foo")
|
||||||
|
|
||||||
# Should only update the nested value
|
# Should only update the nested value
|
||||||
assert orig == {"a": 1, "b": {"c": "foo"}}
|
assert orig == {"a": 1, "b": {"c": "foo"}}
|
||||||
|
|
||||||
orig = {}
|
orig = {}
|
||||||
apply_patch(orig, "a.b.c", "foo")
|
set_value_by_path(orig, "a.b.c", "foo")
|
||||||
|
|
||||||
assert 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)
|
update = deepcopy(data_eval)
|
||||||
|
|
||||||
apply_patch(update, "foo.mimi", {})
|
set_value_by_path(update, "foo.mimi", {})
|
||||||
|
|
||||||
patchset, _ = calc_patches(
|
patchset, _ = calc_patches(
|
||||||
data_disk, update, all_values=data_eval, writeables=writeables
|
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()}
|
assert writeables == {"writeable": {"config", "enabled"}, "non_writeable": set()}
|
||||||
|
|
||||||
update = deepcopy(data_eval)
|
update = deepcopy(data_eval)
|
||||||
apply_patch(update, "config.foo", "foo")
|
set_value_by_path(update, "config.foo", "foo")
|
||||||
|
|
||||||
patchset, delete_set = calc_patches(
|
patchset, delete_set = calc_patches(
|
||||||
data_disk, update, all_values=data_eval, writeables=writeables
|
data_disk, update, all_values=data_eval, writeables=writeables
|
||||||
|
|||||||
Reference in New Issue
Block a user