SettingLoader.py 10 KB

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