Skip to content

Store Module

store

Classes

KeyBuilder

KeyBuilder(split_char: str = '/', prefix: str = __title__)

Utility class for building store keys with a consistent format.

Attributes:

Name Type Description
split_char

Character used to split parts of the key.

prefix

Prefix to prepend to all keys.

Source code in audex/lib/store/__init__.py
def __init__(self, split_char: str = "/", prefix: str = __title__) -> None:
    self.split_char = split_char
    self.prefix = prefix
Functions
build
build(*parts: str) -> str

Build a store key by joining the prefix and parts.

Parameters:

Name Type Description Default
*parts str

Parts to include in the key.

()

Returns:

Type Description
str

The constructed store key.

Source code in audex/lib/store/__init__.py
def build(self, *parts: str) -> str:
    """Build a store key by joining the prefix and parts.

    Args:
        *parts: Parts to include in the key.

    Returns:
        The constructed store key.
    """
    return self.split_char.join((self.prefix, *parts))
validate
validate(key: str) -> bool

Validate if a given key starts with the defined prefix.

Parameters:

Name Type Description Default
key str

The store key to validate.

required

Returns:

Type Description
bool

True if the key starts with the prefix, False otherwise.

Source code in audex/lib/store/__init__.py
def validate(self, key: str) -> bool:
    """Validate if a given key starts with the defined prefix.

    Args:
        key: The store key to validate.

    Returns:
        True if the key starts with the prefix, False otherwise.
    """
    return key.startswith(self.prefix + self.split_char)

Store

Store()

Bases: LoggingMixin, ABC

Abstract base class for storage operations.

This class defines the interface for storage backends, providing methods for uploading, downloading, deleting, and managing stored objects.

Source code in audex/helper/mixin.py
def __init__(self) -> None:
    self.logger = loguru.logger.bind(tag=self.__logtag__)
Attributes
key_builder abstractmethod property
key_builder: KeyBuilder

Get a KeyBuilder instance for constructing store keys.

Returns:

Type Description
KeyBuilder

An instance of KeyBuilder.

Functions
upload abstractmethod async
upload(data: bytes | IO[bytes], key: str, metadata: Mapping[str, Any] | None = None, **kwargs: Any) -> str

Upload data to storage.

Parameters:

Name Type Description Default
data bytes | IO[bytes]

The data to upload, either as bytes or a file-like object.

required
key str

The unique identifier for the stored object.

required
metadata Mapping[str, Any] | None

Optional metadata to associate with the object.

None
**kwargs Any

Additional storage-specific parameters.

{}

Returns:

Type Description
str

The key of the uploaded object.

Raises:

Type Description
Exception

If the upload fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def upload(
    self,
    data: bytes | t.IO[bytes],
    key: str,
    metadata: t.Mapping[str, t.Any] | None = None,
    **kwargs: t.Any,
) -> str:
    """Upload data to storage.

    Args:
        data: The data to upload, either as bytes or a file-like object.
        key: The unique identifier for the stored object.
        metadata: Optional metadata to associate with the object.
        **kwargs: Additional storage-specific parameters.

    Returns:
        The key of the uploaded object.

    Raises:
        Exception: If the upload fails.
    """
upload_multipart abstractmethod async
upload_multipart(parts: AsyncIterable[bytes], key: str, metadata: Mapping[str, Any] | None = None, **kwargs: Any) -> str

Upload data in multiple parts.

Parameters:

Name Type Description Default
parts AsyncIterable[bytes]

An async iterable of byte chunks to upload.

required
key str

The unique identifier for the stored object.

required
metadata Mapping[str, Any] | None

Optional metadata to associate with the object.

None
**kwargs Any

Additional storage-specific parameters.

{}

Returns:

Type Description
str

The key of the uploaded object.

Raises:

Type Description
Exception

If the multipart upload fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def upload_multipart(
    self,
    parts: t.AsyncIterable[bytes],
    key: str,
    metadata: t.Mapping[str, t.Any] | None = None,
    **kwargs: t.Any,
) -> str:
    """Upload data in multiple parts.

    Args:
        parts: An async iterable of byte chunks to upload.
        key: The unique identifier for the stored object.
        metadata: Optional metadata to associate with the object.
        **kwargs: Additional storage-specific parameters.

    Returns:
        The key of the uploaded object.

    Raises:
        Exception: If the multipart upload fails.
    """
get_metadata abstractmethod async
get_metadata(key: str) -> dict[str, Any]

Retrieve metadata for a stored object.

Parameters:

Name Type Description Default
key str

The unique identifier of the object.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the object's metadata.

Raises:

Type Description
Exception

If the object doesn't exist or metadata retrieval fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def get_metadata(self, key: str) -> builtins.dict[str, t.Any]:
    """Retrieve metadata for a stored object.

    Args:
        key: The unique identifier of the object.

    Returns:
        A dictionary containing the object's metadata.

    Raises:
        Exception: If the object doesn't exist or metadata retrieval fails.
    """
download abstractmethod async
download(key: str, *, stream: Literal[False] = False, chunk_size: int = 8192, **kwargs: Any) -> bytes
download(key: str, *, stream: Literal[True], chunk_size: int = 8192, **kwargs: Any) -> bytes
download(key: str, *, stream: bool = False, chunk_size: int = 8192, **kwargs: Any) -> bytes | AsyncIterable[bytes]

Download data from storage.

Parameters:

Name Type Description Default
key str

The unique identifier of the object to download.

required
stream bool

If True, return an async iterable of chunks; otherwise return all bytes.

False
chunk_size int

Size of each chunk when streaming (in bytes).

8192
**kwargs Any

Additional storage-specific parameters.

{}

Returns:

Type Description
bytes | AsyncIterable[bytes]

The object's data as bytes, or an async iterable of byte chunks if streaming.

Raises:

Type Description
Exception

If the download fails or object doesn't exist.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def download(
    self,
    key: str,
    *,
    stream: bool = False,
    chunk_size: int = 8192,
    **kwargs: t.Any,
) -> bytes | t.AsyncIterable[bytes]:
    """Download data from storage.

    Args:
        key: The unique identifier of the object to download.
        stream: If True, return an async iterable of chunks; otherwise return all bytes.
        chunk_size: Size of each chunk when streaming (in bytes).
        **kwargs: Additional storage-specific parameters.

    Returns:
        The object's data as bytes, or an async iterable of byte chunks if streaming.

    Raises:
        Exception: If the download fails or object doesn't exist.
    """
delete abstractmethod async
delete(key: str) -> None

Delete an object from storage.

Parameters:

Name Type Description Default
key str

The unique identifier of the object to delete.

required

Raises:

Type Description
Exception

If the deletion fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def delete(self, key: str) -> None:
    """Delete an object from storage.

    Args:
        key: The unique identifier of the object to delete.

    Raises:
        Exception: If the deletion fails.
    """
list abstractmethod
list(prefix: str = '', page_size: int = 10, **kwargs: Any) -> AsyncIterable[list[str]]

List objects in storage with the given prefix.

Parameters:

Name Type Description Default
prefix str

Optional prefix to filter objects.

''
page_size int

Number of object keys to return per iteration.

10
**kwargs Any

Additional storage-specific parameters.

{}

Yields:

Type Description
AsyncIterable[list[str]]

Lists of object keys matching the prefix.

Raises:

Type Description
Exception

If listing fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
def list(
    self,
    prefix: str = "",
    page_size: int = 10,
    **kwargs: t.Any,
) -> t.AsyncIterable[builtins.list[str]]:
    """List objects in storage with the given prefix.

    Args:
        prefix: Optional prefix to filter objects.
        page_size: Number of object keys to return per iteration.
        **kwargs: Additional storage-specific parameters.

    Yields:
        Lists of object keys matching the prefix.

    Raises:
        Exception: If listing fails.
    """
exists abstractmethod async
exists(key: str) -> bool

Check if an object exists in storage.

Parameters:

Name Type Description Default
key str

The unique identifier of the object.

required

Returns:

Type Description
bool

True if the object exists, False otherwise.

Raises:

Type Description
Exception

If the existence check fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def exists(self, key: str) -> bool:
    """Check if an object exists in storage.

    Args:
        key: The unique identifier of the object.

    Returns:
        True if the object exists, False otherwise.

    Raises:
        Exception: If the existence check fails.
    """
clear abstractmethod async
clear(prefix: str = '') -> None

Delete all objects with the given prefix.

Parameters:

Name Type Description Default
prefix str

Optional prefix to filter objects for deletion.

''

Raises:

Type Description
Exception

If the clear operation fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def clear(self, prefix: str = "") -> None:
    """Delete all objects with the given prefix.

    Args:
        prefix: Optional prefix to filter objects for deletion.

    Raises:
        Exception: If the clear operation fails.
    """
copy abstractmethod async
copy(source_key: str, dest_key: str, **kwargs: Any) -> str

Copy an object to a new location.

Parameters:

Name Type Description Default
source_key str

The unique identifier of the source object.

required
dest_key str

The unique identifier for the destination object.

required
**kwargs Any

Additional storage-specific parameters.

{}

Returns:

Type Description
str

The key of the copied object.

Raises:

Type Description
Exception

If the copy operation fails.

Source code in audex/lib/store/__init__.py
@abc.abstractmethod
async def copy(self, source_key: str, dest_key: str, **kwargs: t.Any) -> str:
    """Copy an object to a new location.

    Args:
        source_key: The unique identifier of the source object.
        dest_key: The unique identifier for the destination object.
        **kwargs: Additional storage-specific parameters.

    Returns:
        The key of the copied object.

    Raises:
        Exception: If the copy operation fails.
    """

options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true