mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-10-29 13:20:48 +08:00
40 lines
1.2 KiB
Python
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)
|