コード例 #1
0
    def test_load_stt_plugin(self):

        # Test getting default stt
        ol = OrderListener()

        stt1 = Stt(name="default-stt", parameters=dict())

        stt2 = Stt(name="second-stt", parameters=dict())

        stt3 = Stt(name="third-stt", parameters=dict())

        resources = Resources(stt_folder="/tmp")
        ol.settings = mock.MagicMock(default_stt_name="default-stt",
                                     stts=[stt1, stt2, stt3],
                                     resources=resources)

        callback = mock.MagicMock()

        ol.callback = callback

        with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation"
                        ) as mock_get_dynamic_class_instantiation:
            mock_get_dynamic_class_instantiation.return_value = 'class_instance'
            self.assertEqual(ol.load_stt_plugin(), "class_instance",
                             "Fail getting the proper value")

            mock_get_dynamic_class_instantiation.assert_called_once_with(
                package_name="stt",
                module_name="Default-stt",
                parameters={
                    'callback': callback,
                    'audio_file_path': None
                },
                resources_dir="/tmp")
コード例 #2
0
ファイル: test_models.py プロジェクト: igorstarki/kalliope
    def test_Stt(self):
        stt1 = Stt(name="stt1", parameters={"key1": "val1"})
        stt2 = Stt(name="stt2", parameters={"key2": "val2"})
        stt3 = Stt(name="stt1", parameters={"key1": "val1"})

        expected_result_serialize = {'name': 'stt1', 'parameters': {'key1': 'val1'}}

        self.assertDictEqual(expected_result_serialize, stt1.serialize())

        self.assertTrue(stt1.__eq__(stt3))
        self.assertFalse(stt1.__eq__(stt2))
コード例 #3
0
 def test_set_stts(self):
     new_stt = Stt(name="totoStt", parameters={})
     with mock.patch("kalliope.core.ConfigurationManager.SettingLoader"
                     ) as mock_setting_loader:
         mock_setting_loader.return_value(self.sl)
         SettingEditor.set_stts(new_stt)
         self.assertIn(new_stt, self.sl.settings.stts)
コード例 #4
0
    def _get_stts(settings):
        """
        Return a list of stt object

        :param settings: The YAML settings file
        :type settings: dict
        :return: List of Stt
        :rtype: list

        :Example:

            stts = cls._get_stts(settings)

        .. seealso:: Stt
        .. raises:: SettingNotFound
        .. warnings:: Static Method and Private
        """

        try:
            speechs_to_text_list = settings["speech_to_text"]
        except KeyError:
            raise SettingNotFound("speech_to_text settings not found")

        stts = list()
        for speechs_to_text_el in speechs_to_text_list:
            if isinstance(speechs_to_text_el, dict):
                for stt_name in speechs_to_text_el:
                    name = stt_name
                    parameters = speechs_to_text_el[name]
                    new_stt = Stt(name=name, parameters=parameters)
                    stts.append(new_stt)
            else:
                # the stt does not have parameter
                new_stt = Stt(name=speechs_to_text_el, parameters=dict())
                stts.append(new_stt)
        return stts
コード例 #5
0
    def test_Stt(self):
        stt1 = Stt(name="stt1", parameters={"key1": "val1"})
        stt2 = Stt(name="stt2", parameters={"key2": "val2"})
        stt3 = Stt(name="stt1", parameters={"key1": "val1"})

        expected_result_serialize = {
            'name': 'stt1',
            'parameters': {
                'key1': 'val1'
            }
        }

        self.assertDictEqual(expected_result_serialize, stt1.serialize())

        self.assertTrue(stt1.__eq__(stt3))
        self.assertFalse(stt1.__eq__(stt2))
コード例 #6
0
 def test_get_stts(self):
     stt = Stt(name="google", parameters={'language': 'fr-FR'})
     sl = SettingLoader(file_path=self.settings_file_to_test)
     self.assertEqual([stt], sl._get_stts(self.settings_dict))
コード例 #7
0
    def test_get_settings(self):
        settings_object = Settings()
        settings_object.default_tts_name = "pico2wave"
        settings_object.default_stt_name = "google"
        settings_object.default_trigger_name = "snowboy"
        settings_object.default_player_name = "mplayer"
        tts1 = Tts(name="pico2wave",
                   parameters={
                       'cache': True,
                       'language': 'fr-FR'
                   })
        tts2 = Tts(name="googletts",
                   parameters={
                       'language': 'fr',
                       'cache': True
                   })
        settings_object.ttss = [tts1, tts2]
        stt = Stt(name="google", parameters={'language': 'fr-FR'})
        settings_object.stts = [stt]
        trigger1 = Trigger(
            name="snowboy",
            parameters={
                'pmdl_file':
                'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'
            })
        settings_object.triggers = [trigger1]
        player1 = Player(name="mplayer", parameters={})
        player2 = Player(name="pyalsaaudio", parameters={"device": "default"})
        settings_object.players = [player1, player2]
        settings_object.rest_api = RestAPI(password_protected=True,
                                           active=True,
                                           login="******",
                                           password="******",
                                           port=5000,
                                           allowed_cors_origin=False)
        settings_object.cache_path = '/tmp/kalliope_tts_cache'
        resources = Resources(
            neuron_folder="/tmp/kalliope/tests/kalliope_resources_dir/neurons",
            stt_folder="/tmp/kalliope/tests/kalliope_resources_dir/stt",
            tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
            trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger"
        )
        settings_object.resources = resources
        settings_object.variables = {
            "author": "Lamonf",
            "test_number": 60,
            "test": "kalliope"
        }
        settings_object.options = Options(energy_threshold=3000,
                                          deaf=True,
                                          mute=False)
        settings_object.machine = platform.machine()
        settings_object.hooks = {
            'on_waiting_for_trigger': 'test',
            'on_stop_listening': None,
            'on_start_listening': None,
            'on_order_found': None,
            'on_start': ['on-start-synapse', 'bring-led-on'],
            'on_undeaf': [],
            'on_triggered': ['on-triggered-synapse'],
            'on_deaf': [],
            'on_mute': [],
            'on_unmute': [],
            'on_order_not_found': ['order-not-found-synapse'],
            'on_processed_synapses': None,
            'on_start_speaking': None,
            'on_stop_speaking': None,
        }

        sl = SettingLoader(file_path=self.settings_file_to_test)

        self.assertEqual(settings_object, sl.settings)
コード例 #8
0
    def test_set_settings(self):
        # tts
        self.neuron_settings.default_tts = "randomtts"
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_default_tts") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(self.neuron_settings.default_tts)

        self.neuron_settings.text_to_speech = [{"randomTTS": {"language": "fr-FR"}}]
        tts = Tts(name= "randomTTS", parameters= {"language": "fr-FR"})
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_ttss") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(tts)

        # stt
        self.neuron_settings.default_stt = "randomstt"
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_default_stt") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(self.neuron_settings.default_stt)

        self.neuron_settings.speech_to_text = [{"randomStt": {"language": "fr-FR"}}]
        stt = Stt(name="randomStt", parameters={"language": "fr-FR"})
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_stts") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(stt)

        # players
        self.neuron_settings.default_player = "randomPlayer"
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_default_player") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(self.neuron_settings.default_player)

        self.neuron_settings.players = [{"randomPlayer": {}}]
        player = Player(name="randomPlayer", parameters={})
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_players") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(player)

        # triggers
        self.neuron_settings.default_trigger = "randomTrigger"
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_default_trigger") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(self.neuron_settings.default_trigger)

        self.neuron_settings.triggers = [{"randomTrigger": {}}]
        trigger = Trigger(name="randomTrigger", parameters={})
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_trigger") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(trigger)

        # Hooks
        self.neuron_settings.hooks = {"randomHook": "randomSynapse"}
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_hooks") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(self.neuron_settings.hooks)

        # Variables
        with tempfile.NamedTemporaryFile() as tmpfile:
            tmpfile.write("coucou: 'hello'".encode()) # encode to get the binary format
            tmpfile.flush() # To refresh the file with the data
            self.neuron_settings.var_files = [tmpfile.name]
            with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_variables") as mock_setting_editor:
                self.neuron_settings._set_settings()
                mock_setting_editor.assert_called_once_with({'coucou': 'hello'})
                self.neuron_settings.var_files = [] # reset var_file

        # Deaf
        self.neuron_settings.deaf = True
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_deaf_status") as mock_setting_editor:
            with mock.patch("kalliope.core.SignalLauncher.SignalLauncher.get_order_instance"):
                self.neuron_settings._set_settings()
                mock_setting_editor.assert_called_once()

        # Mute
        self.neuron_settings.mute = True
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_mute_status") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(True)

        # set_recognizer_multiplier
        self.neuron_settings.recognizer_multiplier = 50
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_recognizer_multiplier") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(50)

        # set_recognizer_energy_ratio
        self.neuron_settings.recognizer_energy_ratio = 50
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_recognizer_energy_ratio") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(50)

        # set_recognizer_recording_timeout
        self.neuron_settings.recognizer_recording_timeout = 50
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_recognizer_recording_timeout") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(50)

        # set_recognizer_recording_timeout_with_silence
        self.neuron_settings.recognizer_recording_timeout_with_silence = 50
        with mock.patch("kalliope.core.ConfigurationManager.SettingEditor.set_recognizer_recording_timeout_with_silence") as mock_setting_editor:
            self.neuron_settings._set_settings()
            mock_setting_editor.assert_called_once_with(50)
コード例 #9
0
    def test_Settings(self):
        with mock.patch('platform.machine', return_value='pumpkins'):
            rest_api1 = RestAPI(password_protected=True,
                                login="******",
                                password="******",
                                active=True,
                                port=5000,
                                allowed_cors_origin="*")

            tts1 = Tts(name="tts1", parameters=dict())
            tts2 = Tts(name="tts2", parameters=dict())
            stt1 = Stt(name="stt1", parameters=dict())
            stt2 = Stt(name="stt2", parameters=dict())
            trigger1 = Trigger(name="snowboy", parameters=dict())
            player = Player(name="player1")
            resources = Resources()
            options = Options()

            setting1 = Settings(default_tts_name="pico2wav",
                                default_stt_name="google",
                                default_trigger_name="swoyboy",
                                default_player_name="mplayer",
                                ttss=[tts1],
                                stts=[stt1],
                                triggers=[trigger1],
                                players=[player],
                                rest_api=rest_api1,
                                cache_path="/tmp/kalliope",
                                resources=resources,
                                variables={"key1": "val1"},
                                options=options,
                                send_anonymous_usage_stats=0)
            setting1.kalliope_version = "0.4.5"

            setting2 = Settings(default_tts_name="pico2wav",
                                default_stt_name="google",
                                default_trigger_name="swoyboy",
                                default_player_name="mplayer",
                                ttss=[tts2],
                                stts=[stt2],
                                triggers=[trigger1],
                                players=[player],
                                rest_api=rest_api1,
                                cache_path="/tmp/kalliope",
                                resources=resources,
                                variables={"key1": "val1"},
                                options=options,
                                send_anonymous_usage_stats=0)

            setting3 = Settings(default_tts_name="pico2wav",
                                default_stt_name="google",
                                default_trigger_name="swoyboy",
                                default_player_name="mplayer",
                                ttss=[tts1],
                                stts=[stt1],
                                triggers=[trigger1],
                                players=[player],
                                rest_api=rest_api1,
                                cache_path="/tmp/kalliope",
                                resources=resources,
                                variables={"key1": "val1"},
                                options=options,
                                send_anonymous_usage_stats=0)
            setting3.kalliope_version = "0.4.5"

            expected_result_serialize = {
                'default_tts_name': 'pico2wav',
                'default_stt_name': 'google',
                'default_trigger_name': 'swoyboy',
                'default_player_name': 'mplayer',
                'ttss': [{
                    'name': 'tts1',
                    'parameters': {}
                }],
                'stts': [{
                    'name': 'stt1',
                    'parameters': {}
                }],
                'triggers': [{
                    'name': 'snowboy',
                    'parameters': {}
                }],
                'players': [{
                    'name': 'player1',
                    'parameters': None
                }],
                'rest_api': {
                    'password_protected': True,
                    'login': '******',
                    'password': '******',
                    'active': True,
                    'port': 5000,
                    'allowed_cors_origin': '*'
                },
                'cache_path': '/tmp/kalliope',
                'resources': {
                    'neuron_folder': None,
                    'stt_folder': None,
                    'tts_folder': None,
                    'trigger_folder': None,
                    'signal_folder': None
                },
                'variables': {
                    'key1': 'val1'
                },
                'machine': 'pumpkins',
                'kalliope_version': '0.4.5',
                'options': {
                    'name': 'Options',
                    'recognizer_multiplier': 1.0,
                    'recognizer_energy_ratio': 1.5,
                    'recognizer_recording_timeout': 15.0,
                    'recognizer_recording_timeout_with_silence': 3.0,
                    'deaf': None,
                    'mute': None
                },
                'hooks': None,
                'send_anonymous_usage_stats': 0
            }

            self.maxDiff = None
            self.assertDictEqual(expected_result_serialize,
                                 setting1.serialize())

            self.assertTrue(setting1.__eq__(setting3))
            self.assertFalse(setting1.__eq__(setting2))
コード例 #10
0
    def _set_settings(self):
        # PLAYERS
        if self.default_player:
            SettingEditor.set_default_player(self.default_player)

        if self.players:
            for player_el in self.players:
                if isinstance(player_el, dict):
                    for player_name in player_el:
                        name = player_name
                        parameters = player_el[name]
                        new_player = Player(name=name, parameters=parameters)
                        SettingEditor.set_players(new_player)

        # STT
        if self.default_stt:
            SettingEditor.set_default_stt(self.default_stt)

        if self.speech_to_text:
            for stt_el in self.speech_to_text:
                if isinstance(stt_el, dict):
                    for stt_name in stt_el:
                        name = stt_name
                        parameters = stt_el[name]
                        new_stt = Stt(name=name, parameters=parameters)
                        SettingEditor.set_stts(new_stt)

        # TRIGGER
        if self.default_trigger:
            SettingEditor.set_default_trigger(self.default_trigger)

        if self.triggers:
            for trigger_el in self.triggers:
                if isinstance(trigger_el, dict):
                    for trigger_name in trigger_el:
                        name = trigger_name
                        parameters = trigger_el[name]
                        new_trigger = Trigger(name=name, parameters=parameters)
                        SettingEditor.set_trigger(new_trigger)

        # TTS
        if self.default_tts:
            SettingEditor.set_default_tts(self.default_tts)

        if self.text_to_speech:
            for tts_el in self.text_to_speech:
                if isinstance(tts_el, dict):
                    for tts_name in tts_el:
                        name = tts_name
                        parameters = tts_el[name]
                        new_tts = Tts(name=name, parameters=parameters)
                        SettingEditor.set_ttss(new_tts)

        # Options
        if self.deaf is not None:
            signal_order = SignalLauncher.get_order_instance()
            if signal_order is not None:
                SettingEditor.set_deaf_status(signal_order.trigger_instance,
                                              self.deaf)

        if self.mute is not None:
            SettingEditor.set_mute_status(self.mute)

        if self.energy_threshold is not None:
            SettingEditor.set_energy_threshold(self.energy_threshold)

        if self.adjust_for_ambient_noise_second is not None:
            SettingEditor.set_adjust_for_ambient_noise_second(
                self.adjust_for_ambient_noise_second)

        # Hooks
        if self.hooks:
            SettingEditor.set_hooks(self.hooks)

        # Variables
        if self.var_files:
            variables = dict()
            for files in self.var_files:
                var = Utils.get_real_file_path(files)
                # var is None has been checked previously in _is_parameters_ok() method
                variables.update(YAMLLoader.get_config(var))
            SettingEditor.set_variables(variables)

        if self.variable is not None:
            SettingEditor.set_variables(self.variable)