Przeglądaj źródła

Brainloader now use class methodes

nico 8 lat temu
rodzic
commit
7f5cdc540f

+ 22 - 16
core/ConfigurationManager/BrainLoader.py

@@ -14,26 +14,28 @@ logging.basicConfig()
 logger = logging.getLogger("jarvis")
 
 
-class BrainLoader(YAMLLoader):
+class BrainLoader(object):
 
-    def __init__(self, filepath=None):
-        self.brain_file_path = filepath
-        if filepath is None:
-            self.brain_file_path = self._get_root_brain_path()
-        # self.filePath = "../../" + self.fileName
-        YAMLLoader.__init__(self, self.brain_file_path)
+    def __init__(self):
+        pass
 
-    def get_config(self):
-        return YAMLLoader.get_config(self)
+    @classmethod
+    def get_yaml_config(cls, file_path=None):
+        if file_path is None:
+            brain_file_path = cls._get_root_brain_path()
+        else:
+            brain_file_path = file_path
+        return YAMLLoader.get_config(brain_file_path)
 
-    def get_brain(self):
+    @classmethod
+    def get_brain(cls, file_path=None):
         """
         return a brain object from YAML settings
         :return: Brain object
         :rtype: Brain
         """
         # get the brain with dict
-        dict_brain = self.get_config()
+        dict_brain = cls.get_yaml_config(file_path)
         # create a new brain
         brain = Brain()
         # create list of Synapse
@@ -43,12 +45,15 @@ class BrainLoader(YAMLLoader):
             if ConfigurationChecker().check_synape_dict(synapses_dict):
                 # print "synapses_dict ok"
                 name = synapses_dict["name"]
-                neurons = self._get_neurons(synapses_dict["neurons"])
-                signals = self._get_signals(synapses_dict["signals"])
+                neurons = cls._get_neurons(synapses_dict["neurons"])
+                signals = cls._get_signals(synapses_dict["signals"])
                 new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
                 synapses.append(new_synapse)
         brain.synapses = synapses
-        brain.brain_file = self.brain_file_path
+        if file_path is None:
+            brain.brain_file = cls._get_root_brain_path()
+        else:
+            brain.brain_file = file_path
         # check that no synapse have the same name than another
         if ConfigurationChecker().check_synapes(synapses):
             return brain
@@ -81,13 +86,14 @@ class BrainLoader(YAMLLoader):
 
         return neurons
 
-    def _get_signals(self, signals_dict):
+    @classmethod
+    def _get_signals(cls, signals_dict):
         # print signals_dict
         signals = list()
         for signal_dict in signals_dict:
             if ConfigurationChecker().check_signal_dict(signal_dict):
                 # print "Signals dict ok"
-                event_or_order = self._get_event_or_order_from_dict(signal_dict)
+                event_or_order = cls._get_event_or_order_from_dict(signal_dict)
                 signals.append(event_or_order)
 
         return signals

+ 0 - 159
core/ConfigurationManager/ConfigurationManager.py

@@ -1,159 +0,0 @@
-from BrainLoader import BrainLoader
-from SettingLoader import SettingLoader
-import logging
-
-logging.basicConfig()
-logger = logging.getLogger("jarvis")
-
-
-class DefaultSpeechToTextNotFound(Exception):
-    pass
-
-
-class DefaultSpeechNull(Exception):
-    pass
-
-
-class NoSpeechToTextConfiguration(Exception):
-    pass
-
-
-class ConfigurationManager:
-
-    def __init__(self):
-        pass
-
-    @classmethod
-    def get_brain(cls, brain_file_name=None):
-        if brain_file_name is None:
-            brain_file_name = "brain.yml"
-        return BrainLoader(brain_file_name).get_config()
-
-    @classmethod
-    def get_settings(cls, setting_file_name=None):
-        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")
-            logger.debug("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_default_text_to_speech(cls):
-        settings = cls.get_settings()
-
-        try:
-            default_text_to_speech = settings["default_text_to_speech"]
-            if default_text_to_speech is None:
-                raise DefaultSpeechNull("Attribute default_text_to_speech is null")
-            logger.debug("Default TTS: %s" % default_text_to_speech)
-            return default_text_to_speech
-        except KeyError:
-            raise DefaultSpeechToTextNotFound("Attribute default_text_to_speech 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")
-
-        logger.debug("Settings file content: %s" % speechs_to_text)
-        # get args
-        args = find(speechs_to_text, default_stt_plugin_name)
-
-        logger.debug("Args for %s STT: %s" % (default_stt_plugin_name, args))
-
-        return args
-
-    @classmethod
-    def get_tts_args(cls, tts_name):
-        """
-        Return argument set for the current STT engine
-        :param tts_name: Name of the TTS 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:
-            texts_to_speech = settings["text_to_speech"]
-        except KeyError:
-            raise NoSpeechToTextConfiguration("No text_to_speech in settings")
-
-        logger.debug("Settings file content: %s" % texts_to_speech)
-        # get args
-        args = find(texts_to_speech, tts_name)
-        logger.debug("Args for %s TTS: %s" % (tts_name, args))
-        # print args
-        return args
-
-    @classmethod
-    def get_tts_list(cls):
-        settings = cls.get_settings()
-        try:
-            texts_to_speech = settings["text_to_speech"]
-        except KeyError:
-            raise NoSpeechToTextConfiguration("No text_to_speech in settings")
-
-        return texts_to_speech
-
-    @classmethod
-    def get_stt_list(cls):
-        settings = cls.get_settings()
-        try:
-            speech_to_text = settings["speech_to_text"]
-        except KeyError:
-            raise NoSpeechToTextConfiguration("No speech_to_text in settings")
-
-        return speech_to_text

+ 5 - 6
core/ConfigurationManager/SettingLoader.py

@@ -26,19 +26,18 @@ class SettingLoader(object):
         pass
 
     @classmethod
-    def get_yaml_config(cls, file_name=None):
-        if file_name is None:
-            file_name = FILE_NAME
-        file_path = "../../" + file_name
+    def get_yaml_config(cls, file_path=None):
+        if file_path is None:
+            file_path = "../../" + FILE_NAME
         return YAMLLoader.get_config(file_path)
 
     @classmethod
-    def get_settings(cls, file_name=None):
+    def get_settings(cls, file_path=None):
         """
         Return a Settings object from settings.yml file
         :return:
         """
-        settings = cls.get_yaml_config(file_name)
+        settings = cls.get_yaml_config(file_path)
         default_stt_name = cls._get_default_speech_to_text(settings)
         default_tts_name = cls._get_default_text_to_speech(settings)
         default_trigger_name = cls._get_default_trigger(settings)

+ 1 - 1
core/CrontabManager.py

@@ -21,7 +21,7 @@ class CrontabManager:
 
     def __init__(self, brain_file=None):
         self.my_user_cron = CronTab(user=True)
-        self.brain = BrainLoader(filepath=brain_file).get_brain()
+        self.brain = BrainLoader.get_brain(file_path=brain_file)
         self.base_command = self._get_base_command()
 
     def load_events_in_crontab(self):

+ 2 - 2
core/OrderAnalyser.py

@@ -21,9 +21,9 @@ class OrderAnalyser:
         self.main_controller = main_controller
         self.order = order
         if brain_file is None:
-            self.brain = BrainLoader().get_brain()
+            self.brain = BrainLoader.get_brain()
         else:
-            self.brain = BrainLoader(brain_file).get_brain()
+            self.brain = BrainLoader.get_brain(file_path=brain_file)
             logger.debug("Receiver order: %s" % self.order)
 
     def start(self):

+ 2 - 2
core/SynapseLauncher.py

@@ -21,9 +21,9 @@ class SynapseLauncher(object):
         synapse_name_launch = name
         # get the brain
         if brain_file is None:
-            brain = BrainLoader().get_brain()
+            brain = BrainLoader.get_brain()
         else:
-            brain = BrainLoader(brain_file).get_brain()
+            brain = BrainLoader.get_brain(file_path=brain_file)
 
         # check if we have found and launched the synapse
         synapse_launched = False