Browse Source

[Refactor] Introduce Options model object

ThiBuff 7 years ago
parent
commit
287345a8cf

+ 1 - 1
Tests/test_brain_loader.py

@@ -8,7 +8,7 @@ from kalliope.core.ConfigurationManager import BrainLoader
 from kalliope.core.Models import Neuron
 from kalliope.core.Models import Synapse
 from kalliope.core.Models.Brain import Brain
-from kalliope.core.Models.Settings import Settings
+from kalliope.core.Models.settings.Settings import Settings
 
 
 class TestBrainLoader(unittest.TestCase):

+ 0 - 1
Tests/test_hook_manager.py

@@ -9,7 +9,6 @@ from kalliope.core.Models import Singleton
 from kalliope.core.ConfigurationManager import SettingLoader
 
 from kalliope.core import HookManager
-from kalliope.core.Models.Settings import Settings
 
 
 class TestInit(unittest.TestCase):

+ 7 - 7
Tests/test_models.py

@@ -2,21 +2,21 @@ import unittest
 import ast
 import mock
 
-from kalliope.core.Models.Player import Player
+from kalliope.core.Models.settings.Player import Player
 from kalliope.core.Models.Signal import Signal
-from kalliope.core.Models.RecognitionOptions import RecognitionOptions
-from kalliope.core.Models.Tts import Tts
+from kalliope.core.Models.settings.RecognitionOptions import RecognitionOptions
+from kalliope.core.Models.settings.Tts import Tts
 
-from kalliope.core.Models.Trigger import Trigger
+from kalliope.core.Models.settings.Trigger import Trigger
 
-from kalliope.core.Models.Stt import Stt
+from kalliope.core.Models.settings.Stt import Stt
 
-from kalliope.core.Models.RestAPI import RestAPI
+from kalliope.core.Models.settings.RestAPI import RestAPI
 
 from kalliope.core.Models.Dna import Dna
 
 from kalliope.core import LIFOBuffer
-from kalliope.core.Models.Settings import Settings
+from kalliope.core.Models.settings.Settings import Settings
 
 from kalliope.core.Models import Neuron, Synapse, Brain, Resources, Singleton
 

+ 1 - 1
Tests/test_neuron_launcher.py

@@ -4,7 +4,7 @@ import unittest
 import mock
 from kalliope.core.Models import Singleton
 
-from kalliope.core.Models.Resources import Resources
+from kalliope.core.Models.settings.Resources import Resources
 from kalliope.core.NeuronLauncher import NeuronLauncher, NeuronParameterNotAvailable
 from kalliope.core.ConfigurationManager import SettingLoader
 

+ 1 - 1
Tests/test_neuron_module.py

@@ -3,7 +3,7 @@ import unittest
 import mock
 
 from kalliope.core.Models import Singleton
-from kalliope.core.Models.Tts import Tts
+from kalliope.core.Models.settings.Tts import Tts
 
 from kalliope import SettingLoader
 from kalliope.core.NeuronModule import NeuronModule, TemplateFileNotFoundException, TTSModuleNotFound

+ 1 - 1
Tests/test_order_listener.py

@@ -4,7 +4,7 @@ from mock import mock
 
 from kalliope.core import OrderListener
 from kalliope.core.Models import Resources
-from kalliope.core.Models.Stt import Stt
+from kalliope.core.Models.settings.Stt import Stt
 
 
 class TestOrderListener(unittest.TestCase):

+ 2 - 2
Tests/test_player_launcher.py

@@ -1,8 +1,8 @@
 import unittest
 import mock
 
-from kalliope.core.Models.Player import Player
-from kalliope.core.Models.Settings import Settings
+from kalliope.core.Models.settings.Player import Player
+from kalliope.core.Models.settings.Settings import Settings
 from kalliope.core.PlayerLauncher import PlayerLauncher
 
 

+ 10 - 15
Tests/test_settings_loader.py

@@ -7,13 +7,14 @@ import unittest
 from kalliope.core.ConfigurationManager import SettingLoader
 from kalliope.core.Models import Singleton
 from kalliope.core.Models import Resources
-from kalliope.core.Models.Player import Player
-from kalliope.core.Models.RestAPI import RestAPI
-from kalliope.core.Models.Settings import Settings
-from kalliope.core.Models.Stt import Stt
-from kalliope.core.Models.RecognitionOptions import RecognitionOptions
-from kalliope.core.Models.Trigger import Trigger
-from kalliope.core.Models.Tts import Tts
+from kalliope.core.Models.settings.Options import Options
+from kalliope.core.Models.settings.Player import Player
+from kalliope.core.Models.settings.RestAPI import RestAPI
+from kalliope.core.Models.settings.Settings import Settings
+from kalliope.core.Models.settings.Stt import Stt
+from kalliope.core.Models.settings.RecognitionOptions import RecognitionOptions
+from kalliope.core.Models.settings.Trigger import Trigger
+from kalliope.core.Models.settings.Tts import Tts
 
 
 class TestSettingLoader(unittest.TestCase):
@@ -125,10 +126,7 @@ class TestSettingLoader(unittest.TestCase):
             "test_number": 60,
             "test": "kalliope"
         }
-        settings_object.options = {
-            "deaf": True,
-            "mute": False
-        }
+        settings_object.options = Options(deaf=True, mute=False)
         settings_object.machine = platform.machine()
         settings_object.recognition_options = RecognitionOptions()
         settings_object.hooks = {'on_waiting_for_trigger': 'test',
@@ -227,10 +225,7 @@ class TestSettingLoader(unittest.TestCase):
                          sl._get_variables(self.settings_dict))
 
     def test_get_options(self):
-        expected_result = {
-            "deaf": True,
-            "mute": False
-        }
+        expected_result = Options(deaf=True, mute=False)
         sl = SettingLoader(file_path=self.settings_file_to_test)
         self.assertEqual(expected_result,
                          sl._get_options(self.settings_dict))

+ 2 - 2
Tests/test_synapse_launcher.py

@@ -2,10 +2,10 @@ import unittest
 
 import mock
 
-from kalliope.core import LIFOBuffer, LifoManager
+from kalliope.core import LifoManager
 from kalliope.core.Models import Brain, Signal, Singleton
 from kalliope.core.Models.MatchedSynapse import MatchedSynapse
-from kalliope.core.Models.Settings import Settings
+from kalliope.core.Models.settings.Settings import Settings
 from kalliope.core.SynapseLauncher import SynapseLauncher, SynapseNameNotFound
 
 from kalliope.core.Models import Neuron

+ 2 - 2
Tests/test_trigger_launcher.py

@@ -1,10 +1,10 @@
 import unittest
 import mock
 
-from kalliope.core.Models.Settings import Settings
+from kalliope.core.Models.settings.Settings import Settings
 from kalliope.core.TriggerLauncher import TriggerLauncher
 
-from kalliope.core.Models.Trigger import Trigger
+from kalliope.core.Models.settings.Trigger import Trigger
 
 
 class TestTriggerLauncher(unittest.TestCase):

+ 1 - 1
Tests/test_tts_module.py

@@ -3,7 +3,7 @@ import unittest
 
 import mock
 
-from kalliope.core.Models.Settings import Settings
+from kalliope.core.Models.settings.Settings import Settings
 from kalliope.core.TTS.TTSModule import TTSModule, TtsGenerateAudioFunctionNotFound
 from kalliope.core.Utils.FileManager import FileManager
 

+ 1 - 1
kalliope/__init__.py

@@ -157,7 +157,7 @@ def main():
         if (parser.run_synapse is None) and (parser.run_order is None):
             # if --deaf
             if parser.deaf:
-                settings.options['deaf'] = True
+                settings.options.deaf = True
 
             # start rest api
             start_rest_api(settings, brain)

+ 2 - 2
kalliope/core/ConfigurationManager/SettingEditor.py

@@ -25,7 +25,7 @@ class SettingEditor(object):
         else:
             Utils.print_info("Kalliope now speaking.")
             HookManager.on_unmute()
-        settings.options["mute"] = mute
+        settings.options.mute = mute
 
     @staticmethod
     def set_deaf_status(trigger_instance, deaf=False):
@@ -44,7 +44,7 @@ class SettingEditor(object):
             trigger_instance.unpause()
             Utils.print_info("Kalliope now listening for trigger detection")
             HookManager.on_undeaf()
-        settings.options["deaf"] = deaf
+        settings.options.deaf = deaf
 
     @staticmethod
     def set_default_player_name(default_player_name):

+ 19 - 18
kalliope/core/ConfigurationManager/SettingLoader.py

@@ -2,17 +2,18 @@ import logging
 import os
 from six import with_metaclass
 
-from kalliope.core.Models.RecognitionOptions import RecognitionOptions
+from kalliope.core.Models.settings.Options import Options
+from kalliope.core.Models.settings.RecognitionOptions import RecognitionOptions
 from .YAMLLoader import YAMLLoader
-from kalliope.core.Models.Resources import Resources
+from kalliope.core.Models.settings.Resources import Resources
 from kalliope.core.Utils.Utils import Utils
 from kalliope.core.Models import Singleton
-from kalliope.core.Models.RestAPI import RestAPI
-from kalliope.core.Models.Settings import Settings
-from kalliope.core.Models.Stt import Stt
-from kalliope.core.Models.Trigger import Trigger
-from kalliope.core.Models.Player import Player
-from kalliope.core.Models.Tts import Tts
+from kalliope.core.Models.settings.RestAPI import RestAPI
+from kalliope.core.Models.settings.Settings import Settings
+from kalliope.core.Models.settings.Stt import Stt
+from kalliope.core.Models.settings.Trigger import Trigger
+from kalliope.core.Models.settings.Player import Player
+from kalliope.core.Models.settings.Tts import Tts
 from kalliope.core.Utils.FileManager import FileManager
 
 FILE_NAME = "settings.yml"
@@ -634,12 +635,15 @@ class SettingLoader(with_metaclass(Singleton, object)):
     @staticmethod
     def _get_options(settings):
         """
-        Return the start options settings
+        Return the Options settings
+        if not set, default values are :
+        deaf: False
+        mute: False
 
         :param settings: The YAML settings file
         :type settings: dict
-        :return: A dict containing the start options
-        :rtype: dict
+        :return: An Options with the start options
+        :rtype: Options
         """
 
         deaf = False
@@ -647,19 +651,16 @@ class SettingLoader(with_metaclass(Singleton, object)):
 
         try:
             options = settings["options"]
-        except KeyError:
-            options = dict()
-
-        if options is not None:
             if options['deaf']:
                 deaf = options['deaf']
             if options['mute']:
                 mute = options['mute']
+        except KeyError:
+            pass
 
-        options['deaf'] = deaf
-        options['mute'] = mute
+        options = Options(deaf=deaf, mute=mute)
 
-        logger.debug("Start options: %s" % options)
+        logger.debug("Options: %s" % options)
         return options
 
     @staticmethod

+ 1 - 1
kalliope/core/Models/__init__.py

@@ -1,5 +1,5 @@
 from .Singleton import Singleton
-from .Resources import Resources
+from kalliope.core.Models.settings.Resources import Resources
 from .Brain import Brain
 from .Synapse import Synapse
 from .Neuron import Neuron

+ 32 - 0
kalliope/core/Models/settings/Options.py

@@ -0,0 +1,32 @@
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
+
+
+class Options(SettingsEntry):
+    """
+    This Class is representing an Option element with parameters and values
+
+    .. note:: must be defined in the settings.yml
+    """
+
+    def __init__(self, deaf=None, mute=None):
+        super(Options, self).__init__(name="Options")
+        self.deaf = deaf
+        self.mute = mute
+
+    def __str__(self):
+        return str(self.serialize())
+
+    def serialize(self):
+        return {
+            'name': self.name,
+            'deaf': self.deaf,
+            'mute': self.mute
+        }
+
+    def __eq__(self, other):
+        """
+        This is used to compare 2 objects
+        :param other: the Options to compare
+        :return: True if both Options are similar, False otherwise
+        """
+        return self.__dict__ == other.__dict__

+ 1 - 1
kalliope/core/Models/Player.py → kalliope/core/Models/settings/Player.py

@@ -1,4 +1,4 @@
-from kalliope.core.Models.SettingsEntry import SettingsEntry
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
 
 
 class Player(SettingsEntry):

+ 5 - 1
kalliope/core/Models/RecognitionOptions.py → kalliope/core/Models/settings/RecognitionOptions.py

@@ -1,4 +1,7 @@
-class RecognitionOptions(object):
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
+
+
+class RecognitionOptions(SettingsEntry):
     """
     This Class is representing a Speech To Text (STT) Recognition elements with name and parameters
 
@@ -6,6 +9,7 @@ class RecognitionOptions(object):
     """
 
     def __init__(self, energy_threshold=4000, adjust_for_ambient_noise_second=0):
+        super(RecognitionOptions, self).__init__("RecognitionOptions")
         self.energy_threshold = energy_threshold
         self.adjust_for_ambient_noise_second = adjust_for_ambient_noise_second
 

+ 3 - 1
kalliope/core/Models/Resources.py → kalliope/core/Models/settings/Resources.py

@@ -1,10 +1,12 @@
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
 
 
-class Resources(object):
+class Resources(SettingsEntry):
     """
 
     """
     def __init__(self, neuron_folder=None, stt_folder=None, tts_folder=None, trigger_folder=None, signal_folder=None):
+        super(Resources, self).__init__("Resources")
         self.neuron_folder = neuron_folder
         self.stt_folder = stt_folder
         self.tts_folder = tts_folder

+ 5 - 1
kalliope/core/Models/RestAPI.py → kalliope/core/Models/settings/RestAPI.py

@@ -1,4 +1,7 @@
-class RestAPI(object):
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
+
+
+class RestAPI(SettingsEntry):
     """
     This Class is representing the rest API with all its configuration.
     """
@@ -16,6 +19,7 @@ class RestAPI(object):
         :param active: specify if the rest api is loaded on start with Kalliope
         :param allowed_cors_origin: specify allowed origins
         """
+        super(RestAPI, self).__init__("RestAPI")
         self.password_protected = password_protected
         self.login = login
         self.password = password

+ 0 - 0
kalliope/core/Models/Settings.py → kalliope/core/Models/settings/Settings.py


+ 0 - 0
kalliope/core/Models/SettingsEntry.py → kalliope/core/Models/settings/SettingsEntry.py


+ 1 - 1
kalliope/core/Models/Stt.py → kalliope/core/Models/settings/Stt.py

@@ -1,4 +1,4 @@
-from kalliope.core.Models.SettingsEntry import SettingsEntry
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
 
 
 class Stt(SettingsEntry):

+ 1 - 1
kalliope/core/Models/Trigger.py → kalliope/core/Models/settings/Trigger.py

@@ -1,4 +1,4 @@
-from kalliope.core.Models.SettingsEntry import SettingsEntry
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
 
 
 class Trigger(SettingsEntry):

+ 1 - 1
kalliope/core/Models/Tts.py → kalliope/core/Models/settings/Tts.py

@@ -1,4 +1,4 @@
-from kalliope.core.Models.SettingsEntry import SettingsEntry
+from kalliope.core.Models.settings.SettingsEntry import SettingsEntry
 
 
 class Tts(SettingsEntry):

+ 0 - 0
kalliope/core/Models/settings/__init__.py


+ 1 - 1
kalliope/core/NeuronModule.py

@@ -170,7 +170,7 @@ class NeuronModule(object):
             Cortex.save("kalliope_last_tts_message", tts_message)
 
             # process the audio only if the mute flag is false
-            if self.settings.options["mute"]:
+            if self.settings.options.mute:
                 logger.debug("[NeuronModule] mute is True, Kalliope is muted")
             else:
                 logger.debug("[NeuronModule] mute is False, make Kalliope speaking")

+ 9 - 9
kalliope/core/RestAPI/FlaskAPI.py

@@ -159,7 +159,7 @@ class FlaskAPI(threading.Thread):
         synapse_target = BrainLoader().brain.get_synapse_by_name(synapse_name=synapse_name)
 
         # Store the mute value, then apply depending of the request parameters
-        old_mute_value = self.settings.options["mute"]
+        old_mute_value = self.settings.options.mute
         mute = self.get_boolean_flag_from_request(request, boolean_flag_to_find="mute")
         if mute is not None:
             SettingEditor.set_mute_status(mute=mute)
@@ -212,7 +212,7 @@ class FlaskAPI(threading.Thread):
         order = request.get_json('order')
 
         # Store the mute value, then apply depending of the request parameters
-        old_mute_value = self.settings.options["mute"]
+        old_mute_value = self.settings.options.mute
         mute = self.get_boolean_flag_from_request(request, boolean_flag_to_find="mute")
         if mute is not None:
             SettingEditor.set_mute_status(mute=mute)
@@ -267,7 +267,7 @@ class FlaskAPI(threading.Thread):
             return jsonify(error=data), 400
 
         # Store the mute value, then apply depending of the request parameters
-        old_mute_value = self.settings.options["mute"]
+        old_mute_value = self.settings.options.mute
         if request.form.get("mute"):
             SettingEditor.set_mute_status(mute=self.str_to_bool(request.form.get("mute")))
 
@@ -339,9 +339,9 @@ class FlaskAPI(threading.Thread):
         """
 
         # find the order signal and call the deaf settings
-        if self.settings.options["deaf"] is not None:
+        if self.settings.options.deaf is not None:
             data = {
-                "deaf": self.settings.options["deaf"]
+                "deaf": self.settings.options.deaf
             }
             return jsonify(data), 200
 
@@ -368,10 +368,10 @@ class FlaskAPI(threading.Thread):
         deaf = self.get_boolean_flag_from_request(request, boolean_flag_to_find="deaf")
 
         signal_order = SignalLauncher.get_order_instance()
-        if signal_order is not None and deaf is not None and self.settings.options["deaf"] is not None:
+        if signal_order is not None and deaf is not None and self.settings.options.deaf is not None:
             SettingEditor.set_deaf_status(signal_order.trigger_instance, deaf)
             data = {
-                "deaf": self.settings.options["deaf"]
+                "deaf": self.settings.options.deaf
             }
             return jsonify(data), 200
 
@@ -390,9 +390,9 @@ class FlaskAPI(threading.Thread):
         """
 
         # find the order signal and call the mute settings
-        if self.settings.options["mute"] is not None:
+        if self.settings.options.mute is not None:
             data = {
-                "mute": self.settings.options["mute"]
+                "mute": self.settings.options.mute
             }
             return jsonify(data), 200
 

+ 1 - 1
kalliope/signals/order/order.py

@@ -96,7 +96,7 @@ class Order(Thread):
         Method to print in debug that the main process is waiting for a trigger detection
         """
         logger.debug("[MainController] Entering state: %s" % self.state)
-        if self.settings.options["deaf"]:  # the user asked to deaf inside the deaf neuron
+        if self.settings.options.deaf:  # the user asked to deaf inside the deaf neuron
             Utils.print_info("Kalliope is deaf")
             self.trigger_instance.pause()
         else: