Skip to content

Commit

Permalink
NavaThread exception (#53)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sepandhaghighi authored Aug 22, 2024
1 parent 07ea3ee commit c166f2b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
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
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

0 comments on commit c166f2b

Please sign in to comment.