lib/convert_inventory_to_machines: add classmethod for common conversion of Machine and InventoryMachine
This commit is contained in:
@@ -16,19 +16,22 @@ from clan_lib.nix_models.clan import InventoryMachine
|
|||||||
log = logging.getLogger(__name__)
|
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]:
|
def list_full_machines(flake: Flake) -> dict[str, Machine]:
|
||||||
"""
|
"""
|
||||||
Like `list_machines`, but returns a full 'machine' instance for each machine.
|
Like `list_machines`, but returns a full 'machine' instance for each machine.
|
||||||
"""
|
"""
|
||||||
machines = list_machines(flake)
|
machines = list_machines(flake)
|
||||||
|
|
||||||
res: dict[str, Machine] = {}
|
return convert_inventory_to_machines(flake, machines)
|
||||||
|
|
||||||
for name in machines:
|
|
||||||
machine = Machine(name=name, flake=flake)
|
|
||||||
res[machine.name] = machine
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
def query_machines_by_tags(
|
def query_machines_by_tags(
|
||||||
|
|||||||
@@ -29,6 +29,15 @@ class Machine:
|
|||||||
name: str
|
name: str
|
||||||
flake: Flake
|
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":
|
def get_inv_machine(self) -> "InventoryMachine":
|
||||||
return get_machine(self.flake, self.name)
|
return get_machine(self.flake, self.name)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user