SettingLoader.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. import logging
  2. import os
  3. from six import with_metaclass
  4. from kalliope.core.Models.RpiSettings import RpiSettings
  5. from kalliope.core.Models.RecognitionOptions import RecognitionOptions
  6. from .YAMLLoader import YAMLLoader
  7. from kalliope.core.Models.Resources import Resources
  8. from kalliope.core.Utils.Utils import Utils
  9. from kalliope.core.Models import Singleton
  10. from kalliope.core.Models.RestAPI import RestAPI
  11. from kalliope.core.Models.Settings import Settings
  12. from kalliope.core.Models.Stt import Stt
  13. from kalliope.core.Models.Trigger import Trigger
  14. from kalliope.core.Models.Player import Player
  15. from kalliope.core.Models.Tts import Tts
  16. from kalliope.core.Utils.FileManager import FileManager
  17. FILE_NAME = "settings.yml"
  18. logging.basicConfig()
  19. logger = logging.getLogger("kalliope")
  20. class SettingInvalidException(Exception):
  21. """
  22. Some data must match the expected value/type
  23. .. seealso:: Settings
  24. """
  25. pass
  26. class NullSettingException(Exception):
  27. """
  28. Some Attributes can not be Null
  29. .. seealso:: Settings
  30. """
  31. pass
  32. class SettingNotFound(Exception):
  33. """
  34. Some Attributes are missing
  35. .. seealso:: Settings
  36. """
  37. pass
  38. class SettingLoader(with_metaclass(Singleton, object)):
  39. """
  40. This Class is used to get the Settings YAML and the Settings as an object
  41. """
  42. def __init__(self, file_path=None):
  43. self.file_path = file_path
  44. if self.file_path is None:
  45. self.file_path = Utils.get_real_file_path(FILE_NAME)
  46. else:
  47. self.file_path = Utils.get_real_file_path(file_path)
  48. # if the returned file path is none, the file doesn't exist
  49. if self.file_path is None:
  50. raise SettingNotFound("Settings.yml file not found")
  51. self.yaml_config = self._get_yaml_config()
  52. self.settings = self._get_settings()
  53. def _get_yaml_config(self):
  54. """
  55. Class Methods which loads default or the provided YAML file and return it as a String
  56. :return: The loaded settings YAML
  57. :rtype: dict
  58. :Example:
  59. settings_yaml = SettingLoader.get_yaml_config(/var/tmp/settings.yml)
  60. .. warnings:: Class Method
  61. """
  62. return YAMLLoader.get_config(self.file_path)
  63. def _get_settings(self):
  64. """
  65. Class Methods which loads default or the provided YAML file and return a Settings Object
  66. :return: The loaded Settings
  67. :rtype: Settings
  68. :Example:
  69. settings = SettingLoader.get_settings(file_path="/var/tmp/settings.yml")
  70. .. seealso:: Settings
  71. .. warnings:: Class Method
  72. """
  73. # create a new setting
  74. setting_object = Settings()
  75. # Get the setting parameters
  76. settings = self._get_yaml_config()
  77. default_stt_name = self._get_default_speech_to_text(settings)
  78. default_tts_name = self._get_default_text_to_speech(settings)
  79. default_trigger_name = self._get_default_trigger(settings)
  80. default_player_name = self._get_default_player(settings)
  81. stts = self._get_stts(settings)
  82. ttss = self._get_ttss(settings)
  83. triggers = self._get_triggers(settings)
  84. players = self._get_players(settings)
  85. random_wake_up_answers = self._get_random_wake_up_answers(settings)
  86. random_wake_up_sound = self._get_random_wake_up_sounds(settings)
  87. play_on_ready_notification = self._get_play_on_ready_notification(settings)
  88. on_ready_answers = self._get_on_ready_answers(settings)
  89. on_ready_sounds = self._get_on_ready_sounds(settings)
  90. rest_api = self._get_rest_api(settings)
  91. cache_path = self._get_cache_path(settings)
  92. default_synapse = self._get_default_synapse(settings)
  93. resources = self._get_resources(settings)
  94. variables = self._get_variables(settings)
  95. rpi_settings = self._get_rpi_settings(settings)
  96. recognition_options = self._get_recognition_options(settings)
  97. # Load the setting singleton with the parameters
  98. setting_object.default_tts_name = default_tts_name
  99. setting_object.default_stt_name = default_stt_name
  100. setting_object.default_trigger_name = default_trigger_name
  101. setting_object.default_player_name = default_player_name
  102. setting_object.stts = stts
  103. setting_object.ttss = ttss
  104. setting_object.triggers = triggers
  105. setting_object.players = players
  106. setting_object.random_wake_up_answers = random_wake_up_answers
  107. setting_object.random_wake_up_sounds = random_wake_up_sound
  108. setting_object.play_on_ready_notification = play_on_ready_notification
  109. setting_object.on_ready_answers = on_ready_answers
  110. setting_object.on_ready_sounds = on_ready_sounds
  111. setting_object.rest_api = rest_api
  112. setting_object.cache_path = cache_path
  113. setting_object.default_synapse = default_synapse
  114. setting_object.resources = resources
  115. setting_object.variables = variables
  116. setting_object.rpi_settings = rpi_settings
  117. setting_object.recognition_options = recognition_options
  118. return setting_object
  119. @staticmethod
  120. def _get_default_speech_to_text(settings):
  121. """
  122. Get the default speech to text defined in the settings.yml file
  123. :param settings: The YAML settings file
  124. :type settings: dict
  125. :return: the default speech to text
  126. :rtype: str
  127. :Example:
  128. default_stt_name = cls._get_default_speech_to_text(settings)
  129. .. seealso:: Stt
  130. .. raises:: NullSettingException, SettingNotFound
  131. .. warnings:: Static and Private
  132. """
  133. try:
  134. default_speech_to_text = settings["default_speech_to_text"]
  135. if default_speech_to_text is None:
  136. raise NullSettingException("Attribute default_speech_to_text is null")
  137. logger.debug("Default STT: %s" % default_speech_to_text)
  138. return default_speech_to_text
  139. except KeyError as e:
  140. raise SettingNotFound("%s setting not found" % e)
  141. @staticmethod
  142. def _get_default_text_to_speech(settings):
  143. """
  144. Get the default text to speech defined in the settings.yml file
  145. :param settings: The YAML settings file
  146. :type settings: dict
  147. :return: the default text to speech
  148. :rtype: str
  149. :Example:
  150. default_tts_name = cls._get_default_text_to_speech(settings)
  151. .. seealso:: Tts
  152. .. raises:: NullSettingException, SettingNotFound
  153. .. warnings:: Static and Private
  154. """
  155. try:
  156. default_text_to_speech = settings["default_text_to_speech"]
  157. if default_text_to_speech is None:
  158. raise NullSettingException("Attribute default_text_to_speech is null")
  159. logger.debug("Default TTS: %s" % default_text_to_speech)
  160. return default_text_to_speech
  161. except KeyError as e:
  162. raise SettingNotFound("%s setting not found" % e)
  163. @staticmethod
  164. def _get_default_trigger(settings):
  165. """
  166. Get the default trigger defined in the settings.yml file
  167. :param settings: The YAML settings file
  168. :type settings: dict
  169. :return: the default trigger
  170. :rtype: str
  171. :Example:
  172. default_trigger_name = cls._get_default_trigger(settings)
  173. .. seealso:: Trigger
  174. .. raises:: NullSettingException, SettingNotFound
  175. .. warnings:: Static and Private
  176. """
  177. try:
  178. default_trigger = settings["default_trigger"]
  179. if default_trigger is None:
  180. raise NullSettingException("Attribute default_trigger is null")
  181. logger.debug("Default Trigger name: %s" % default_trigger)
  182. return default_trigger
  183. except KeyError as e:
  184. raise SettingNotFound("%s setting not found" % e)
  185. @staticmethod
  186. def _get_default_player(settings):
  187. """
  188. Get the default player defined in the settings.yml file
  189. :param settings: The YAML settings file
  190. :type settings: dict
  191. :return: the default player
  192. :rtype: str
  193. :Example:
  194. default_player_name = cls._get_default_player(settings)
  195. .. seealso:: Player
  196. .. raises:: NullSettingException, SettingNotFound
  197. .. warnings:: Static and Private
  198. """
  199. try:
  200. default_player = settings["default_player"]
  201. if default_player is None:
  202. raise NullSettingException("Attribute default_player is null")
  203. logger.debug("Default Player name: %s" % default_player)
  204. return default_player
  205. except KeyError as e:
  206. raise SettingNotFound("%s setting not found" % e)
  207. @staticmethod
  208. def _get_stts(settings):
  209. """
  210. Return a list of stt object
  211. :param settings: The YAML settings file
  212. :type settings: dict
  213. :return: List of Stt
  214. :rtype: list
  215. :Example:
  216. stts = cls._get_stts(settings)
  217. .. seealso:: Stt
  218. .. raises:: SettingNotFound
  219. .. warnings:: Static Method and Private
  220. """
  221. try:
  222. speechs_to_text_list = settings["speech_to_text"]
  223. except KeyError:
  224. raise SettingNotFound("speech_to_text settings not found")
  225. stts = list()
  226. for speechs_to_text_el in speechs_to_text_list:
  227. if isinstance(speechs_to_text_el, dict):
  228. for stt_name in speechs_to_text_el:
  229. name = stt_name
  230. parameters = speechs_to_text_el[name]
  231. new_stt = Stt(name=name, parameters=parameters)
  232. stts.append(new_stt)
  233. else:
  234. # the stt does not have parameter
  235. new_stt = Stt(name=speechs_to_text_el, parameters=dict())
  236. stts.append(new_stt)
  237. return stts
  238. @staticmethod
  239. def _get_ttss(settings):
  240. """
  241. Return a list of stt object
  242. :param settings: The YAML settings file
  243. :type settings: dict
  244. :return: List of Ttss
  245. :rtype: list
  246. :Example:
  247. ttss = cls._get_ttss(settings)
  248. .. seealso:: Tts
  249. .. raises:: SettingNotFound
  250. .. warnings:: Static Method and Private
  251. """
  252. try:
  253. text_to_speech_list = settings["text_to_speech"]
  254. except KeyError as e:
  255. raise SettingNotFound("%s setting not found" % e)
  256. ttss = list()
  257. for text_to_speech_el in text_to_speech_list:
  258. if isinstance(text_to_speech_el, dict):
  259. for tts_name in text_to_speech_el:
  260. name = tts_name
  261. parameters = text_to_speech_el[name]
  262. new_tts = Tts(name=name, parameters=parameters)
  263. ttss.append(new_tts)
  264. else:
  265. # the neuron does not have parameter
  266. new_tts = Tts(name=text_to_speech_el)
  267. ttss.append(new_tts)
  268. return ttss
  269. @staticmethod
  270. def _get_triggers(settings):
  271. """
  272. Return a list of Trigger object
  273. :param settings: The YAML settings file
  274. :type settings: dict
  275. :return: List of Trigger
  276. :rtype: list
  277. :Example:
  278. triggers = cls._get_triggers(settings)
  279. .. seealso:: Trigger
  280. .. raises:: SettingNotFound
  281. .. warnings:: Static Method and Private
  282. """
  283. try:
  284. triggers_list = settings["triggers"]
  285. except KeyError as e:
  286. raise SettingNotFound("%s setting not found" % e)
  287. triggers = list()
  288. for trigger_el in triggers_list:
  289. if isinstance(trigger_el, dict):
  290. for trigger_name in trigger_el:
  291. name = trigger_name
  292. parameters = trigger_el[name]
  293. new_trigger = Trigger(name=name, parameters=parameters)
  294. triggers.append(new_trigger)
  295. else:
  296. # the neuron does not have parameter
  297. new_trigger = Trigger(name=trigger_el)
  298. triggers.append(new_trigger)
  299. return triggers
  300. @staticmethod
  301. def _get_players(settings):
  302. """
  303. Return a list of Player object
  304. :param settings: The YAML settings file
  305. :type settings: dict
  306. :return: List of Player
  307. :rtype: list
  308. :Example:
  309. players = cls._get_players(settings)
  310. .. seealso:: players
  311. .. raises:: SettingNotFound
  312. .. warnings:: Static Method and Private
  313. """
  314. try:
  315. players_list = settings["players"]
  316. except KeyError as e:
  317. raise SettingNotFound("%s setting not found" % e)
  318. players = list()
  319. for player_el in players_list:
  320. if isinstance(player_el, dict):
  321. for player_name in player_el:
  322. name = player_name
  323. parameters = player_el[name]
  324. new_player = Player(name=name, parameters=parameters)
  325. players.append(new_player)
  326. else:
  327. # the player does not have parameters
  328. new_player = Player(name=player_el)
  329. players.append(new_player)
  330. return players
  331. @staticmethod
  332. def _get_random_wake_up_answers(settings):
  333. """
  334. Return a list of the wake up answers set up on the settings.yml file
  335. :param settings: The YAML settings file
  336. :type settings: dict
  337. :return: List of wake up answers
  338. :rtype: list of str
  339. :Example:
  340. wakeup = cls._get_random_wake_up_answers(settings)
  341. .. seealso::
  342. .. raises:: NullSettingException
  343. .. warnings:: Class Method and Private
  344. """
  345. try:
  346. random_wake_up_answers_list = settings["random_wake_up_answers"]
  347. except KeyError:
  348. # User does not provide this settings
  349. return None
  350. # The list cannot be empty
  351. if random_wake_up_answers_list is None:
  352. raise NullSettingException("random_wake_up_answers settings is null")
  353. return random_wake_up_answers_list
  354. @staticmethod
  355. def _get_random_wake_up_sounds(settings):
  356. """
  357. Return a list of the wake up sounds set up on the settings.yml file
  358. :param settings: The YAML settings file
  359. :type settings: dict
  360. :return: list of wake up sounds
  361. :rtype: list of str
  362. :Example:
  363. wakeup_sounds = cls._get_random_wake_up_sounds(settings)
  364. .. seealso::
  365. .. raises:: NullSettingException
  366. .. warnings:: Class Method and Private
  367. """
  368. try:
  369. random_wake_up_sounds_list = settings["random_wake_up_sounds"]
  370. # In case files are declared in settings.yml, make sure kalliope can access them.
  371. for sound in random_wake_up_sounds_list:
  372. if Utils.get_real_file_path(sound) is None:
  373. raise SettingInvalidException("sound file %s not found" % sound)
  374. except KeyError:
  375. # User does not provide this settings
  376. return None
  377. # The the setting is present, the list cannot be empty
  378. if random_wake_up_sounds_list is None:
  379. raise NullSettingException("random_wake_up_sounds settings is empty")
  380. return random_wake_up_sounds_list
  381. @staticmethod
  382. def _get_rest_api(settings):
  383. """
  384. Return the settings of the RestApi
  385. :param settings: The YAML settings file
  386. :type settings: dict
  387. :return: the RestApi object
  388. :rtype: RestApi
  389. :Example:
  390. rest_api = cls._get_rest_api(settings)
  391. .. seealso:: RestApi
  392. .. raises:: SettingNotFound, NullSettingException, SettingInvalidException
  393. .. warnings:: Class Method and Private
  394. """
  395. try:
  396. rest_api = settings["rest_api"]
  397. except KeyError as e:
  398. raise SettingNotFound("%s setting not found" % e)
  399. if rest_api is not None:
  400. try:
  401. password_protected = rest_api["password_protected"]
  402. if password_protected is None:
  403. raise NullSettingException("password_protected setting cannot be null")
  404. login = rest_api["login"]
  405. password = rest_api["password"]
  406. if password_protected:
  407. if login is None:
  408. raise NullSettingException("login setting cannot be null if password_protected is True")
  409. if password is None:
  410. raise NullSettingException("password setting cannot be null if password_protected is True")
  411. active = rest_api["active"]
  412. if active is None:
  413. raise NullSettingException("active setting cannot be null")
  414. port = rest_api["port"]
  415. if port is None:
  416. raise NullSettingException("port setting cannot be null")
  417. # check that the port in an integer
  418. try:
  419. port = int(port)
  420. except ValueError:
  421. raise SettingInvalidException("port must be an integer")
  422. # check the port is a valid port number
  423. if not 1024 <= port <= 65535:
  424. raise SettingInvalidException("port must be in range 1024-65535")
  425. # check the CORS request settings
  426. allowed_cors_origin = False
  427. if "allowed_cors_origin" in rest_api:
  428. allowed_cors_origin = rest_api["allowed_cors_origin"]
  429. except KeyError as e:
  430. raise SettingNotFound("%s settings not found" % e)
  431. # config ok, we can return the rest api object
  432. rest_api_obj = RestAPI(password_protected=password_protected, login=login, password=password,
  433. active=active, port=port, allowed_cors_origin=allowed_cors_origin)
  434. return rest_api_obj
  435. else:
  436. raise NullSettingException("rest_api settings cannot be null")
  437. @staticmethod
  438. def _get_cache_path(settings):
  439. """
  440. Return the path where to store the cache
  441. :param settings: The YAML settings file
  442. :type settings: dict
  443. :return: the path to store the cache
  444. :rtype: String
  445. :Example:
  446. cache_path = cls._get_cache_path(settings)
  447. .. seealso::
  448. .. raises:: SettingNotFound, NullSettingException, SettingInvalidException
  449. .. warnings:: Class Method and Private
  450. """
  451. try:
  452. cache_path = settings["cache_path"]
  453. except KeyError as e:
  454. raise SettingNotFound("%s setting not found" % e)
  455. if cache_path is None:
  456. raise NullSettingException("cache_path setting cannot be null")
  457. # test if that path is usable
  458. if FileManager.is_path_exists_or_creatable(cache_path):
  459. return cache_path
  460. else:
  461. raise SettingInvalidException("The cache_path seems to be invalid: %s" % cache_path)
  462. @staticmethod
  463. def _get_default_synapse(settings):
  464. """
  465. Return the name of the default synapse
  466. :param settings: The YAML settings file
  467. :type settings: dict
  468. :return: the default synapse name
  469. :rtype: String
  470. :Example:
  471. default_synapse = cls._get_default_synapse(settings)
  472. .. seealso::
  473. .. raises:: SettingNotFound, NullSettingException, SettingInvalidException
  474. .. warnings:: Class Method and Private
  475. """
  476. try:
  477. default_synapse = settings["default_synapse"]
  478. logger.debug("Default synapse: %s" % default_synapse)
  479. except KeyError:
  480. default_synapse = None
  481. return default_synapse
  482. @staticmethod
  483. def _get_resources(settings):
  484. """
  485. Return a resources object that contains path of third party modules
  486. :param settings: The YAML settings file
  487. :type settings: dict
  488. :return: the resource object
  489. :rtype: Resources
  490. :Example:
  491. resource_directory = cls._get_resource_dir(settings)
  492. .. seealso::
  493. .. raises:: SettingNotFound, NullSettingException, SettingInvalidException
  494. .. warnings:: Class Method and Private
  495. """
  496. # return an empty resource object anyway
  497. resource_object = Resources()
  498. try:
  499. resource_dir = settings["resource_directory"]
  500. logger.debug("Resource directory synapse: %s" % resource_dir)
  501. neuron_folder = None
  502. stt_folder = None
  503. tts_folder = None
  504. trigger_folder = None
  505. signal_folder = None
  506. if "neuron" in resource_dir:
  507. neuron_folder = resource_dir["neuron"]
  508. if os.path.exists(neuron_folder):
  509. logger.debug("[SettingLoader] Neuron resource folder path loaded: %s" % neuron_folder)
  510. resource_object.neuron_folder = neuron_folder
  511. else:
  512. raise SettingInvalidException("The path %s does not exist on the system" % neuron_folder)
  513. if "stt" in resource_dir:
  514. stt_folder = resource_dir["stt"]
  515. if os.path.exists(stt_folder):
  516. logger.debug("[SettingLoader] STT resource folder path loaded: %s" % stt_folder)
  517. resource_object.stt_folder = stt_folder
  518. else:
  519. raise SettingInvalidException("The path %s does not exist on the system" % stt_folder)
  520. if "tts" in resource_dir:
  521. tts_folder = resource_dir["tts"]
  522. if os.path.exists(tts_folder):
  523. logger.debug("[SettingLoader] TTS resource folder path loaded: %s" % tts_folder)
  524. resource_object.tts_folder = tts_folder
  525. else:
  526. raise SettingInvalidException("The path %s does not exist on the system" % tts_folder)
  527. if "trigger" in resource_dir:
  528. trigger_folder = resource_dir["trigger"]
  529. if os.path.exists(trigger_folder):
  530. logger.debug("[SettingLoader] Trigger resource folder path loaded: %s" % trigger_folder)
  531. resource_object.trigger_folder = trigger_folder
  532. else:
  533. raise SettingInvalidException("The path %s does not exist on the system" % trigger_folder)
  534. if "signal" in resource_dir:
  535. signal_folder = resource_dir["signal"]
  536. if os.path.exists(signal_folder):
  537. logger.debug("[SettingLoader] Signal resource folder path loaded: %s" % signal_folder)
  538. resource_object.signal_folder = signal_folder
  539. else:
  540. raise SettingInvalidException("The path %s does not exist on the system" % signal_folder)
  541. if neuron_folder is None \
  542. and stt_folder is None \
  543. and tts_folder is None \
  544. and trigger_folder is None \
  545. and signal_folder is None:
  546. raise SettingInvalidException("No required folder has been provided in the setting resource_directory. "
  547. "Define : \'neuron\' or/and \'stt\' or/and \'tts\' or/and \'trigger\' "
  548. "or/and \'signal\'")
  549. except KeyError:
  550. logger.debug("Resource directory not found in settings")
  551. return resource_object
  552. return resource_object
  553. @staticmethod
  554. def _get_play_on_ready_notification(settings):
  555. """
  556. Return the on_ready_notification setting. If the user didn't provided it the default is never
  557. :param settings: The YAML settings file
  558. :type settings: dict
  559. :return:
  560. """
  561. try:
  562. play_on_ready_notification = settings["play_on_ready_notification"]
  563. except KeyError:
  564. # User does not provide this settings, by default we set it to never
  565. play_on_ready_notification = "never"
  566. return play_on_ready_notification
  567. return play_on_ready_notification
  568. @staticmethod
  569. def _get_on_ready_answers(settings):
  570. """
  571. Return the list of on_ready_answers string from the settings.
  572. :param settings: The YAML settings file
  573. :type settings: dict
  574. :return: String parameter on_ready_answers
  575. """
  576. try:
  577. on_ready_answers = settings["on_ready_answers"]
  578. except KeyError:
  579. # User does not provide this settings
  580. return None
  581. return on_ready_answers
  582. @staticmethod
  583. def _get_on_ready_sounds(settings):
  584. """
  585. Return the list of on_ready_sounds string from the settings.
  586. :param settings: The YAML settings file
  587. :type settings: dict
  588. :return: String parameter on_ready_sounds
  589. """
  590. try:
  591. on_ready_sounds = settings["on_ready_sounds"]
  592. # In case files are declared in settings.yml, make sure kalliope can access them.
  593. for sound in on_ready_sounds:
  594. if Utils.get_real_file_path(sound) is None:
  595. raise SettingInvalidException("sound file %s not found" % sound)
  596. except KeyError:
  597. # User does not provide this settings
  598. return None
  599. return on_ready_sounds
  600. @staticmethod
  601. def _get_variables(settings):
  602. """
  603. Return the dict of variables from the settings.
  604. :param settings: The YAML settings file
  605. :return: dict
  606. """
  607. variables = dict()
  608. try:
  609. variables_files_name = settings["var_files"]
  610. # In case files are declared in settings.yml, make sure kalliope can access them.
  611. for files in variables_files_name:
  612. var = Utils.get_real_file_path(files)
  613. if var is None:
  614. raise SettingInvalidException("Variables file %s not found" % files)
  615. else:
  616. variables.update(YAMLLoader.get_config(var))
  617. return variables
  618. except KeyError:
  619. # User does not provide this settings
  620. return dict()
  621. @staticmethod
  622. def _get_rpi_settings(settings):
  623. """
  624. return RpiSettings object
  625. :param settings: The loaded YAML settings file
  626. :return:
  627. """
  628. try:
  629. rpi_settings_dict = settings["rpi"]
  630. rpi_settings = RpiSettings()
  631. # affect pin if there are declared
  632. if "pin_mute_button" in rpi_settings_dict:
  633. rpi_settings.pin_mute_button = rpi_settings_dict["pin_mute_button"]
  634. if "pin_led_started" in rpi_settings_dict:
  635. rpi_settings.pin_led_started = rpi_settings_dict["pin_led_started"]
  636. if "pin_led_muted" in rpi_settings_dict:
  637. rpi_settings.pin_led_muted = rpi_settings_dict["pin_led_muted"]
  638. if "pin_led_talking" in rpi_settings_dict:
  639. rpi_settings.pin_led_talking = rpi_settings_dict["pin_led_talking"]
  640. if "pin_led_listening" in rpi_settings_dict:
  641. rpi_settings.pin_led_listening = rpi_settings_dict["pin_led_listening"]
  642. return rpi_settings
  643. except KeyError:
  644. logger.debug("[SettingsLoader] No Rpi config")
  645. return None
  646. @staticmethod
  647. def _get_recognition_options(settings):
  648. """
  649. return the value of stt_threshold
  650. :param settings: The loaded YAML settings file
  651. :return: integer or 1200 by default if not set
  652. """
  653. recognition_options = RecognitionOptions()
  654. try:
  655. recognition_options_dict = settings["RecognitionOptions"]
  656. if "energy_threshold" in recognition_options_dict:
  657. recognition_options.energy_threshold = recognition_options_dict["energy_threshold"]
  658. logger.debug("[SettingsLoader] energy_threshold set to %s" % recognition_options.energy_threshold)
  659. if "adjust_for_ambient_noise_second" in recognition_options_dict:
  660. recognition_options.adjust_for_ambient_noise_second = recognition_options_dict["adjust_for_ambient_noise_second"]
  661. logger.debug("[SettingsLoader] adjust_for_ambient_noise_second set to %s"
  662. % recognition_options.adjust_for_ambient_noise_second)
  663. return recognition_options
  664. except KeyError:
  665. logger.debug("[SettingsLoader] no recognition_options defined. Set to default")
  666. logger.debug("[SettingsLoader] recognition_options: %s" % str(recognition_options))
  667. return recognition_options