def test_get_dynamic_class_instantiation(self): """ Test that an instance as been instantiate properly. """ sl = SettingLoader() sl.settings.resource_dir = '/var/tmp/test/resources' neuron = Neuron( name='Say', parameters={'message': 'test dynamic class instantiate'}) say1 = Utils.get_dynamic_class_instantiation( package_name="neurons", module_name=neuron.name.capitalize(), parameters=neuron.parameters, resources_dir='/var/tmp/test/resources') self.assertTrue(isinstance(say1, Say), "Failed to dynamically instantiate neuron 'Say'") say2 = Utils.get_dynamic_class_instantiation( package_name="neurons", module_name=neuron.name.capitalize(), parameters=neuron.parameters, resources_dir='/var/tmp/test/resources') self.assertTrue(isinstance(say2, Say), "Failed to dynamically instantiate neuron 'Say'") self.assertNotEqual( id(say1), id(say2), "Dynamic class instantiations must return unique instances")
def load_stt_plugin(self): if self.stt is None: self.stt_module_name = self.settings.default_stt_name for stt_object in self.settings.stts: if stt_object.name == self.stt_module_name: stt_object.parameters["callback"] = self.callback Utils.get_dynamic_class_instantiation( package_name='stt', module_name=stt_object.name.capitalize(), parameters=stt_object.parameters)
def say(self, message): """ USe TTS to speak out loud the Message. A message can be a string, a list or a dict If it's a string, simply use the TTS with the message If it's a list, we select randomly a string in the list and give it to the TTS If it's a dict, we use the template given in parameter to create a string that we give to the TTS :param message: Can be a String or a dict or a list .. raises:: TTSModuleNotFound """ logger.debug("[NeuronModule] Say() called with message: %s" % message) tts_message = None # we can save parameters from the neuron in memory Cortex.save_neuron_parameter_in_memory(self.kalliope_memory, message) if isinstance(message, str) or isinstance(message, six.text_type): logger.debug("[NeuronModule] message is string") tts_message = message if isinstance(message, list): logger.debug("[NeuronModule] message is list") tts_message = random.choice(message) if isinstance(message, dict): logger.debug("[NeuronModule] message is dict") tts_message = self._get_message_from_dict(message) if tts_message is not None: logger.debug("[NeuronModule] tts_message to say: %s" % tts_message) self.tts_message = tts_message Utils.print_success(tts_message) # save in kalliope memory the last tts message Cortex.save("kalliope_last_tts_message", tts_message) # process the audio only if the mute flag is false if self.settings.options.mute: logger.debug("[NeuronModule] mute is True, Kalliope is muted") else: logger.debug( "[NeuronModule] mute is False, make Kalliope speaking") HookManager.on_start_speaking() # get the instance of the TTS module tts_folder = None if self.settings.resources: tts_folder = self.settings.resources.tts_folder tts_module_instance = Utils.get_dynamic_class_instantiation( package_name="tts", module_name=self.tts.name, parameters=self.tts.parameters, resources_dir=tts_folder) # generate the audio file and play it tts_module_instance.say(tts_message) HookManager.on_stop_speaking()
def say(self, message): """ USe TTS to speak out loud the Message. A message can be a string, a list or a dict If it's a string, simply use the TTS with the message If it's a list, we select randomly a string in the list and give it to the TTS If it's a dict, we use the template given in parameter to create a string that we give to the TTS :param message: Can be a String or a dict or a list .. raises:: TTSModuleNotFound """ logger.debug("NeuronModule Say() called with message: %s" % message) tts_message = None if isinstance(message, str) or isinstance(message, unicode): logger.debug("message is string") tts_message = message if isinstance(message, list): logger.debug("message is list") tts_message = random.choice(message) if isinstance(message, dict): logger.debug("message is dict") tts_message = self._get_message_from_dict(message) if tts_message is not None: logger.debug("tts_message to say: %s" % tts_message) # create a tts object from the tts the user want to use tts_object = next( (x for x in self.settings.ttss if x.name == self.tts), None) if tts_object is None: raise TTSModuleNotFound( "The tts module name %s does not exist in settings file" % self.tts) # change the cache settings with the one precised for the current neuron if self.override_cache is not None: tts_object.parameters = self._update_cache_var( self.override_cache, tts_object.parameters) logger.debug("NeuroneModule: TTS args: %s" % tts_object) # get the instance of the TTS module tts_folder = None if self.settings.resources: tts_folder = self.settings.resources.tts_folder tts_module_instance = Utils.get_dynamic_class_instantiation( package_name="tts", module_name=tts_object.name, parameters=tts_object.parameters, resources_dir=tts_folder) # generate the audio file and play it tts_module_instance.say(tts_message)
def say(self, message): """ USe TTS to speak out loud the Message. A message can be a string, a list or a dict If it's a string, simply use the TTS with the message If it's a list, we select randomly a string in the list and give it to the TTS If it's a dict, we use the template given in parameter to create a string that we give to the TTS :param message: Can be a String or a dict or a list .. raises:: TTSModuleNotFound """ logger.debug("[NeuronModule] Say() called with message: %s" % message) tts_message = None # we can save parameters from the neuron in memory Cortex.save_neuron_parameter_in_memory(self.kalliope_memory, message) if isinstance(message, str) or isinstance(message, six.text_type): logger.debug("[NeuronModule] message is string") tts_message = message if isinstance(message, list): logger.debug("[NeuronModule] message is list") tts_message = random.choice(message) if isinstance(message, dict): logger.debug("[NeuronModule] message is dict") tts_message = self._get_message_from_dict(message) if tts_message is not None: logger.debug("[NeuronModule] tts_message to say: %s" % tts_message) self.tts_message = tts_message Utils.print_success(tts_message) # save in kalliope memory the last tts message Cortex.save("kalliope_last_tts_message", tts_message) # process the audio only if the mute flag is false if self.settings.options.mute: logger.debug("[NeuronModule] mute is True, Kalliope is muted") else: logger.debug("[NeuronModule] mute is False, make Kalliope speaking") HookManager.on_start_speaking() # get the instance of the TTS module tts_folder = None if self.settings.resources: tts_folder = self.settings.resources.tts_folder tts_module_instance = Utils.get_dynamic_class_instantiation(package_name="tts", module_name=self.tts.name, parameters=self.tts.parameters, resources_dir=tts_folder) # generate the audio file and play it tts_module_instance.say(tts_message) HookManager.on_stop_speaking()
def say(self, message): """ USe TTS to speak out loud the Message. A message can be a string, a list or a dict If it's a string, simply use the TTS with the message If it's a list, we select randomly a string in the list and give it to the TTS If it's a dict, we use the template given in parameter to create a string that we give to the TTS :param message: Can be a String or a dict or a list .. raises:: TTSModuleNotFound """ logger.debug("[NeuronModule] Say() called with message: %s" % message) tts_message = None if isinstance(message, str) or isinstance(message, six.text_type): logger.debug("[NeuronModule] message is string") tts_message = message if isinstance(message, list): logger.debug("[NeuronModule] message is list") tts_message = random.choice(message) if isinstance(message, dict): logger.debug("[NeuronModule] message is dict") tts_message = self._get_message_from_dict(message) if tts_message is not None: logger.debug("[NeuronModule] tts_message to say: %s" % tts_message) self.tts_message = tts_message Utils.print_success(tts_message) # process the audio only if the no_voice flag is false if self.no_voice: logger.debug("[NeuronModule] no_voice is True, Kalliope is muted") else: logger.debug("[NeuronModule] no_voice is False, make Kalliope speaking") # get the instance of the TTS module tts_folder = None if self.settings.resources: tts_folder = self.settings.resources.tts_folder tts_module_instance = Utils.get_dynamic_class_instantiation(package_name="tts", module_name=self.tts.name, parameters=self.tts.parameters, resources_dir=tts_folder) # Kalliope will talk, turn on the LED self.switch_on_led_talking(rpi_settings=self.settings.rpi_settings, on=True) # generate the audio file and play it tts_module_instance.say(tts_message) # Kalliope has finished to talk, turn off the LED self.switch_on_led_talking(rpi_settings=self.settings.rpi_settings, on=False)
def test_get_dynamic_class_instantiation(self): """ Test that an instance as been instantiate properly. """ sl = SettingLoader() sl.settings.resource_dir = '/var/tmp/test/resources' neuron = Neuron(name='Say', parameters={'message': 'test dynamic class instantiate'}) self.assertTrue(isinstance(Utils.get_dynamic_class_instantiation(package_name="neurons", module_name=neuron.name.capitalize(), parameters=neuron.parameters, resources_dir='/var/tmp/test/resources'), Say), "Fail instantiate a class")
def launch_neuron(cls, neuron): """ Start a neuron plugin :param neuron: neuron object :type neuron: Neuron :return: """ logger.debug("Run neuron: \"%s\"" % (neuron.__str__())) settings = cls.load_settings() neuron_folder = None if settings.resources: neuron_folder = settings.resources.neuron_folder return Utils.get_dynamic_class_instantiation(package_name="neurons", module_name=neuron.name, parameters=neuron.parameters, resources_dir=neuron_folder)
def test_get_dynamic_class_instantiation(self): """ Test that an instance as been instantiate properly. """ sl = SettingLoader() sl.settings.resource_dir = '/var/tmp/test/resources' neuron = Neuron( name='Say', parameters={'message': 'test dynamic class instantiate'}) self.assertTrue( isinstance( Utils.get_dynamic_class_instantiation( package_name="neurons", module_name=neuron.name.capitalize(), parameters=neuron.parameters, resources_dir='/var/tmp/test/resources'), Say), "Fail instantiate a class")
def load_stt_plugin(self): if self.stt is None: self.stt_module_name = self.settings.default_stt_name logger.debug("[OrderListener] stt module name : %s" % self.stt_module_name) for stt_object in self.settings.stts: if stt_object.name == self.stt_module_name: stt_object.parameters["callback"] = self.callback # add the audio file path to the list of parameter if set stt_object.parameters["audio_file_path"] = self.audio_file_path stt_folder = None if self.settings.resources: stt_folder = self.settings.resources.stt_folder return Utils.get_dynamic_class_instantiation( package_name='stt', module_name=stt_object.name.capitalize(), parameters=stt_object.parameters, resources_dir=stt_folder)
def start_neuron(cls, neuron): """ Start a neuron plugin :param neuron: neuron object :type neuron: Neuron :return: """ logger.debug("Run plugin \"%s\" with parameters %s" % (neuron.name, neuron.parameters)) sl = SettingLoader() settings = sl.settings neuron_folder = None if settings.resources: neuron_folder = settings.resources.neuron_folder return Utils.get_dynamic_class_instantiation( package_name="neurons", module_name=neuron.name, parameters=neuron.parameters, resources_dir=neuron_folder)
def load_stt_plugin(self): if self.stt is None: self.stt_module_name = self.settings.default_stt_name logger.debug("[OrderListener] stt module name : %s" % self.stt_module_name) for stt_object in self.settings.stts: if stt_object.name == self.stt_module_name: stt_object.parameters["callback"] = self.callback # add the audio file path to the list of parameter if set if self.audio_file_path is not None: stt_object.parameters["audio_file_path"] = self.audio_file_path stt_folder = None if self.settings.resources: stt_folder = self.settings.resources.stt_folder return Utils.get_dynamic_class_instantiation(package_name='stt', module_name=stt_object.name.capitalize(), parameters=stt_object.parameters, resources_dir=stt_folder)