refactor(lib/persist): move WriteInfo class into persistence lib

This commit is contained in:
Johannes Kirschbauer
2025-05-14 10:19:07 +02:00
parent 925756f7e0
commit 1a4922817a
2 changed files with 10 additions and 22 deletions

View File

@@ -1,4 +1,5 @@
import json import json
from dataclasses import dataclass
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
from clan_cli.flake import Flake from clan_cli.flake import Flake
@@ -7,7 +8,6 @@ from clan_cli.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 (
WriteInfo,
calc_patches, calc_patches,
delete_by_path, delete_by_path,
determine_writeability, determine_writeability,
@@ -15,6 +15,13 @@ from .util import (
) )
@dataclass
class WriteInfo:
writeables: dict[str, set[str]]
data_eval: Inventory
data_disk: Inventory
class InventoryStore: class InventoryStore:
def __init__( def __init__(
self, self,

View File

@@ -1,25 +1,13 @@
""" """
All read/write operations MUST use the inventory. Utilities for working with nested dictionaries, particularly for
flattening, unmerging lists, finding duplicates, and calculating patches.
Machine data, clan data or service data can be accessed in a performant way.
This file exports stable classnames for static & dynamic type safety.
Utilize:
- load_inventory_eval: To load the actual inventory with nix declarations merged.
Operate on the returned inventory to make changes
- save_inventory: To persist changes.
""" """
from collections import Counter from collections import Counter
from dataclasses import dataclass
from typing import Any from typing import Any
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
from clan_lib.nix_models.inventory import Inventory
def flatten_data(data: dict, parent_key: str = "", separator: str = ".") -> dict: def flatten_data(data: dict, parent_key: str = "", separator: str = ".") -> dict:
""" """
@@ -334,10 +322,3 @@ def patch(d: dict[str, Any], path: str, content: Any) -> None:
for key in keys[:-1]: for key in keys[:-1]:
current = current.setdefault(key, {}) current = current.setdefault(key, {})
current[keys[-1]] = content current[keys[-1]] = content
@dataclass
class WriteInfo:
writeables: dict[str, set[str]]
data_eval: Inventory
data_disk: Inventory