Neurone.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import importlib
  2. from core import ConfigurationManager
  3. class TTSModuleNotFound(Exception):
  4. pass
  5. class TTSNotInstantiable(Exception):
  6. pass
  7. class Neurone:
  8. def __init__(self, tts=None):
  9. # get the name of the plugin
  10. # print self.__class__.__name__
  11. # load the tts from settings
  12. self.tts = tts
  13. if tts is None:
  14. self.tts = ConfigurationManager.get_default_text_to_speech()
  15. # get tts args
  16. self.tts_args = ConfigurationManager.get_tts_args(self.tts)
  17. self.tts = self.tts.capitalize()
  18. print "tts args: %s" % str(self.tts_args)
  19. # instantiate the TTS
  20. self.tts_instance = self._get_tts_instance()
  21. def say(self, message):
  22. # here we use the tts to make jarvis talk
  23. # the module is imported on fly, depending on the selected tts from settings
  24. self.tts_instance.say(words=message, **(self.tts_args if self.tts_args is not None else {}))
  25. def _get_tts_instance(self):
  26. print "Import TTS module named %s " % self.tts
  27. mod = __import__('tts', fromlist=[self.tts])
  28. try:
  29. klass = getattr(mod, self.tts)
  30. except ImportError, e:
  31. raise TTSModuleNotFound("The TTS not found: %s" % e)
  32. if klass is not None:
  33. # run the plugin
  34. return klass()
  35. else:
  36. raise TTSNotInstantiable("TTS module %s not instantiable" % self.tts)