Skip to content

Commit

Permalink
Update documentation and it's style
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Movchan committed Jul 23, 2024
1 parent 2d54a9f commit b9db93a
Show file tree
Hide file tree
Showing 17 changed files with 481 additions and 96 deletions.
3 changes: 3 additions & 0 deletions aana/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from aana.sdk import AanaSDK

__all__ = ["AanaSDK"]
38 changes: 19 additions & 19 deletions aana/core/models/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
class Audio(Media):
"""A class representing an audio.
At least one of 'path', 'url', or 'content' must be provided.
If 'save_on_disk' is True, the audio will be saved on disk automatically.
At least one of `path`, `url`, or `content` must be provided.
If `save_on_disk` is True, the audio will be saved on disk automatically.
Attributes:
path (Path): the path to the audio file
Expand All @@ -34,15 +34,15 @@ class Audio(Media):
numpy: np.ndarray | None = None
audio_lib: type[AbstractAudioLibrary] = pyAVWrapper

def validate(self):
def _validate(self):
"""Validate the audio.
Raises:
ValueError: if none of 'path', 'url', or 'content' is provided
AudioReadingException: if the audio is not valid
"""
# validate the parent class
super().validate()
super()._validate()

# check that at least one of 'path', 'url' or 'content' is provided
if not any(
Expand Down Expand Up @@ -87,19 +87,19 @@ def save(self):
audio_dir.mkdir(parents=True, exist_ok=True)
file_path = audio_dir / (self.media_id + ".wav")
if self.content is not None:
self.save_from_audio_content(file_path)
self._save_from_audio_content(file_path)
elif self.numpy is not None:
self.save_from_numpy(file_path)
self._save_from_numpy(file_path)
elif self.url:
self.save_from_url(file_path)
self._save_from_url(file_path)
else:
raise ValueError( # noqa: TRY003
"At least one of 'path', 'url', 'content' or 'numpy' must be provided."
)
self.path = file_path
self.is_saved = True

def save_from_audio_content(self, file_path: Path):
def _save_from_audio_content(self, file_path: Path):
"""Save the audio from the content.
Args:
Expand All @@ -108,7 +108,7 @@ def save_from_audio_content(self, file_path: Path):
assert self.content is not None # noqa: S101
self.audio_lib.write_audio_bytes(file_path, self.content)

def save_from_audio_url(self, file_path):
def _save_from_audio_url(self, file_path):
"""Save the audio from the URL.
Args:
Expand All @@ -121,7 +121,7 @@ def save_from_audio_url(self, file_path):
content: bytes = download_file(self.url)
self.audio_lib.write_audio_bytes(file_path, content)

def save_from_numpy(self, file_path: Path):
def _save_from_numpy(self, file_path: Path):
"""Save the audio from numpy on disk.
Args:
Expand All @@ -143,19 +143,19 @@ def get_numpy(self) -> np.ndarray:
if self.numpy is not None:
return self.numpy
elif self.path:
self.load_numpy_from_path()
self._load_numpy_from_path()
elif self.url:
self.load_numpy_from_url()
self._load_numpy_from_url()
elif self.content:
self.load_numpy_from_content()
self._load_numpy_from_content()
else:
raise ValueError( # noqa: TRY003
"At least one of 'path', 'url', 'content' or 'numpy' must be provided."
)
assert self.numpy is not None # noqa: S101
return self.numpy

def load_numpy_from_path(self):
def _load_numpy_from_path(self):
"""Load the audio as a numpy array from a path.
Raises:
Expand All @@ -167,7 +167,7 @@ def load_numpy_from_path(self):
except Exception as e:
raise AudioReadingException(self) from e

def load_numpy_from_audio_bytes(self, audio_bytes: bytes):
def _load_numpy_from_audio_bytes(self, audio_bytes: bytes):
"""Load the image as a numpy array from image bytes (downloaded from URL or read from file).
Raises:
Expand All @@ -178,24 +178,24 @@ def load_numpy_from_audio_bytes(self, audio_bytes: bytes):
except Exception as e:
raise AudioReadingException(self) from e

def load_numpy_from_url(self):
def _load_numpy_from_url(self):
"""Load the audio as a numpy array from a URL.
Raises:
AudioReadingException: If there is an error reading the audio.
"""
assert self.url is not None # noqa: S101
content: bytes = download_file(self.url)
self.load_numpy_from_audio_bytes(content)
self._load_numpy_from_audio_bytes(content)

def load_numpy_from_content(self):
def _load_numpy_from_content(self):
"""Load the image as a numpy array from content.
Raises:
ImageReadingException: If there is an error reading the image.
"""
assert self.content is not None # noqa: S101
self.load_numpy_from_audio_bytes(self.content)
self._load_numpy_from_audio_bytes(self.content)

def __repr__(self) -> str:
"""Get the representation of the audio.
Expand Down
42 changes: 21 additions & 21 deletions aana/core/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
class Image(Media):
"""A class representing an image.
At least one of 'path', 'url', 'content' or 'numpy' must be provided.
If 'save_on_disk' is True, the image will be saved on disk automatically.
At least one of `path`, `url`, `content` or `numpy` must be provided.
If `save_on_disk` is True, the image will be saved on disk automatically.
Attributes:
path (Path): The file path of the image.
Expand All @@ -49,10 +49,10 @@ class Image(Media):
AbstractImageLibrary
] = OpenCVWrapper # The image library to use, TODO: add support for PIL and allow to choose the library

def validate(self):
def _validate(self):
"""Validate the image."""
# validate the parent class
super().validate()
super()._validate()

# check that at least one of 'path', 'url', 'content' or 'numpy' is provided
if not any(
Expand Down Expand Up @@ -89,19 +89,19 @@ def save(self):
file_path = image_dir / (self.media_id + ".bmp")

if self.content:
self.save_from_content(file_path)
self._save_from_content(file_path)
elif self.numpy is not None:
self.save_from_numpy(file_path)
self._save_from_numpy(file_path)
elif self.url:
self.save_from_url(file_path)
self._save_from_url(file_path)
else:
raise ValueError( # noqa: TRY003
"At least one of 'path', 'url', 'content' or 'numpy' must be provided."
)
self.path = file_path
self.is_saved = True

def save_from_numpy(self, file_path: Path):
def _save_from_numpy(self, file_path: Path):
"""Save the image from numpy on disk.
Args:
Expand All @@ -123,11 +123,11 @@ def get_numpy(self) -> np.ndarray:
if self.numpy is not None:
return self.numpy
elif self.path:
self.load_numpy_from_path()
self._load_numpy_from_path()
elif self.url:
self.load_numpy_from_url()
self._load_numpy_from_url()
elif self.content:
self.load_numpy_from_content()
self._load_numpy_from_content()
else:
raise ValueError( # noqa: TRY003
"At least one of 'path', 'url', 'content' or 'numpy' must be provided."
Expand All @@ -143,7 +143,7 @@ def get_pil_image(self) -> PIL.Image:
"""
return PIL.Image.fromarray(self.get_numpy())

def load_numpy_from_path(self):
def _load_numpy_from_path(self):
"""Load the image as a numpy array from a path.
Raises:
Expand All @@ -155,7 +155,7 @@ def load_numpy_from_path(self):
except Exception as e:
raise ImageReadingException(self) from e

def load_numpy_from_image_bytes(self, img_bytes: bytes):
def _load_numpy_from_image_bytes(self, img_bytes: bytes):
"""Load the image as a numpy array from image bytes (downloaded from URL or read from file).
Raises:
Expand All @@ -166,24 +166,24 @@ def load_numpy_from_image_bytes(self, img_bytes: bytes):
except Exception as e:
raise ImageReadingException(self) from e

def load_numpy_from_url(self):
def _load_numpy_from_url(self):
"""Load the image as a numpy array from a URL.
Raises:
ImageReadingException: If there is an error reading the image.
"""
assert self.url is not None # noqa: S101
content: bytes = download_file(self.url)
self.load_numpy_from_image_bytes(content)
self._load_numpy_from_image_bytes(content)

def load_numpy_from_content(self):
def _load_numpy_from_content(self):
"""Load the image as a numpy array from content.
Raises:
ImageReadingException: If there is an error reading the image.
"""
assert self.content is not None # noqa: S101
self.load_numpy_from_image_bytes(self.content)
self._load_numpy_from_image_bytes(self.content)

def get_content(self) -> bytes:
"""Get the content of the image as bytes.
Expand All @@ -197,19 +197,19 @@ def get_content(self) -> bytes:
if self.content:
return self.content
elif self.path:
self.load_content_from_path()
self._load_content_from_path()
elif self.url:
self.load_content_from_url()
self._load_content_from_url()
elif self.numpy is not None:
self.load_content_from_numpy()
self._load_content_from_numpy()
else:
raise ValueError( # noqa: TRY003
"At least one of 'path', 'url', 'content' or 'numpy' must be provided."
)
assert self.content is not None # noqa: S101
return self.content

def load_content_from_numpy(self):
def _load_content_from_numpy(self):
"""Load the content of the image from numpy."""
assert self.numpy is not None # noqa: S101
self.content = self.image_lib.write_to_bytes(self.numpy)
Expand Down
30 changes: 15 additions & 15 deletions aana/core/models/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class Media:
It is used to represent images, videos, and audio files.
At least one of 'path', 'url', or 'content' must be provided.
If 'save_on_disk' is True, the media will be saved on disk automatically.
At least one of `path`, `url`, or `content` must be provided.
If `save_on_disk` is True, the media will be saved on disk automatically.
Attributes:
path (Path): the path to the media file
Expand All @@ -69,7 +69,7 @@ class Media:
is_saved: bool = False
media_dir: Path | None = None

def validate(self):
def _validate(self):
"""Validate the media."""
# check that path is a Path object
if self.path and not isinstance(self.path, Path):
Expand All @@ -84,7 +84,7 @@ def __post_init__(self):
Perform checks and save the media on disk if needed.
"""
self.validate()
self._validate()

if self.save_on_disk:
self.save()
Expand All @@ -110,16 +110,16 @@ def save(self):
file_path = self.media_dir / (self.media_id + ".mp4")

if self.content:
self.save_from_content(file_path)
self._save_from_content(file_path)
elif self.url:
self.save_from_url(file_path)
self._save_from_url(file_path)
else:
raise ValueError( # noqa: TRY003
"At least one of 'path', 'url', or 'content' must be provided."
)
self.is_saved = True

def save_from_bytes(self, file_path: Path, content: bytes):
def _save_from_bytes(self, file_path: Path, content: bytes):
"""Save the media from bytes.
Args:
Expand All @@ -129,16 +129,16 @@ def save_from_bytes(self, file_path: Path, content: bytes):
file_path.write_bytes(content)
self.path = file_path

def save_from_content(self, file_path: Path):
def _save_from_content(self, file_path: Path):
"""Save the media from the content.
Args:
file_path (Path): the path to save the media to
"""
assert self.content is not None # noqa: S101
self.save_from_bytes(file_path, self.content)
self._save_from_bytes(file_path, self.content)

def save_from_url(self, file_path):
def _save_from_url(self, file_path):
"""Save the media from the URL.
Args:
Expand All @@ -149,7 +149,7 @@ def save_from_url(self, file_path):
"""
assert self.url is not None # noqa: S101
content: bytes = download_file(self.url)
self.save_from_bytes(file_path, content)
self._save_from_bytes(file_path, content)

def get_content(self) -> bytes:
"""Get the content of the media as bytes.
Expand All @@ -163,22 +163,22 @@ def get_content(self) -> bytes:
if self.content:
return self.content
elif self.path:
self.load_content_from_path()
self._load_content_from_path()
elif self.url:
self.load_content_from_url()
self._load_content_from_url()
else:
raise ValueError( # noqa: TRY003
"At least one of 'path', 'url', or 'content' must be provided."
)
assert self.content is not None # noqa: S101
return self.content

def load_content_from_path(self):
def _load_content_from_path(self):
"""Load the content of the media from the path."""
assert self.path is not None # noqa: S101
self.content = self.path.read_bytes()

def load_content_from_url(self):
def _load_content_from_url(self):
"""Load the content of the media from the URL.
Raises:
Expand Down
Loading

0 comments on commit b9db93a

Please sign in to comment.