SettingLoader.py 29 KB

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