Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavaThread exception #53

Merged
merged 9 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion nava/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
24 changes: 15 additions & 9 deletions nava/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -25,22 +26,27 @@ def __init__(self, loop, engine, *args, **kwargs):
self._play_process = None
self._loop = loop
self._engine = engine
self._nava_exception = None

def run(self):
"""
Run target function.

: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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sadrasabouri Currently, it's not straightforward to test this exception, but we will address it in a future PR. Please disregard it for this PR.

self._nava_exception = SOUND_FILE_PLAY_ERROR
raise NavaBaseError(SOUND_FILE_PLAY_ERROR)

def stop(self):
"""
Expand Down
13 changes: 12 additions & 1 deletion test/error_test.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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.
Expand Down
Loading