ConfigurationManager.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_default_text_to_speech(cls):
  36. settings = cls.get_settings()
  37. try:
  38. default_text_to_speech = settings["default_text_to_speech"]
  39. if default_text_to_speech is None:
  40. raise DefaultSpeechNull("Attribute default_text_to_speech is null")
  41. logging.info("Default TTS: %s" % default_text_to_speech)
  42. return default_text_to_speech
  43. except KeyError:
  44. raise DefaultSpeechToTextNotFound("Attribute default_text_to_speech not found in settings")
  45. @classmethod
  46. def get_stt_args(cls, default_stt_plugin_name):
  47. """
  48. Return argument set for the current STT engine
  49. :param default_stt_plugin_name: Name of the STT engine
  50. :return:
  51. """
  52. def find(lst, key):
  53. """
  54. Find a key name in a list
  55. :param lst: list()
  56. :param key: key name to find i the list
  57. :return: Return the dict
  58. """
  59. for el in lst:
  60. try:
  61. if el[key]:
  62. return el[key]
  63. except TypeError:
  64. pass
  65. except KeyError:
  66. pass
  67. return None
  68. settings = cls.get_settings()
  69. try:
  70. speechs_to_text = settings["speech_to_text"]
  71. except KeyError:
  72. raise NoSpeechToTextConfiguration("No speech_to_text in settings")
  73. logging.debug("Settings file content: %s" % speechs_to_text)
  74. # get args
  75. args = find(speechs_to_text, default_stt_plugin_name)
  76. logging.debug("Args for %s STT: %s" % (default_stt_plugin_name, args))
  77. return args
  78. @classmethod
  79. def get_tts_args(cls, tts_name):
  80. """
  81. Return argument set for the current STT engine
  82. :param tts_name: Name of the TTS engine
  83. :return:
  84. """
  85. def find(lst, key):
  86. """
  87. Find a key name in a list
  88. :param lst: list()
  89. :param key: key name to find i the list
  90. :return: Return the dict
  91. """
  92. for el in lst:
  93. try:
  94. if el[key]:
  95. return el[key]
  96. except TypeError:
  97. pass
  98. except KeyError:
  99. pass
  100. return None
  101. settings = cls.get_settings()
  102. try:
  103. texts_to_speech = settings["text_to_speech"]
  104. except KeyError:
  105. raise NoSpeechToTextConfiguration("No text_to_speech in settings")
  106. logging.debug("Settings file content: %s" % texts_to_speech)
  107. # get args
  108. args = find(texts_to_speech, tts_name)
  109. logging.debug("Args for %s STT: %s" % (tts_name, args))
  110. return args
  111. @classmethod
  112. def get_tts_list(cls):
  113. settings = cls.get_settings()
  114. try:
  115. texts_to_speech = settings["text_to_speech"]
  116. except KeyError:
  117. raise NoSpeechToTextConfiguration("No text_to_speech in settings")
  118. return texts_to_speech