From c166f2bb84ca7613899dfcb800748bf748dea3a7 Mon Sep 17 00:00:00 2001 From: Sepand Haghighi Date: Fri, 23 Aug 2024 00:07:59 +0330 Subject: [PATCH] `NavaThread` exception (#53) * fix : NavaThread run method updated * fix : error_test.py updated * fix : error_test.py updated * fix : NavaThread exception section updated * fix : error_test.py updated * fix : error_test.py updated * revert error_test.py * fix : autopep8 * doc : CHANGELOG.md updated --- CHANGELOG.md | 1 + nava/functions.py | 2 +- nava/thread.py | 24 +++++++++++++++--------- test/error_test.py | 13 ++++++++++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e542d86..ec12d62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `engine` parameter added to `NavaThread` class - `README.md` modified - Test system modified +- `NavaThread` exception bug fixed - `__play_win` function renamed to `__play_winsound` - `__play_mac` function renamed to `__play_afplay` - `__play_linux` function renamed to `__play_alsa` diff --git a/nava/functions.py b/nava/functions.py index 426d4ee..026350d 100644 --- a/nava/functions.py +++ b/nava/functions.py @@ -302,7 +302,7 @@ def play(sound_path, async_mode=False, loop=False, engine=Engine.AUTO): return __play_afplay(sound_path=sound_path, async_mode=async_mode, loop=loop) elif engine == Engine.ALSA: return __play_alsa(sound_path=sound_path, async_mode=async_mode, loop=loop) - except Exception: # pragma: no cover + except Exception: raise NavaBaseError(SOUND_FILE_PLAY_ERROR) diff --git a/nava/thread.py b/nava/thread.py index f281d74..440e85d 100644 --- a/nava/thread.py +++ b/nava/thread.py @@ -2,7 +2,8 @@ """Nava thread.""" import threading -from .params import Engine +from .params import Engine, SOUND_FILE_PLAY_ERROR +from .errors import NavaBaseError class NavaThread(threading.Thread): @@ -25,6 +26,7 @@ def __init__(self, loop, engine, *args, **kwargs): self._play_process = None self._loop = loop self._engine = engine + self._nava_exception = None def run(self): """ @@ -32,15 +34,19 @@ def run(self): :return: None """ - if self._target is not None: - if self._engine == Engine.WINSOUND: - self._play_process = self._target(*self._args, **self._kwargs) - else: - while True: + try: + if self._target is not None: + if self._engine == Engine.WINSOUND: self._play_process = self._target(*self._args, **self._kwargs) - self._play_process.wait() - if not self._loop: - break + else: + while True: + self._play_process = self._target(*self._args, **self._kwargs) + self._play_process.wait() + if not self._loop: + break + except Exception: # pragma: no cover + self._nava_exception = SOUND_FILE_PLAY_ERROR + raise NavaBaseError(SOUND_FILE_PLAY_ERROR) def stop(self): """ diff --git a/test/error_test.py b/test/error_test.py index c223720..9085a96 100644 --- a/test/error_test.py +++ b/test/error_test.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- """ >>> import os ->>> from nava import play, stop +>>> import sys +>>> from nava import play, stop, Engine >>> test_sound_path = os.path.join("others", "test.wav") >>> play("test.wav") Traceback (most recent call last): @@ -23,6 +24,16 @@ Traceback (most recent call last): ... nava.errors.NavaBaseError: `engine` type must be `Engine` enum. +>>> sys_platform = sys.platform +>>> if sys_platform == "win32": +... sound_id = play(test_sound_path, async_mode=False, engine=Engine.AFPLAY) +... elif sys_platform == "darwin": +... sound_id = play(test_sound_path, async_mode=False, engine=Engine.WINSOUND) +... else: +... sound_id = play(test_sound_path, async_mode=False, engine=Engine.WINSOUND) +Traceback (most recent call last): + ... +nava.errors.NavaBaseError: Sound can not play due to some issues. >>> import nava >>> nava.functions.play_cli("test2.wav") Error: Given sound file doesn't exist.