Source code for qibolab._core.platform.load
import importlib.util
import os
from pathlib import Path
from typing import Optional, Union
from ..parameters import Hardware
from .platform import Platform
__all__ = ["create_platform", "locate_platform"]
PLATFORM = "platform.py"
PLATFORMS = "QIBOLAB_PLATFORMS"
def _platforms_paths() -> list[Path]:
"""Get path to repository containing the platforms.
Path is specified using the environment variable QIBOLAB_PLATFORMS.
"""
paths = os.environ.get(PLATFORMS)
if paths is None:
raise RuntimeError(f"Platforms path ${PLATFORMS} unset.")
return [Path(p) for p in paths.split(os.pathsep)]
def _search(name: str, paths: list[Path]) -> Path:
"""Search paths for given platform name."""
for path in paths:
platform = path / name
if platform.exists():
return platform
raise ValueError(
f"Platform {name} not found. Check ${PLATFORMS} environment variable.",
)
def _load(platform: Path) -> Union[Platform, Hardware]:
"""Load the platform module."""
module_name = "platform"
spec = importlib.util.spec_from_file_location(module_name, platform / PLATFORM)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module.create()
def available_platforms() -> list[str]:
"""Returns the platforms found in the $QIBOLAB_PLATFORMS directory."""
return [
d.name
for platforms in _platforms_paths()
for d in platforms.iterdir()
if d.is_dir()
and Path(f"{os.environ.get(PLATFORMS)}/{d.name}/platform.py") in d.iterdir()
]