aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/backend/config.py
diff options
context:
space:
mode:
authorBenjamin Klimczak <benjamin.klimczak@arm.com>2022-12-14 11:20:11 +0000
committerBenjamin Klimczak <benjamin.klimczak@arm.com>2023-01-04 10:11:33 +0000
commitdcd0bd31985c27e1d07333351b26cf8ad12ad1fd (patch)
treea3388ff5f91e7cdc7ec41271a1a76cdbfae38ece /src/mlia/backend/config.py
parent4b4cf29cb1e7d917ae001e258ff01f7846c34778 (diff)
downloadmlia-dcd0bd31985c27e1d07333351b26cf8ad12ad1fd.tar.gz
MLIA-589 Create an API to get target information
Change-Id: Ieeaa9188ea1e29e2ccaad7475d457bce71e3140d
Diffstat (limited to 'src/mlia/backend/config.py')
-rw-r--r--src/mlia/backend/config.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/mlia/backend/config.py b/src/mlia/backend/config.py
new file mode 100644
index 0000000..8d14b28
--- /dev/null
+++ b/src/mlia/backend/config.py
@@ -0,0 +1,82 @@
+# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+"""Backend config module."""
+from __future__ import annotations
+
+import platform
+from enum import auto
+from enum import Enum
+from typing import cast
+
+from mlia.core.common import AdviceCategory
+
+
+class System(Enum):
+ """Enum of system configurations (e.g. OS and architecture)."""
+
+ LINUX_AMD64 = ("Linux", "x86_64")
+ LINUX_AARCH64 = ("Linux", "aarch64")
+ WINDOWS_AMD64 = ("Windows", "AMD64")
+ WINDOWS_AARCH64 = ("Windows", "ARM64")
+ DARWIN_AARCH64 = ("Darwin", "arm64")
+ CURRENT = (platform.system(), platform.machine())
+
+ def __init__(
+ self, system: str = platform.system(), machine: str = platform.machine()
+ ) -> None:
+ """Set the system parameters (defaults to the current system)."""
+ self.system = system.lower()
+ self.machine = machine.lower()
+
+ def __eq__(self, other: object) -> bool:
+ """
+ Compare two System instances for equality.
+
+ Raises a TypeError if the input is not a System.
+ """
+ if isinstance(other, self.__class__):
+ return self.system == other.system and self.machine == other.machine
+ return False
+
+ def is_compatible(self) -> bool:
+ """Check if this system is compatible with the current system."""
+ return self == self.CURRENT
+
+
+class BackendType(Enum):
+ """Define the type of the backend (builtin, wheel file etc)."""
+
+ BUILTIN = auto()
+ WHEEL = auto()
+ CUSTOM = auto()
+
+
+class BackendConfiguration:
+ """Base class for backend configurations."""
+
+ def __init__(
+ self,
+ supported_advice: list[AdviceCategory],
+ supported_systems: list[System] | None,
+ backend_type: BackendType,
+ ) -> None:
+ """Set up basic information about the backend."""
+ self.supported_advice = supported_advice
+ self.supported_systems = supported_systems
+ self.type = backend_type
+
+ def __str__(self) -> str:
+ """List supported advice."""
+ return ", ".join(cast(str, adv.name).lower() for adv in self.supported_advice)
+
+ def is_supported(
+ self, advice: AdviceCategory | None = None, check_system: bool = False
+ ) -> bool:
+ """Check backend supports the current system and advice."""
+ is_system_supported = (
+ not self.supported_systems
+ or not check_system
+ or any(sys.is_compatible() for sys in self.supported_systems)
+ )
+ is_advice_supported = advice is None or advice in self.supported_advice
+ return is_system_supported and is_advice_supported