def __init__(self, file_path=None): self.file_path = file_path if self.file_path is None: self.file_path = Utils.get_real_file_path(FILE_NAME) else: self.file_path = Utils.get_real_file_path(file_path) # if the returned file path is none, the file doesn't exist if self.file_path is None: raise SettingNotFound("Settings.yml file not found") self.yaml_config = self._get_yaml_config() self.settings = self._get_settings()
def _get_random_wake_up_sounds(settings): """ Return a list of the wake up sounds set up on the settings.yml file :param settings: The YAML settings file :type settings: dict :return: list of wake up sounds :rtype: list of str :Example: wakeup_sounds = cls._get_random_wake_up_sounds(settings) .. seealso:: .. raises:: NullSettingException .. warnings:: Class Method and Private """ try: random_wake_up_sounds_list = settings["random_wake_up_sounds"] # In case files are declared in settings.yml, make sure kalliope can access them. for sound in random_wake_up_sounds_list: if Utils.get_real_file_path(sound) is None: raise SettingInvalidException("sound file %s not found" % sound) except KeyError: # User does not provide this settings return None # The the setting is present, the list cannot be empty if random_wake_up_sounds_list is None: raise NullSettingException( "random_wake_up_sounds settings is empty") return random_wake_up_sounds_list
def load_stt_correction_file(cls, stt_correction_file): stt_correction_file_path = Utils.get_real_file_path( stt_correction_file) stt_correction_file = open(stt_correction_file_path, "r") stt_correction = yaml.full_load(stt_correction_file) return stt_correction
def _get_file_template(cls, file_template, message_dict): real_file_template_path = Utils.get_real_file_path(file_template) if real_file_template_path is None: raise TemplateFileNotFoundException("Template file %s not found in templates folder" % real_file_template_path) # load the content of the file as template t = Template(cls._get_content_of_file(real_file_template_path)) returned_message = t.render(**message_dict) return returned_message
def _get_file_template(cls, file_template, message_dict): real_file_template_path = Utils.get_real_file_path(file_template) if real_file_template_path is None: raise TemplateFileNotFoundException("Template file %s not found in templates folder" % real_file_template_path) # load the content of the file as template t = Template(cls._get_content_of_file(real_file_template_path)) returned_message = t.render(**message_dict) return returned_message
def _get_on_ready_sounds(settings): """ Return the list of on_ready_sounds string from the settings. :param settings: The YAML settings file :type settings: dict :return: String parameter on_ready_sounds """ try: on_ready_sounds = settings["on_ready_sounds"] # In case files are declared in settings.yml, make sure kalliope can access them. for sound in on_ready_sounds: if Utils.get_real_file_path(sound) is None: raise SettingInvalidException("sound file %s not found" % sound) except KeyError: # User does not provide this settings return None return on_ready_sounds
def _get_file_template(cls, file_template, message_dict): real_file_template_path = Utils.get_real_file_path(file_template) if real_file_template_path is None: raise TemplateFileNotFoundException( "Template file %s not found in templates folder" % real_file_template_path) # load the content of the file as template t = Template(cls._get_content_of_file(real_file_template_path)) # add kalliope memory final_message_dict = dict() final_message_dict["kalliope_memory"] = Cortex.get_memory() if message_dict: final_message_dict.update(**message_dict) returned_message = t.render(final_message_dict) return returned_message
def _get_variables(settings): """ Return the dict of variables from the settings. :param settings: The YAML settings file :return: dict """ variables = dict() try: variables_files_name = settings["var_files"] # In case files are declared in settings.yml, make sure kalliope can access them. for files in variables_files_name: var = Utils.get_real_file_path(files) if var is None: raise SettingInvalidException("Variables file %s not found" % files) else: variables.update(YAMLLoader.get_config(var)) return variables except KeyError: # User does not provide this settings return dict()
def test_get_real_file_path(self): """ Expect to load the proper file following the order : - Provided absolute path - Current user path + file_name - /etc/kalliope + file_name - /path/to/kalliope/ +file_name """ ### # Test the absolute path dir_path = "/tmp/kalliope/tests/" file_name = "test_real_file_path" absolute_path_to_test = os.path.join(dir_path, file_name) expected_result = absolute_path_to_test if not os.path.exists(dir_path): os.makedirs(dir_path) # touch the file open(absolute_path_to_test, 'a').close() self.assertEqual(Utils.get_real_file_path(absolute_path_to_test), expected_result, "Fail to match the given absolute path ") # Clean up if os.path.exists(absolute_path_to_test): os.remove(absolute_path_to_test) ### # test the Current path file_name = "test_real_file_path" expected_result = os.getcwd() + os.sep + file_name # touch the file open(file_name, 'a').close() self.assertEqual(Utils.get_real_file_path(file_name), expected_result, "Fail to match the Current path ") # Clean up if os.path.exists(file_name): os.remove(file_name) ### # test /etc/kalliope # /!\ need permissions # dir_path = "/etc/kalliope/" # file_name = "test_real_file_path" # path_to_test = os.path.join(dir_path,file_name) # expected_result = "/etc/kalliope" + os.sep + file_name # if not os.path.exists(dir_path): # os.makedirs(dir_path) # # # touch the file # open(path_to_test, 'a').close() # # self.assertEquals(Utils.get_real_file_path(file_name), # expected_result, # "Fail to match the /etc/kalliope path") # # Clean up # if os.path.exists(file_name): # os.remove(file_name) ### # /an/unknown/path/kalliope/ dir_path = "../kalliope/" file_name = "test_real_file_path" path_to_test = os.path.join(dir_path, file_name) expected_result = os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep + "kalliope" + os.sep + file_name) if not os.path.exists(dir_path): os.makedirs(dir_path) # touch the file open(path_to_test, 'a').close() self.assertEqual(Utils.get_real_file_path(file_name), expected_result, "Fail to match the /an/unknown/path/kalliope path") # Clean up if os.path.exists(expected_result): os.remove(expected_result)
def load_stt_correction_file(cls, stt_correction_file): stt_correction_file_path = Utils.get_real_file_path(stt_correction_file) stt_correction_file = open(stt_correction_file_path, "r") stt_correction = yaml.load(stt_correction_file) return stt_correction
def test_get_real_file_path(self): """ Expect to load the proper file following the order : - Provided absolute path - Current user path + file_name - /etc/kalliope + file_name - /path/to/kalliope/ +file_name """ ### # Test the absolute path dir_path = "/tmp/kalliope/tests/" file_name = "test_real_file_path" absolute_path_to_test = os.path.join(dir_path, file_name) expected_result = absolute_path_to_test if not os.path.exists(dir_path): os.makedirs(dir_path) # touch the file open(absolute_path_to_test, 'a').close() self.assertEqual(Utils.get_real_file_path(absolute_path_to_test), expected_result, "Fail to match the given absolute path ") # Clean up if os.path.exists(absolute_path_to_test): os.remove(absolute_path_to_test) ### # test the Current path file_name = "test_real_file_path" expected_result = os.getcwd() + os.sep + file_name # touch the file open(file_name, 'a').close() self.assertEqual(Utils.get_real_file_path(file_name), expected_result, "Fail to match the Current path ") # Clean up if os.path.exists(file_name): os.remove(file_name) ### # test /etc/kalliope # /!\ need permissions # dir_path = "/etc/kalliope/" # file_name = "test_real_file_path" # path_to_test = os.path.join(dir_path,file_name) # expected_result = "/etc/kalliope" + os.sep + file_name # if not os.path.exists(dir_path): # os.makedirs(dir_path) # # # touch the file # open(path_to_test, 'a').close() # # self.assertEquals(Utils.get_real_file_path(file_name), # expected_result, # "Fail to match the /etc/kalliope path") # # Clean up # if os.path.exists(file_name): # os.remove(file_name) ### # /an/unknown/path/kalliope/ dir_path = "../kalliope/" file_name = "test_real_file_path" path_to_test = os.path.join(dir_path, file_name) expected_result = os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep + "kalliope" + os.sep + file_name) if not os.path.exists(dir_path): os.makedirs(dir_path) # touch the file open(path_to_test, 'a').close() self.assertEqual(Utils.get_real_file_path(file_name), expected_result, "Fail to match the /an/unknown/path/kalliope path") # Clean up if os.path.exists(expected_result): os.remove(expected_result)