ConfigurationManager.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from BrainLoader import BrainLoader
  2. from SettingLoader import SettingLoader
  3. import logging
  4. logging.basicConfig()
  5. logger = logging.getLogger("jarvis")
  6. class DefaultSpeechToTextNotFound(Exception):
  7. pass
  8. class DefaultSpeechNull(Exception):
  9. pass
  10. class NoSpeechToTextConfiguration(Exception):
  11. pass
  12. class ConfigurationManager:
  13. def __init__(self):
  14. pass
  15. @classmethod
  16. def get_brain(cls, brain_file_name=None):
  17. if brain_file_name is None:
  18. brain_file_name = "brain.yml"
  19. return BrainLoader(brain_file_name).get_config()
  20. @classmethod
  21. def get_settings(cls, setting_file_name=None):
  22. if setting_file_name is None:
  23. setting_file_name = "settings.yml"
  24. return SettingLoader(setting_file_name).get_config()
  25. @classmethod
  26. def get_default_speech_to_text(cls):
  27. settings = cls.get_settings()
  28. try:
  29. default_speech_to_text = settings["default_speech_to_text"]
  30. if default_speech_to_text is None:
  31. raise DefaultSpeechNull("Attribute default_speech_to_text is null")
  32. logger.info("Default STT: %s" % default_speech_to_text)
  33. return default_speech_to_text
  34. except KeyError:
  35. raise DefaultSpeechToTextNotFound("Attribute default_speech_to_text not found in settings")
  36. @classmethod
  37. def get_default_text_to_speech(cls):
  38. settings = cls.get_settings()
  39. try:
  40. default_text_to_speech = settings["default_text_to_speech"]
  41. if default_text_to_speech is None:
  42. raise DefaultSpeechNull("Attribute default_text_to_speech is null")
  43. logger.info("Default TTS: %s" % default_text_to_speech)
  44. return default_text_to_speech
  45. except KeyError:
  46. raise DefaultSpeechToTextNotFound("Attribute default_text_to_speech not found in settings")
  47. @classmethod
  48. def get_stt_args(cls, default_stt_plugin_name):
  49. """
  50. Return argument set for the current STT engine
  51. :param default_stt_plugin_name: Name of the STT engine
  52. :return:
  53. """
  54. def find(lst, key):
  55. """
  56. Find a key name in a list
  57. :param lst: list()
  58. :param key: key name to find i the list
  59. :return: Return the dict
  60. """
  61. for el in lst:
  62. try:
  63. if el[key]:
  64. return el[key]
  65. except TypeError:
  66. pass
  67. except KeyError:
  68. pass
  69. return None
  70. settings = cls.get_settings()
  71. try:
  72. speechs_to_text = settings["speech_to_text"]
  73. except KeyError:
  74. raise NoSpeechToTextConfiguration("No speech_to_text in settings")
  75. logger.debug("Settings file content: %s" % speechs_to_text)
  76. # get args
  77. args = find(speechs_to_text, default_stt_plugin_name)
  78. logger.debug("Args for %s STT: %s" % (default_stt_plugin_name, args))
  79. return args
  80. @classmethod
  81. def get_tts_args(cls, tts_name):
  82. """
  83. Return argument set for the current STT engine
  84. :param tts_name: Name of the TTS engine
  85. :return:
  86. """
  87. def find(lst, key):
  88. """
  89. Find a key name in a list
  90. :param lst: list()
  91. :param key: key name to find i the list
  92. :return: Return the dict
  93. """
  94. for el in lst:
  95. try:
  96. if el[key]:
  97. return el[key]
  98. except TypeError:
  99. pass
  100. except KeyError:
  101. pass
  102. return None
  103. settings = cls.get_settings()
  104. try:
  105. texts_to_speech = settings["text_to_speech"]
  106. except KeyError:
  107. raise NoSpeechToTextConfiguration("No text_to_speech in settings")
  108. logger.debug("Settings file content: %s" % texts_to_speech)
  109. # get args
  110. args = find(texts_to_speech, tts_name)
  111. logger.debug("Args for %s TTS: %s" % (tts_name, args))
  112. # print args
  113. return args
  114. @classmethod
  115. def get_tts_list(cls):
  116. settings = cls.get_settings()
  117. try:
  118. texts_to_speech = settings["text_to_speech"]
  119. except KeyError:
  120. raise NoSpeechToTextConfiguration("No text_to_speech in settings")
  121. return texts_to_speech
  122. @classmethod
  123. def get_stt_list(cls):
  124. settings = cls.get_settings()
  125. try:
  126. speech_to_text = settings["speech_to_text"]
  127. except KeyError:
  128. raise NoSpeechToTextConfiguration("No speech_to_text in settings")
  129. return speech_to_text