示例#1
0
    def _generate_audio_file(self):
        """
        Generic method used as a Callback in TTSModule
        """

        # Prepare payload
        payload = self.get_payload()

        headers = {
            "Content-Type": "application/json",
            "Accept": "audio/wav"
        }

        url = "%s/synthesize?voice=%s" % (TTS_URL, self.voice)

        response = requests.post(url,
                                 auth=HTTPBasicAuth(self.username, self.password),
                                 headers=headers,
                                 json=payload)

        logger.debug("[Watson TTS] status code: %s" % response.status_code)

        if response.status_code == 200:
            # OK we get the audio we can write the sound file
            FileManager.write_in_file(self.file_path, response.content)
        else:
            logger.debug("[Watson TTS] Fail to get audio. Header: %s" % response.headers)
示例#2
0
    def __init__(self, **kwargs):

        # set parameter from what we receive from the settings
        self.cache = kwargs.get('cache', False)
        self.language = kwargs.get('language', None)
        self.voice = kwargs.get('voice', "default")
        # the name of the TSS is the name of the Tss module that have instantiated TTSModule
        self.tts_caller_name = self.__class__.__name__

        # we don't know yet the words that will be converted to an audio and so we don't have the audio path yet too
        self.words = None
        self.file_path = None
        self.base_cache_path = None

        # load settings
        sl = SettingLoader()
        self.settings = sl.settings

        # create the path in the tmp folder
        base_path = os.path.join(self.settings.cache_path,
                                 self.tts_caller_name, self.language,
                                 self.voice)
        FileManager.create_directory(base_path)

        logger.debug(
            "Class TTSModule called from module %s, cache: %s, language: %s, voice: %s"
            % (self.tts_caller_name, self.cache, self.language, self.voice))
示例#3
0
    def generate_and_play(self,
                          words,
                          generate_audio_function_from_child=None):
        """
        Generate an audio file from <words> if not already in cache and call the Player to play it
        :param words: Sentence text from which we want to generate an audio file
        :type words: String
        :param generate_audio_function_from_child: The child function to generate a file if necessary
        :type generate_audio_function_from_child; Callback function

        .. raises:: TtsGenerateAudioFunctionNotFound
        """
        if generate_audio_function_from_child is None:
            raise TtsGenerateAudioFunctionNotFound

        self.words = words
        # we can generate the file path from info we have
        self.file_path = self._get_path_to_store_audio()

        if not self.cache:
            # no cache, we need to generate the file
            generate_audio_function_from_child()
        else:
            # we check if the file already exist. If not we generate it with the TTS engine
            if not self._is_file_already_in_cache(self.base_cache_path,
                                                  self.file_path):
                generate_audio_function_from_child()

        # then play the generated audio file
        self.play_audio()

        # if the user don't want to keep the cache we remove the file
        if not self.cache:
            FileManager.remove_file(self.file_path)
    def test_write_in_file(self):
        """
        Test to write in file.
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.write_in_file
        FileManager.write_in_file(file_path=file_path, content=in_file_text)
        with open(file_path, 'r') as content_file:
            content = content_file.read()
            self.assertEqual(content, in_file_text,
                             "Fail writing in the file ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)
示例#5
0
    def _generate_audio_file(self):
        """
        Generic method used as a Callback in TTSModule
        """

        # Prepare payload
        payload = self.get_payload()

        headers = {"Content-Type": "application/json", "Accept": "audio/wav"}

        endpoint_location = self.location if self.location.endswith(
            '/') else self.location + "/"
        url = "%s/synthesize?voice=%s" % (endpoint_location + API_VERSION,
                                          self.voice)

        response = requests.post(url,
                                 auth=HTTPBasicAuth("apikey", self.apikey),
                                 headers=headers,
                                 json=payload)

        logger.debug("[Watson TTS] status code: %s" % response.status_code)

        if response.status_code == 200:
            # OK we get the audio we can write the sound file
            FileManager.write_in_file(self.file_path, response.content)
        else:
            logger.debug("[Watson TTS] Fail to get audio. Header: %s" %
                         response.headers)
示例#6
0
    def generate_and_play(self, words, generate_audio_function_from_child=None):
        """
        Generate an audio file from <words> if not already in cache and call the Player to play it
        :param words: Sentence text from which we want to generate an audio file
        :type words: String
        :param generate_audio_function_from_child: The child function to generate a file if necessary
        :type generate_audio_function_from_child; Callback function

        .. raises:: TtsGenerateAudioFunctionNotFound
        """
        if generate_audio_function_from_child is None:
            raise TtsGenerateAudioFunctionNotFound

        self.words = words
        # we can generate the file path from info we have
        self.file_path = self._get_path_to_store_audio()

        if not self.cache:
            # no cache, we need to generate the file
            generate_audio_function_from_child()
        else:
            # we check if the file already exist. If not we generate it with the TTS engine
            if not self._is_file_already_in_cache(self.base_cache_path, self.file_path):
                generate_audio_function_from_child()

        # then play the generated audio file
        self.play_audio()

        # if the user don't want to keep the cache we remove the file
        if not self.cache:
            FileManager.remove_file(self.file_path)
示例#7
0
    def __init__(self, **kwargs):

        # set parameter from what we receive from the settings
        self.cache = kwargs.get('cache', False)
        self.language = kwargs.get('language', "default")
        self.voice = kwargs.get('voice', "default")
        # the name of the TSS is the name of the Tss module that have instantiated TTSModule
        self.tts_caller_name = self.__class__.__name__

        # we don't know yet the words that will be converted to an audio and so we don't have the audio path yet too
        self.words = None
        self.file_path = None
        self.base_cache_path = None

        # load settings
        sl = SettingLoader()
        self.settings = sl.settings
        self.player = PlayerLauncher.get_player(settings=self.settings)

        # create the path in the tmp folder
        base_path = os.path.join(self.settings.cache_path, self.tts_caller_name, self.language, self.voice)
        FileManager.create_directory(base_path)

        logger.debug("Class TTSModule called from module %s, cache: %s, language: %s, voice: %s" % (self.tts_caller_name,
                                                                                                     self.cache,
                                                                                                     self.language,
                                                                                                     self.voice))
示例#8
0
    def __init__(self, app, port=5000, brain=None, allowed_cors_origin=False):
        """

        :param app: Flask API
        :param port: Port to listen
        :param brain: Brain object
        :type brain: Brain
        """
        super(FlaskAPI, self).__init__()
        self.app = app
        self.port = port
        self.brain = brain
        self.allowed_cors_origin = allowed_cors_origin

        # get current settings
        sl = SettingLoader()
        self.settings = sl.settings

        # configure the upload folder
        app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
        # create the temp folder
        FileManager.create_directory(UPLOAD_FOLDER)

        # Flask configuration remove default Flask behaviour to encode to ASCII
        self.app.url_map.strict_slashes = False
        self.app.config['JSON_AS_ASCII'] = False

        if self.allowed_cors_origin is not False:
            CORS(app, resources={r"/*": {"origins": allowed_cors_origin}}, supports_credentials=True)

        # Add routing rules
        self.app.add_url_rule('/', view_func=self.get_main_page, methods=['GET'])
        self.app.add_url_rule('/shutdown/', view_func=self.shutdown_server, methods=['POST'])

        # Register blue prints
        self.synapses_blueprint = SynapsesView('synapses',
                                               __name__,
                                               app=self.app,
                                               brain=self.brain,
                                               settings=self.settings)
        self.app.register_blueprint(self.synapses_blueprint)
        self.settings_blueprint = SettingsView('settings',
                                               __name__,
                                               app=self.app,
                                               brain=self.brain,
                                               settings=self.settings)
        self.app.register_blueprint(self.settings_blueprint)
        self.neurons_blueprint = NeuronsView('neurons',
                                             __name__,
                                             app=self.app,
                                             brain=self.brain,
                                             settings=self.settings)
        self.app.register_blueprint(self.neurons_blueprint)
示例#9
0
    def _is_file_already_in_cache(base_cache_path, file_path):
        """
        Return true if the file to generate has already been generated before
        """
        # generate sub folder
        FileManager.create_directory(base_cache_path)

        # check if the audio file exist
        exist_in_cache = os.path.exists(file_path)

        if exist_in_cache:
            logger.debug("TTSModule, File already in cache: %s" % file_path)
        else:
            logger.debug("TTSModule, File not yet in cache: %s" % file_path)
        return exist_in_cache
示例#10
0
    def _is_file_already_in_cache(base_cache_path, file_path):
        """
        Return true if the file to generate has already been generated before
        """
        # generate sub folder
        FileManager.create_directory(base_cache_path)

        # check if the audio file exist
        exist_in_cache = os.path.exists(file_path)

        if exist_in_cache:
            logger.debug("TTSModule, File already in cache: %s" % file_path)
        else:
            logger.debug("TTSModule, File not yet in cache: %s" % file_path)
        return exist_in_cache
示例#11
0
    def test_create_directory(self):
        """
        Test to create a new directory.
        """
        # set up
        cache_path = "/tmp/kalliope/tests/testDirectory"
        if os.path.exists(cache_path):
            os.removedirs(cache_path)

        # Test FileManager.create_directory
        FileManager.create_directory(cache_path)
        self.assertTrue(os.path.exists(cache_path),
                        "Fail creating a directory to the path ")

        # Remove the directory
        os.removedirs(cache_path)
    def test_create_directory(self):
        """
        Test to create a new directory.
        """
        # set up
        cache_path = "/tmp/kalliope/tests/testDirectory"
        if os.path.exists(cache_path):
            os.removedirs(cache_path)

        # Test FileManager.create_directory
        FileManager.create_directory(cache_path)
        self.assertTrue(os.path.exists(cache_path),
                        "Fail creating a directory to the path ")

        # Remove the directory
        os.removedirs(cache_path)
示例#13
0
    def _get_cache_path(settings):
        """
        Return the path where to store the cache

        :param settings: The YAML settings file
        :type settings: dict
        :return: the path to store the cache
        :rtype: String

        :Example:

            cache_path = cls._get_cache_path(settings)

        .. seealso::
        .. raises:: SettingNotFound, NullSettingException, SettingInvalidException
        .. warnings:: Class Method and Private
        """

        try:
            cache_path = settings["cache_path"]
        except KeyError as e:
            raise SettingNotFound("%s setting not found" % e)

        if cache_path is None:
            raise NullSettingException("cache_path setting cannot be null")

        # test if that path is usable
        if FileManager.is_path_exists_or_creatable(cache_path):
            return cache_path
        else:
            raise SettingInvalidException("The cache_path seems to be invalid: %s" % cache_path)
示例#14
0
 def convert_mp3_to_wav(file_path_mp3):
     """ 
     PyAudio, AlsaPlayer, sounddevices  do not support mp3 files 
     MP3 files must be converted to a wave in order to be played
     This function assumes ffmpeg is available on the system
     :param file_path_mp3: the file path to convert from mp3 to wav
     """
     logger.debug("Converting mp3 file to wav file: %s" % file_path_mp3)
     fnull = open(os.devnull, 'w')
     # temp file
     tmp_file_wav = file_path_mp3 + ".wav"
     # Convert mp3 to wave
     subprocess.call(['avconv', '-y', '-i', file_path_mp3, tmp_file_wav],
                     stdout=fnull, stderr=fnull)
     # remove the original file
     FileManager.remove_file(file_path_mp3)
     # rename the temp file with the same name as the original file
     os.rename(tmp_file_wav, file_path_mp3)
示例#15
0
 def convert_mp3_to_wav(file_path_mp3):
     """ 
     PyAudio, AlsaPlayer, sounddevices  do not support mp3 files 
     MP3 files must be converted to a wave in order to be played
     This function assumes ffmpeg is available on the system
     :param file_path_mp3: the file path to convert from mp3 to wav
     """
     logger.debug("[PlayerModule] Converting mp3 file to wav file: %s" % file_path_mp3)
     fnull = open(os.devnull, 'w')
     # temp file
     tmp_file_wav = file_path_mp3 + ".wav"
     # Convert mp3 to wave
     subprocess.call(['avconv', '-y', '-i', file_path_mp3, tmp_file_wav],
                     stdout=fnull, stderr=fnull)
     # remove the original file
     FileManager.remove_file(file_path_mp3)
     # rename the temp file with the same name as the original file
     os.rename(tmp_file_wav, file_path_mp3)
    def test_is_path_exists_or_creatable(self):
        """
        Test the _is_path_exists_or_creatable
        4 scenarii :
            - the file exists and is creatable : return True
            - the file does not exist but is creatable : return True
            - the file exists but is not allowed : return True --> need a review !
            - the file does not exist and is not allowed : return False
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_fileIsPathExistsOrCreatable"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test the file exist and creatable : return True
        with open(file_path, "wb") as file_open:
            file_open.write(
                b"[Kalliope] Test Running the test_is_path_exists_or_creatable method"
            )
            file_open.close()
        self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
                        "Fail to assert the file exist ")

        # test the file not exist but creatable : return True
        os.remove(file_path)
        self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
                        "Fail asserting the file does not exist ")

        # test the file exist but not creatable : return True
        # file_exist_not_allowed = "/root/.ssh/known_hosts"
        # self.assertTrue(FileManager.is_path_creatable(file_exist_not_allowed))

        # test the file not exist and not allowed : return False
        not_allowed_root_path = "/root/"
        not_allowed_path = os.path.join(not_allowed_root_path, file_name)
        self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
                         "Fail to assert not accessing this path ")
    def test_write_in_file(self):
        """
        Test to write in file.
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.write_in_file
        FileManager.write_in_file(file_path=file_path, content=in_file_text)
        with open(file_path, 'r') as content_file:
            content = content_file.read()
            self.assertEqual(content, in_file_text,
                             "Fail writing in the file ")

        if sys.version_info[0] > 2:
            # Test writing of bytes object for python3
            FileManager.write_in_file(file_path=file_path,
                                      content=bytes(in_file_text, 'utf-8'))
            with open(file_path, 'r') as content_file:
                content = content_file.read()
                self.assertEqual(content, in_file_text,
                                 "Fail writing in the file ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)

        # run into IOError by trying to write something in root
        dir_path = "/root/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        self.assertFalse(
            FileManager.write_in_file(file_path=file_path,
                                      content=in_file_text))
    def test_remove_file(self):
        """
        Test to remove a file
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_fileRemove"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test to remove the file
        # FileManager.remove_file
        with open(file_path, "wb") as file_open:
            file_open.write(b"")
            file_open.close()
        FileManager.remove_file(file_path=file_path)
        self.assertFalse(os.path.exists(file_path), "Fail removing the file")
示例#19
0
    def test_is_file_already_in_cache(self):
        """
        Test if file is already stored in cache
        """

        base_cache_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
        md5_word = "5c186d1e123be2667fb5fd54640e4fd0"
        file_path = os.path.join(base_cache_path,
                                 "5c186d1e123be2667fb5fd54640e4fd0.tts")

        if os.path.isfile(file_path):
            # Remove the file
            FileManager.remove_file(file_path)
        # Create a tmp file
        if not os.path.exists(base_cache_path):
            os.makedirs(base_cache_path)
        tmp_path = os.path.join(base_cache_path, md5_word + ".tts")
        FileManager.write_in_file(
            tmp_path, "[kalliope-test] test_is_file_already_in_cache")

        # Test true
        self.assertTrue(
            TTSModule._is_file_already_in_cache(
                base_cache_path=base_cache_path, file_path=file_path),
            "Fail retrieving the cached file. The file does not exist but it should !"
        )

        # Remove the tmp file
        FileManager.remove_file(tmp_path)

        # Test False
        self.assertFalse(
            TTSModule._is_file_already_in_cache(
                base_cache_path=base_cache_path, file_path=file_path),
            "Fail asserting that the file does not exist.")
示例#20
0
    def test_is_path_exists_or_creatable(self):
        """
        Test the _is_path_exists_or_creatable
        4 scenarii :
            - the file exists and is creatable : return True
            - the file does not exist but is creatable : return True
            - the file exists but is not allowed : return True --> need a review !
            - the file does not exist and is not allowed : return False
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_fileIsPathExistsOrCreatable"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test the file exist and creatable : return True
        with open(file_path, "wb") as file_open:
            file_open.write(b"[Kalliope] Test Running the test_is_path_exists_or_creatable method")
            file_open.close()
        self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
                        "Fail to assert the file exist ")

        # test the file not exist but creatable : return True
        os.remove(file_path)
        self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
                         "Fail asserting the file does not exist ")

        # test the file exist but not creatable : return True
        # file_exist_not_allowed = "/root/.ssh/known_hosts"
        # self.assertTrue(FileManager.is_path_creatable(file_exist_not_allowed))

        # test the file not exist and not allowed : return False
        not_allowed_root_path = "/root/"
        not_allowed_path = os.path.join(not_allowed_root_path, file_name)
        self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
                         "Fail to assert not accessing this path ")
示例#21
0
    def test_is_file_already_in_cache(self):
        """
        Test if file is already stored in cache
        """

        base_cache_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
        md5_word = "5c186d1e123be2667fb5fd54640e4fd0"
        file_path = os.path.join(base_cache_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")

        if os.path.isfile(file_path):
            # Remove the file
            FileManager.remove_file(file_path)
        # Create a tmp file
        if not os.path.exists(base_cache_path):
            os.makedirs(base_cache_path)
        tmp_path = os.path.join(base_cache_path, md5_word+".tts")
        FileManager.write_in_file(tmp_path, "[kalliope-test] test_is_file_already_in_cache")

        # Test true
        self.assertTrue(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
                        "Fail retrieving the cached file. The file does not exist but it should !")

        # Remove the tmp file
        FileManager.remove_file(tmp_path)

        # Test False
        self.assertFalse(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
                         "Fail asserting that the file does not exist.")
示例#22
0
    def test_remove_file(self):
        """
        Test to remove a file
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_fileRemove"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test to remove the file
        # FileManager.remove_file
        with open(file_path, "wb") as file_open:
            file_open.write(b"")
            file_open.close()
        FileManager.remove_file(file_path=file_path)
        self.assertFalse(os.path.exists(file_path),
                         "Fail removing the file")
示例#23
0
    def test_write_in_file(self):
        """
        Test to write in file.
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path,file_name)
        in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.write_in_file
        FileManager.write_in_file(file_path=file_path, content=in_file_text)
        with open(file_path, 'r') as content_file:
            content = content_file.read()
            self.assertEqual(content, in_file_text,
                             "Fail writing in the file ")

        if sys.version_info[0] > 2:
            # Test writing of bytes object for python3
            FileManager.write_in_file(file_path=file_path, content=bytes(in_file_text, 'utf-8'))
            with open(file_path, 'r') as content_file:
                content = content_file.read()
                self.assertEqual(content, in_file_text,
                                "Fail writing in the file ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)

        # run into IOError by trying to write something in root
        dir_path = "/root/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        self.assertFalse(FileManager.write_in_file(file_path=file_path, content=in_file_text))
示例#24
0
 def convert_to_wav(file_path):
     """
     PyAudio, AlsaPlayer, sounddevices only support wav files.
     Other files must be converted to a wav in order to be played
     This function uses ffmpeg (which must be installed) for
     conversion; python core lib sndhdr is used to check if the
     provided file is already in wav format (skips conversion).
     :param file_path: the file path to convert to wav
     """
     filetype = sndhdr.whathdr(file_path)
     if filetype is None or filetype.filetype != 'wav':
         logger.debug("[PlayerModule] Converting file to wav file: %s" % file_path)
         fnull = open(os.devnull, 'w')
         # temp file
         tmp_file_wav = file_path+ ".wav"
         # Convert file to wave
         subprocess.call(['ffmpeg', '-y', '-i', file_path, tmp_file_wav],
                         stdout=fnull, stderr=fnull)
         # remove the original file
         FileManager.remove_file(file_path)
         # rename the temp file with the same name as the original file
         os.rename(tmp_file_wav, file_path)
示例#25
0
    def test_is_path_creatable(self):
        """
        Test if the path is creatable for the user
        Does the user has the permission to use this path ?
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_filePathCreatable"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # test not allowed : return False
        not_allowed_root_path = "/root/"
        not_allowed_path = os.path.join(not_allowed_root_path, file_name)
        self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
                         "Fail to assert not accessing this path ")
        # test allowed : return True
        self.assertTrue(FileManager.is_path_creatable(file_path))
    def test_is_path_creatable(self):
        """
        Test if the path is creatable for the user
        Does the user has the permission to use this path ?
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_filePathCreatable"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # test not allowed : return False
        not_allowed_root_path = "/root/"
        not_allowed_path = os.path.join(not_allowed_root_path, file_name)
        self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
                         "Fail to assert not accessing this path ")
        # test allowed : return True
        self.assertTrue(FileManager.is_path_creatable(file_path))
示例#27
0
    def test_generate_and_play(self):
        """
        Test to generate and play sound
        """
        def new_play_audio(TTSModule):
            pass

        words = "kalliope"

        with mock.patch.object(TTSModule, 'play_audio', new=new_play_audio):
            settings = Settings(cache_path="/tmp/kalliope/tests")
            self.TTSMod.settings = settings

            # test missing callback
            with self.assertRaises(TtsGenerateAudioFunctionNotFound):
                self.TTSMod.generate_and_play(words=words)

            # Assert Callback is called
            # no Cache
            self.TTSMod.cache = False
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=
                                          generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True but not existing on system
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=
                                          generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True and existing on system
            # create tmp file
            tmp_base_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
            file_path = os.path.join(tmp_base_path,
                                     "5c186d1e123be2667fb5fd54640e4fd0.tts")
            if os.path.isfile(file_path):
                # Remove the file
                FileManager.remove_file(file_path)
            if not os.path.exists(tmp_base_path):
                os.makedirs(tmp_base_path)
            FileManager.write_in_file(
                file_path, "[kalliope-test] test_generate_and_play")
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=
                                          generate_audio_function_from_child)
            generate_audio_function_from_child.assert_not_called()
            # Remove the tmp file
            FileManager.remove_file(file_path)
示例#28
0
    def test_generate_and_play(self):
        """
        Test to generate and play sound
        """
        def new_play_audio(TTSModule):
            pass

        words = "kalliope"

        with mock.patch.object(TTSModule, 'play_audio', new=new_play_audio):
            settings = Settings(cache_path="/tmp/kalliope/tests")
            self.TTSMod.settings = settings

            # test missing callback
            with self.assertRaises(TtsGenerateAudioFunctionNotFound):
                self.TTSMod.generate_and_play(words=words)

            # Assert Callback is called
            # no Cache
            self.TTSMod.cache = False
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True but not existing on system
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True and existing on system
            # create tmp file
            tmp_base_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
            file_path = os.path.join(tmp_base_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")
            if os.path.isfile(file_path):
                # Remove the file
                FileManager.remove_file(file_path)
            if not os.path.exists(tmp_base_path):
                os.makedirs(tmp_base_path)
            FileManager.write_in_file(file_path, "[kalliope-test] test_generate_and_play")
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=generate_audio_function_from_child)
            generate_audio_function_from_child.assert_not_called()
            # Remove the tmp file
            FileManager.remove_file(file_path)
示例#29
0
    def test_file_is_empty(self):
        """
        Test that the file is empty
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_fileIsEmpty"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.file_is_empty
        with open(file_path, "wb") as file_open:
            file_open.write(b"")
            file_open.close()
        self.assertTrue(FileManager.file_is_empty(file_path=file_path),
                        "Fail matching to verify that file is empty ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)
    def test_file_is_empty(self):
        """
        Test that the file is empty
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_fileIsEmpty"
        file_path = os.path.join(dir_path, file_name)
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.file_is_empty
        with open(file_path, "wb") as file_open:
            file_open.write(b"")
            file_open.close()
        self.assertTrue(FileManager.file_is_empty(file_path=file_path),
                        "Fail matching to verify that file is empty ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)
示例#31
0
        .. seealso::
        .. raises:: SettingNotFound, NullSettingException, SettingInvalidException
        .. warnings:: Class Method and Private
        """

        try:
            cache_path = settings["cache_path"]
        except KeyError, e:
            raise SettingNotFound("%s setting not found" % e)

        if cache_path is None:
            raise NullSettingException("cache_path setting cannot be null")

        # test if that path is usable
        if FileManager.is_path_exists_or_creatable(cache_path):
            return cache_path
        else:
            raise SettingInvalidException(
                "The cache_path seems to be invalid: %s" % cache_path)

    @staticmethod
    def _get_default_synapse(settings):
        """
        Return the name of the default synapse

        :param settings: The YAML settings file
        :type settings: dict
        :return: the default synapse name
        :rtype: String
示例#32
0
    def __init__(self, app, port=5000, brain=None, allowed_cors_origin=False):
        """

        :param app: Flask API
        :param port: Port to listen
        :param brain: Brain object
        :type brain: Brain
        """
        super(FlaskAPI, self).__init__()
        self.app = app
        self.port = port
        self.brain = brain
        self.allowed_cors_origin = allowed_cors_origin

        # get current settings
        sl = SettingLoader()
        self.settings = sl.settings

        # api_response sent by the Order Analyser when using the /synapses/start/audio URL
        self.api_response = None
        # boolean used to notify the main process that we get the list of returned synapse
        self.order_analyser_return = False

        # configure the upload folder
        app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
        # create the temp folder
        FileManager.create_directory(UPLOAD_FOLDER)

        # Flask configuration remove default Flask behaviour to encode to ASCII
        self.app.url_map.strict_slashes = False
        self.app.config['JSON_AS_ASCII'] = False

        if self.allowed_cors_origin is not False:
            cors = CORS(app,
                        resources={r"/*": {
                            "origins": allowed_cors_origin
                        }},
                        supports_credentials=True)

        # Add routing rules
        self.app.add_url_rule('/',
                              view_func=self.get_main_page,
                              methods=['GET'])
        self.app.add_url_rule('/synapses',
                              view_func=self.get_synapses,
                              methods=['GET'])
        self.app.add_url_rule('/synapses/<synapse_name>',
                              view_func=self.get_synapse,
                              methods=['GET'])
        self.app.add_url_rule('/synapses/start/id/<synapse_name>',
                              view_func=self.run_synapse_by_name,
                              methods=['POST'])
        self.app.add_url_rule('/synapses/start/order',
                              view_func=self.run_synapse_by_order,
                              methods=['POST'])
        self.app.add_url_rule('/synapses/start/audio',
                              view_func=self.run_synapse_by_audio,
                              methods=['POST'])
        self.app.add_url_rule('/shutdown/',
                              view_func=self.shutdown_server,
                              methods=['POST'])
示例#33
0
    def __init__(self, app, port=5000, brain=None, allowed_cors_origin=False):
        """

        :param app: Flask API
        :param port: Port to listen
        :param brain: Brain object
        :type brain: Brain
        """
        super(FlaskAPI, self).__init__()
        self.app = app
        self.port = port
        self.brain = brain
        self.allowed_cors_origin = allowed_cors_origin

        # get current settings
        sl = SettingLoader()
        self.settings = sl.settings

        # api_response sent by the Order Analyser when using the /synapses/start/audio URL
        self.api_response = None
        # boolean used to notify the main process that we get the list of returned synapse
        self.order_analyser_return = False

        # configure the upload folder
        app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
        # create the temp folder
        FileManager.create_directory(UPLOAD_FOLDER)

        # Flask configuration remove default Flask behaviour to encode to ASCII
        self.app.url_map.strict_slashes = False
        self.app.config['JSON_AS_ASCII'] = False

        if self.allowed_cors_origin is not False:
            CORS(app, resources={r"/*": {"origins": allowed_cors_origin}}, supports_credentials=True)

        # Add routing rules
        self.app.add_url_rule('/', view_func=self.get_main_page, methods=['GET'])

        # Synapses
        self.app.add_url_rule('/synapses', view_func=self.get_synapses, methods=['GET'])
        self.app.add_url_rule('/synapses/<synapse_name>', view_func=self.get_synapse, methods=['GET'])
        self.app.add_url_rule('/synapses/start/id/<synapse_name>', view_func=self.run_synapse_by_name, methods=['POST'])
        self.app.add_url_rule('/synapses/start/order', view_func=self.run_synapse_by_order, methods=['POST'])
        self.app.add_url_rule('/synapses/start/audio', view_func=self.run_synapse_by_audio, methods=['POST'])

        # Life Cycle
        self.app.add_url_rule('/shutdown/', view_func=self.shutdown_server, methods=['POST'])

        # Settings
        self.app.add_url_rule('/settings', view_func=self.get_current_settings, methods=['GET'])
        self.app.add_url_rule('/settings/deaf/', view_func=self.get_deaf, methods=['GET'])
        self.app.add_url_rule('/settings/deaf/', view_func=self.set_deaf, methods=['POST'])
        self.app.add_url_rule('/settings/mute/', view_func=self.get_mute, methods=['GET'])
        self.app.add_url_rule('/settings/mute/', view_func=self.set_mute, methods=['POST'])
        self.app.add_url_rule('/settings/ambient_noise_second/', view_func=self.get_ambient_noise_second,
                              methods=['GET'])
        self.app.add_url_rule('/settings/ambient_noise_second/', view_func=self.set_adjust_for_ambient_noise_second,
                              methods=['POST'])
        self.app.add_url_rule('/settings/energy_threshold/', view_func=self.get_energy_threshold,
                              methods=['GET'])
        self.app.add_url_rule('/settings/energy_threshold/', view_func=self.set_energy_threshold,
                              methods=['POST'])
        self.app.add_url_rule('/settings/default_tts/', view_func=self.get_default_tts,
                              methods=['GET'])
        self.app.add_url_rule('/settings/default_tts/', view_func=self.set_default_tts,
                              methods=['POST'])
        self.app.add_url_rule('/settings/default_stt/', view_func=self.get_default_stt,
                              methods=['GET'])
        self.app.add_url_rule('/settings/default_stt/', view_func=self.set_default_stt,
                              methods=['POST'])
        self.app.add_url_rule('/settings/default_player/', view_func=self.get_default_player,
                              methods=['GET'])
        self.app.add_url_rule('/settings/default_player/', view_func=self.set_default_player,
                              methods=['POST'])
        self.app.add_url_rule('/settings/default_trigger/', view_func=self.get_default_trigger,
                              methods=['GET'])
        self.app.add_url_rule('/settings/default_trigger/', view_func=self.set_default_trigger,
                              methods=['POST'])
        self.app.add_url_rule('/settings/hooks/', view_func=self.get_hooks,
                              methods=['GET'])
        self.app.add_url_rule('/settings/hooks/', view_func=self.set_hooks,
                              methods=['POST'])
        self.app.add_url_rule('/settings/variables/', view_func=self.get_variables,
                              methods=['GET'])
        self.app.add_url_rule('/settings/variables/', view_func=self.set_variables,
                              methods=['POST'])
 def create_file_manager(self):
     file_manager = FileManager()
     self.assertIsInstance(FileManager, file_manager)