changeset 290:2878d2f83282

Refactor workers.py - Move exception classes to exceptions.py - Use metaclass=ABCMeta in Worker declaration - Remove Python 2 compatibility code
author Amine Sehili <amine.sehili@gmail.com>
date Sat, 05 Oct 2019 14:54:14 +0200
parents 69ca1c64a9b0
children cd9897f4519f
files auditok/exceptions.py auditok/workers.py
diffstat 2 files changed, 17 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/exceptions.py	Sat Oct 05 14:26:03 2019 +0200
+++ b/auditok/exceptions.py	Sat Oct 05 14:54:14 2019 +0200
@@ -12,4 +12,13 @@
 
 
 class TimeFormatError(Exception):
-    pass
\ No newline at end of file
+    """Raised when duration formatting directicve is wrong"""
+
+
+class EndOfProcessing(Exception):
+    """Raised within command line script's main function to jump to 
+    postprocessing code"""
+
+
+class AudioEncodingError(Exception):
+    """Raised if audio data can not be encoded in the provided format"""
--- a/auditok/workers.py	Sat Oct 05 14:26:03 2019 +0200
+++ b/auditok/workers.py	Sat Oct 05 14:54:14 2019 +0200
@@ -1,4 +1,3 @@
-from __future__ import print_function
 import os
 import sys
 from tempfile import NamedTemporaryFile
@@ -8,33 +7,18 @@
 from collections import namedtuple
 import wave
 import subprocess
+from queue import Queue, Empty
 from .io import _guess_audio_format
 from .util import AudioDataSource
-
-try:
-    import future
-    from queue import Queue, Empty
-except ImportError:
-    if sys.version_info >= (3, 0):
-        from queue import Queue, Empty
-    else:
-        from Queue import Queue, Empty
-
 from .core import split
 from . import cmdline_util
+from .exceptions import EndOfProcessing, AudioEncodingError
+
 
 _STOP_PROCESSING = "STOP_PROCESSING"
 _Detection = namedtuple("_Detection", "id start end duration")
 
 
-class EndOfProcessing(Exception):
-    pass
-
-
-class AudioEncodingError(Exception):
-    pass
-
-
 def _run_subprocess(command):
     try:
         with subprocess.Popen(
@@ -50,7 +34,7 @@
         raise AudioEncodingError(err_msg)
 
 
-class Worker(Thread):
+class Worker(Thread, metaclass=ABCMeta):
     def __init__(self, timeout=0.5, logger=None):
         self._timeout = timeout
         self._logger = logger
@@ -110,6 +94,9 @@
         self._log_format += "end: {0.end:.3f}, duration: {0.duration:.3f})"
         Worker.__init__(self, timeout=0.2, logger=logger)
 
+    def _process_message(self):
+        pass
+
     @property
     def detections(self):
         return self._detections