From 91df5c258e0bd8a0ced7872e9595c336809ee54b Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Fri, 4 Jul 2025 10:18:14 +0200 Subject: [PATCH] lib/convert_inventory_to_machines: add classmethod for common conversion of Machine and InventoryMachine --- pkgs/clan-cli/clan_lib/machines/list.py | 17 ++++++++++------- pkgs/clan-cli/clan_lib/machines/machines.py | 9 +++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pkgs/clan-cli/clan_lib/machines/list.py b/pkgs/clan-cli/clan_lib/machines/list.py index bb292f2a2..c595b8f69 100644 --- a/pkgs/clan-cli/clan_lib/machines/list.py +++ b/pkgs/clan-cli/clan_lib/machines/list.py @@ -16,19 +16,22 @@ from clan_lib.nix_models.clan import InventoryMachine log = logging.getLogger(__name__) +def convert_inventory_to_machines( + flake: Flake, machines: dict[str, InventoryMachine] +) -> dict[str, Machine]: + return { + name: Machine.from_inventory(name, flake, inventory_machine) + for name, inventory_machine in machines.items() + } + + def list_full_machines(flake: Flake) -> dict[str, Machine]: """ Like `list_machines`, but returns a full 'machine' instance for each machine. """ machines = list_machines(flake) - res: dict[str, Machine] = {} - - for name in machines: - machine = Machine(name=name, flake=flake) - res[machine.name] = machine - - return res + return convert_inventory_to_machines(flake, machines) def query_machines_by_tags( diff --git a/pkgs/clan-cli/clan_lib/machines/machines.py b/pkgs/clan-cli/clan_lib/machines/machines.py index 80d8431aa..6cf07a2cb 100644 --- a/pkgs/clan-cli/clan_lib/machines/machines.py +++ b/pkgs/clan-cli/clan_lib/machines/machines.py @@ -29,6 +29,15 @@ class Machine: name: str flake: Flake + @classmethod + def from_inventory( + cls, + name: str, + flake: Flake, + _inventory_machine: InventoryMachine, + ) -> "Machine": + return cls(name=name, flake=flake) + def get_inv_machine(self) -> "InventoryMachine": return get_machine(self.flake, self.name)