From 53d658a3c039acaa8b4d027fdd15566ce4207c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 16 Feb 2024 14:47:39 +0100 Subject: [PATCH] make facts stores inherit from an interface --- .../clan_cli/facts/modules/__init__.py | 28 +++++++++++++++++++ .../clan_cli/facts/modules/in_repo.py | 10 ++++--- pkgs/clan-cli/clan_cli/facts/modules/vm.py | 4 ++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/facts/modules/__init__.py b/pkgs/clan-cli/clan_cli/facts/modules/__init__.py index e69de29bb..a53ba10c0 100644 --- a/pkgs/clan-cli/clan_cli/facts/modules/__init__.py +++ b/pkgs/clan-cli/clan_cli/facts/modules/__init__.py @@ -0,0 +1,28 @@ +from abc import ABC, abstractmethod +from pathlib import Path + +from clan_cli.machines.machines import Machine + + +class FactStoreBase(ABC): + @abstractmethod + def __init__(self, machine: Machine) -> None: + pass + + @abstractmethod + def exists(self, service: str, name: str) -> bool: + pass + + @abstractmethod + def set(self, service: str, name: str, value: bytes) -> Path | None: + pass + + # get a single fact + @abstractmethod + def get(self, service: str, name: str) -> bytes: + pass + + # get all facts + @abstractmethod + def get_all(self) -> dict[str, dict[str, bytes]]: + pass diff --git a/pkgs/clan-cli/clan_cli/facts/modules/in_repo.py b/pkgs/clan-cli/clan_cli/facts/modules/in_repo.py index 5806b1294..f6aad79b8 100644 --- a/pkgs/clan-cli/clan_cli/facts/modules/in_repo.py +++ b/pkgs/clan-cli/clan_cli/facts/modules/in_repo.py @@ -3,13 +3,15 @@ from pathlib import Path from clan_cli.errors import ClanError from clan_cli.machines.machines import Machine +from . import FactStoreBase -class FactStore: + +class FactStore(FactStoreBase): def __init__(self, machine: Machine) -> None: self.machine = machine self.works_remotely = False - def set(self, _service: str, name: str, value: bytes) -> Path | None: + def set(self, service: str, name: str, value: bytes) -> Path | None: if isinstance(self.machine.flake, Path): fact_path = ( self.machine.flake / "machines" / self.machine.name / "facts" / name @@ -23,14 +25,14 @@ class FactStore: f"in_flake fact storage is only supported for local flakes: {self.machine.flake}" ) - def exists(self, _service: str, name: str) -> bool: + def exists(self, service: str, name: str) -> bool: fact_path = ( self.machine.flake_dir / "machines" / self.machine.name / "facts" / name ) return fact_path.exists() # get a single fact - def get(self, _service: str, name: str) -> bytes: + def get(self, service: str, name: str) -> bytes: fact_path = ( self.machine.flake_dir / "machines" / self.machine.name / "facts" / name ) diff --git a/pkgs/clan-cli/clan_cli/facts/modules/vm.py b/pkgs/clan-cli/clan_cli/facts/modules/vm.py index c2d5817ab..10e0c6b7c 100644 --- a/pkgs/clan-cli/clan_cli/facts/modules/vm.py +++ b/pkgs/clan-cli/clan_cli/facts/modules/vm.py @@ -5,10 +5,12 @@ from clan_cli.dirs import vm_state_dir from clan_cli.errors import ClanError from clan_cli.machines.machines import Machine +from . import FactStoreBase + log = logging.getLogger(__name__) -class FactStore: +class FactStore(FactStoreBase): def __init__(self, machine: Machine) -> None: self.machine = machine self.works_remotely = False