Files
optuna-dashboard/optuna_dashboard/artifact/_backend_to_store.py
T
2023-08-08 12:17:04 +09:00

40 lines
1.2 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import BinaryIO
from typing import TypeGuard
from optuna.artifacts._protocol import ArtifactStore
from .protocol import ArtifactBackend
def is_artifact_backend(store: ArtifactBackend | ArtifactStore) -> TypeGuard[ArtifactBackend]:
return getattr(store, "open_reader", None) is None
def to_artifact_store(store: ArtifactBackend | ArtifactStore) -> ArtifactStore:
if is_artifact_backend(store):
return ArtifactBackendToStore(store)
# mypy cannot infer the type of `store` here.
return store # type: ignore
class ArtifactBackendToStore:
"""Converts a Dashboard's ArtifactBackend to Optuna's ArtifactStore."""
def __init__(self, artifact_backend: ArtifactBackend) -> None:
self._backend = artifact_backend
def open_reader(self, artifact_id: str) -> BinaryIO:
return self._backend.open(artifact_id)
def write(self, artifact_id: str, content_body: BinaryIO) -> None:
self._backend.write(artifact_id, content_body)
def remove(self, artifact_id: str) -> None:
self._backend.remove(artifact_id)