SettingLoader.py 10 KB

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