ConfigurationManager.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from BrainLoader import BrainLoader
  2. from SettingLoader import SettingLoader
  3. import logging
  4. class DefaultSpeechToTextNotFound(Exception):
  5. pass
  6. class DefaultSpeechNull(Exception):
  7. pass
  8. class NoSpeechToTextConfiguration(Exception):
  9. pass
  10. class ConfigurationManager:
  11. def __init__(self):
  12. pass
  13. @classmethod
  14. def get_brain(cls, brain_file_name=None):
  15. if brain_file_name is None:
  16. brain_file_name = "brain.yml"
  17. return BrainLoader(brain_file_name).get_config()
  18. @classmethod
  19. def get_settings(cls, setting_file_name=None):
  20. if setting_file_name is None:
  21. setting_file_name = "settings.yml"
  22. return SettingLoader(setting_file_name).get_config()
  23. @classmethod
  24. def get_default_speech_to_text(cls):
  25. settings = cls.get_settings()
  26. try:
  27. default_speech_to_text = settings["default_speech_to_text"]
  28. if default_speech_to_text is None:
  29. raise DefaultSpeechNull("Attribute default_speech_to_text is null")
  30. logging.info("Default STT: %s" % default_speech_to_text)
  31. return default_speech_to_text
  32. except KeyError:
  33. raise DefaultSpeechToTextNotFound("Attribute default_speech_to_text not found in settings")
  34. @classmethod
  35. def get_stt_args(cls, default_stt_plugin_name):
  36. """
  37. Return argument set for the current STT engine
  38. :param default_stt_plugin_name: Name of the STT engine
  39. :return:
  40. """
  41. def find(lst, key):
  42. """
  43. Find a key name in a list
  44. :param lst: list()
  45. :param key: key name to find i the list
  46. :return: Return the dict
  47. """
  48. for el in lst:
  49. try:
  50. if el[key]:
  51. return el[key]
  52. except TypeError:
  53. pass
  54. except KeyError:
  55. pass
  56. return None
  57. settings = cls.get_settings()
  58. try:
  59. speechs_to_text = settings["speech_to_text"]
  60. except KeyError:
  61. raise NoSpeechToTextConfiguration("No speech_to_text in settings")
  62. logging.debug("Settings file content: %s" % speechs_to_text)
  63. # get args
  64. args = find(speechs_to_text, default_stt_plugin_name)
  65. logging.debug("Args for %s STT: %s" % (default_stt_plugin_name, args))
  66. return args