storage_handler.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """Storage handler."""
  2. from __future__ import annotations
  3. from abc import ABC, abstractmethod
  4. from typing import TYPE_CHECKING, Final
  5. from wandb.sdk.lib.paths import FilePathStr, URIStr
  6. if TYPE_CHECKING:
  7. from urllib.parse import ParseResult
  8. from wandb.sdk.artifacts.artifact import Artifact
  9. from wandb.sdk.artifacts.artifact_manifest_entry import ArtifactManifestEntry
  10. DEFAULT_MAX_OBJECTS: Final[int] = 10_000_000 # 10**7
  11. class _BaseStorageHandler(ABC):
  12. @abstractmethod
  13. def load_path(
  14. self,
  15. manifest_entry: ArtifactManifestEntry,
  16. local: bool = False,
  17. ) -> URIStr | FilePathStr:
  18. """Load a file or directory given the corresponding index entry.
  19. Args:
  20. manifest_entry: The index entry to load
  21. local: Whether to load the file locally or not
  22. Returns:
  23. A path to the file represented by `index_entry`
  24. """
  25. raise NotImplementedError
  26. @abstractmethod
  27. def store_path(
  28. self,
  29. artifact: Artifact,
  30. path: URIStr | FilePathStr,
  31. name: str | None = None,
  32. checksum: bool = True,
  33. max_objects: int | None = None,
  34. ) -> list[ArtifactManifestEntry]:
  35. """Store the file or directory at the given path to the specified artifact.
  36. Args:
  37. path: The path to store
  38. name: If specified, the logical name that should map to `path`
  39. checksum: Whether to compute the checksum of the file
  40. max_objects: The maximum number of objects to store
  41. Returns:
  42. A list of manifest entries to store within the artifact
  43. """
  44. raise NotImplementedError
  45. class StorageHandler(_BaseStorageHandler, ABC): # Handles a single storage protocol
  46. @abstractmethod
  47. def can_handle(self, parsed_url: ParseResult) -> bool:
  48. """Checks whether this handler can handle the given url.
  49. Returns:
  50. Whether this handler can handle the given url.
  51. """
  52. raise NotImplementedError