SettingLoader.py 10 KB

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