SettingLoader.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from YAMLLoader import YAMLLoader
  2. import logging
  3. from core.Models.RestAPI import RestAPI
  4. from core.Models.Settings import Settings
  5. from core.Models.Stt import Stt
  6. from core.Models.Trigger import Trigger
  7. from core.Models.Tts import Tts
  8. FILE_NAME = "settings.yml"
  9. logging.basicConfig()
  10. logger = logging.getLogger("kalliope")
  11. class NullSettingException(Exception):
  12. pass
  13. class SettingNotFound(Exception):
  14. pass
  15. class SettingLoader(object):
  16. def __init__(self):
  17. pass
  18. @classmethod
  19. def get_yaml_config(cls, file_path=None):
  20. if file_path is None:
  21. file_path = FILE_NAME
  22. return YAMLLoader.get_config(file_path)
  23. @classmethod
  24. def get_settings(cls, file_path=None):
  25. """
  26. Return a Settings object from settings.yml file
  27. :return:
  28. """
  29. settings = cls.get_yaml_config(file_path)
  30. default_stt_name = cls._get_default_speech_to_text(settings)
  31. default_tts_name = cls._get_default_text_to_speech(settings)
  32. default_trigger_name = cls._get_default_trigger(settings)
  33. stts = cls._get_stts(settings)
  34. ttss = cls._get_ttss(settings)
  35. triggers = cls._get_triggers(settings)
  36. random_wake_up_answers = cls._get_random_wake_up_answers(settings)
  37. random_wake_up_sounds = cls._get_random_wake_up_sounds(settings)
  38. rest_api = cls._get_rest_api(settings)
  39. # create a setting object
  40. setting_object = Settings(default_stt_name=default_stt_name,
  41. default_tts_name=default_tts_name,
  42. default_trigger_name=default_trigger_name,
  43. stts=stts,
  44. ttss=ttss,
  45. triggers=triggers,
  46. random_wake_up_answers=random_wake_up_answers,
  47. random_wake_up_sounds=random_wake_up_sounds,
  48. rest_api=rest_api)
  49. return setting_object
  50. @staticmethod
  51. def _get_default_speech_to_text(settings):
  52. try:
  53. default_speech_to_text = settings["default_speech_to_text"]
  54. if default_speech_to_text is None:
  55. raise NullSettingException("Attribute default_speech_to_text is null")
  56. logger.debug("Default STT: %s" % default_speech_to_text)
  57. return default_speech_to_text
  58. except KeyError:
  59. raise SettingNotFound("Attribute default_speech_to_text not found in settings")
  60. @staticmethod
  61. def _get_default_text_to_speech(settings):
  62. try:
  63. default_text_to_speech = settings["default_text_to_speech"]
  64. if default_text_to_speech is None:
  65. raise NullSettingException("Attribute default_text_to_speech is null")
  66. logger.debug("Default TTS: %s" % default_text_to_speech)
  67. return default_text_to_speech
  68. except KeyError:
  69. raise SettingNotFound("Attribute default_text_to_speech not found in settings")
  70. @staticmethod
  71. def _get_default_trigger(settings):
  72. try:
  73. default_trigger = settings["default_trigger"]
  74. if default_trigger is None:
  75. raise NullSettingException("Attribute default_trigger is null")
  76. logger.debug("Default Trigger name: %s" % default_trigger)
  77. return default_trigger
  78. except KeyError:
  79. raise SettingNotFound("Attribute default_trigger not found in settings")
  80. @classmethod
  81. def _get_stts(cls, settings):
  82. """
  83. Return a list of stt object
  84. :param settings: loaded settings file
  85. :return: List of Stt
  86. """
  87. try:
  88. speechs_to_text_list = settings["speech_to_text"]
  89. except KeyError:
  90. raise NullSettingException("speech_to_text settings not found")
  91. stts = list()
  92. for speechs_to_text_el in speechs_to_text_list:
  93. if isinstance(speechs_to_text_el, dict):
  94. # print "Neurons dict ok"
  95. for stt_name in speechs_to_text_el:
  96. name = stt_name
  97. parameters = speechs_to_text_el[name]
  98. new_stt = Stt(name=name, parameters=parameters)
  99. stts.append(new_stt)
  100. else:
  101. # the neuron does not have parameter
  102. new_stt = Stt(name=speechs_to_text_el)
  103. stts.append(new_stt)
  104. return stts
  105. @classmethod
  106. def _get_ttss(cls, settings):
  107. """
  108. Return a list of Tts object
  109. :param settings: loaded settings file
  110. :return: List of Tts
  111. """
  112. try:
  113. text_to_speech_list = settings["text_to_speech"]
  114. except KeyError:
  115. raise SettingNotFound("text_to_speech settings not found")
  116. ttss = list()
  117. for text_to_speech_el in text_to_speech_list:
  118. if isinstance(text_to_speech_el, dict):
  119. # print "Neurons dict ok"
  120. for tts_name in text_to_speech_el:
  121. name = tts_name
  122. parameters = text_to_speech_el[name]
  123. new_tts = Tts(name=name, parameters=parameters)
  124. ttss.append(new_tts)
  125. else:
  126. # the neuron does not have parameter
  127. new_tts = Tts(name=text_to_speech_el)
  128. ttss.append(new_tts)
  129. return ttss
  130. @classmethod
  131. def _get_triggers(cls, settings):
  132. """
  133. Return a list of Trigger object
  134. :param settings: loaded settings file
  135. :return: List of Trigger
  136. """
  137. try:
  138. triggers_list = settings["triggers"]
  139. except KeyError:
  140. raise SettingNotFound("text_to_speech settings not found")
  141. triggers = list()
  142. for trigger_el in triggers_list:
  143. if isinstance(trigger_el, dict):
  144. # print "Neurons dict ok"
  145. for tts_name in trigger_el:
  146. name = tts_name
  147. parameters = trigger_el[name]
  148. new_tts = Trigger(name=name, parameters=parameters)
  149. triggers.append(new_tts)
  150. else:
  151. # the neuron does not have parameter
  152. new_tts = Trigger(name=trigger_el)
  153. triggers.append(new_tts)
  154. return triggers
  155. @classmethod
  156. def _get_random_wake_up_answers(cls, settings):
  157. """
  158. return a list of string
  159. :param settings:
  160. :return:
  161. """
  162. try:
  163. random_wake_up_answers_list = settings["random_wake_up_answers"]
  164. except KeyError:
  165. # User does not provide this settings
  166. return None
  167. # The list cannot be empty
  168. if random_wake_up_answers_list is None:
  169. raise NullSettingException("random_wake_up_answers settings is null")
  170. return random_wake_up_answers_list
  171. @classmethod
  172. def _get_random_wake_up_sounds(cls, settings):
  173. """
  174. return a list of string
  175. :param settings:
  176. :return: List of string
  177. """
  178. try:
  179. random_wake_up_sounds_list = settings["random_wake_up_sounds"]
  180. except KeyError:
  181. # User does not provide this settings
  182. return None
  183. # The the setting is present, the list cannot be empty
  184. if random_wake_up_sounds_list is None:
  185. raise NullSettingException("random_wake_up_sounds settings is empty")
  186. return random_wake_up_sounds_list
  187. @classmethod
  188. def _get_rest_api(cls, settings):
  189. try:
  190. rest_api = settings["rest_api"]
  191. except KeyError:
  192. return None
  193. if rest_api is not None:
  194. password_protected = rest_api["password_protected"]
  195. login = rest_api["login"]
  196. password = rest_api["password"]
  197. active = rest_api["active"]
  198. rest_api_obj = RestAPI(password_protected=password_protected, login=login, password=password, active=active)
  199. return rest_api_obj