瀏覽代碼

move catch execption in Filemanager

nico 8 年之前
父節點
當前提交
f73c451be2
共有 2 個文件被更改,包括 16 次插入13 次删除
  1. 12 4
      core/FileManager.py
  2. 4 9
      tts/voxygen/voxygen.py

+ 12 - 4
core/FileManager.py

@@ -1,7 +1,12 @@
+import logging
 import os
 import shutil
 
 
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+
 class FileManager:
     def __init__(self):
         pass
@@ -13,10 +18,13 @@ class FileManager:
 
     @staticmethod
     def write_in_file(file_path, content):
-        with open(file_path, "wb") as file_open:
-            file_open.write(content)
-            file_open.close()
-        return not FileManager.file_is_empty(file_path)
+        try:
+            with open(file_path, "wb") as file_open:
+                file_open.write(content)
+                file_open.close()
+            return not FileManager.file_is_empty(file_path)
+        except IOError as e:
+            logger.error("I/O error(%s): %s", e.errno, e.strerror)
 
     @staticmethod
     def wipe_cache(cache_path):

+ 4 - 9
tts/voxygen/voxygen.py

@@ -37,15 +37,10 @@ class Voxygen(TTSModule):
                      r.status_code,
                      content_type)
 
-        try:
-            if r.status_code == requests.codes.ok and content_type == TTS_CONTENT_TYPE:
-                return FileManager.write_in_file(self.file_path, r.content)
-            else:
-                return False
-        except IOError as e:
-            logger.error("I/O error(%s): %s", e.errno, e.strerror)
-        except ValueError:
-            logger.error("Could not convert data to an integer.")
+        if r.status_code == requests.codes.ok and content_type == TTS_CONTENT_TYPE:
+            FileManager.write_in_file(self.file_path, r.content)
+        else:
+            logger.debug("Unable to get a valid audio file. Returned code: %s" % r.status_code)
 
     @staticmethod
     def get_payload(voice, words):