Explorar o código

move settings management class from order listenner in Configuration manager

nico %!s(int64=8) %!d(string=hai) anos
pai
achega
782ada99c5
Modificáronse 2 ficheiros con 68 adicións e 61 borrados
  1. 65 0
      core/ConfigurationManager/ConfigurationManager.py
  2. 3 61
      core/OrderListener.py

+ 65 - 0
core/ConfigurationManager/ConfigurationManager.py

@@ -1,5 +1,18 @@
 from BrainLoader import BrainLoader
 from SettingLoader import SettingLoader
+import logging
+
+
+class DefaultSpeechToTextNotFound(Exception):
+    pass
+
+
+class DefaultSpeechNull(Exception):
+    pass
+
+
+class NoSpeechToTextConfiguration(Exception):
+    pass
 
 
 class ConfigurationManager:
@@ -18,3 +31,55 @@ class ConfigurationManager:
         if setting_file_name is None:
             setting_file_name = "settings.yml"
         return SettingLoader(setting_file_name).get_config()
+
+    @classmethod
+    def get_default_speech_to_text(cls):
+        settings = cls.get_settings()
+
+        try:
+            default_speech_to_text = settings["default_speech_to_text"]
+            if default_speech_to_text is None:
+                raise DefaultSpeechNull("Attribute default_speech_to_text is null")
+            logging.info("Default STT: %s" % default_speech_to_text)
+            return default_speech_to_text
+        except KeyError:
+            raise DefaultSpeechToTextNotFound("Attribute default_speech_to_text not found in settings")
+
+    @classmethod
+    def get_stt_args(cls, default_stt_plugin_name):
+        """
+        Return argument set for the current STT engine
+        :param default_stt_plugin_name: Name of the STT engine
+        :return:
+        """
+
+        def find(lst, key):
+            """
+            Find a key name in a list
+            :param lst: list()
+            :param key: key name to find i the list
+            :return: Return the dict
+            """
+            for el in lst:
+                try:
+                    if el[key]:
+                        return el[key]
+                except TypeError:
+                    pass
+                except KeyError:
+                    pass
+            return None
+
+        settings = cls.get_settings()
+        try:
+            speechs_to_text = settings["speech_to_text"]
+        except KeyError:
+            raise NoSpeechToTextConfiguration("No speech_to_text in settings")
+
+        logging.debug("Settings file content: %s" % speechs_to_text)
+        # get args
+        args = find(speechs_to_text, default_stt_plugin_name)
+
+        logging.debug("Args for %s STT: %s" % (default_stt_plugin_name, args))
+
+        return args

+ 3 - 61
core/OrderListener.py

@@ -2,18 +2,6 @@ import logging
 from core import ConfigurationManager
 
 
-class DefaultSpeechToTextNotFound(Exception):
-    pass
-
-
-class DefaultSpeechNull(Exception):
-    pass
-
-
-class NoSpeechToTextConfiguration(Exception):
-    pass
-
-
 class OrderListener:
 
     def __init__(self, main_controller=None):
@@ -29,60 +17,14 @@ class OrderListener:
         self.settings = ConfigurationManager().get_settings()
 
     def load_stt_plugin(self):
-        default_stt_plugin = self._get_stt_plugins()
+        default_stt_plugin = ConfigurationManager.get_default_speech_to_text()
 
-        stt_args = self._get_stt_args(default_stt_plugin)
+        stt_args = ConfigurationManager.get_stt_args(default_stt_plugin)
 
         # capitalizes the first letter (because classes have first letter upper case)
         default_stt_plugin = default_stt_plugin.capitalize()
         self._run_stt_plugin(default_stt_plugin, stt_args)
 
-    def _get_stt_plugins(self):
-        try:
-            default_speech_to_text = self.settings["default_speech_to_text"]
-            if default_speech_to_text is None:
-                raise DefaultSpeechNull("Attribute default_speech_to_text is null")
-            logging.info("Default STT: %s" % default_speech_to_text)
-            return default_speech_to_text
-        except KeyError:
-            raise DefaultSpeechToTextNotFound("Attribute default_speech_to_text not found in settings")
-
-    def _get_stt_args(self, default_stt_plugin_name):
-        """
-        Return argument set for the current STT engine
-        :param default_stt_plugin: Name of the STT engine
-        :return:
-        """
-        def find(lst, key):
-            """
-            Find a key name in a list
-            :param lst: list()
-            :param key: key name to find i the list
-            :return: Return the dict
-            """
-            for el in lst:
-                try:
-                    if el[key]:
-                        return el[key]
-                except TypeError:
-                    pass
-                except KeyError:
-                    pass
-            return None
-
-        try:
-            speechs_to_text = self.settings["speech_to_text"]
-        except KeyError:
-            raise NoSpeechToTextConfiguration("No speech_to_text in settings")
-
-        logging.debug("Settings file content: %s" % speechs_to_text)
-        # get args
-        args = find(speechs_to_text, default_stt_plugin_name)
-
-        logging.debug("Args for %s STT: %s" % (default_stt_plugin_name, args))
-
-        return args
-
     def _run_stt_plugin(self, stt_plugin, parameters=None):
         """
         Dynamic loading of a STT module
@@ -90,7 +32,7 @@ class OrderListener:
         :param parameters: Parameter of the module
         :return:
         """
-        print "Running STT %s with parameter %s" % (stt_plugin, parameters)
+        logging.debug("Running STT %s with parameter %s" % (stt_plugin, parameters))
         mod = __import__('stt', fromlist=[stt_plugin])
 
         klass = getattr(mod, stt_plugin)