Neurone.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import importlib
  2. from jinja2 import Template
  3. import random
  4. import os.path
  5. from core import ConfigurationManager
  6. class NoTemplateException(Exception):
  7. pass
  8. class MultipleTemplateException(Exception):
  9. pass
  10. class TemplateFileNotFoundException(Exception):
  11. pass
  12. class TTSModuleNotFound(Exception):
  13. pass
  14. class TTSNotInstantiable(Exception):
  15. pass
  16. class Neurone:
  17. def __init__(self, **kwargs):
  18. # get the name of the plugin
  19. # print self.__class__.__name__
  20. # load the tts from settings
  21. # get the tts if is specified otherwise use default
  22. tts = kwargs.get('tts', None)
  23. if tts is not None:
  24. self.tts = tts
  25. else:
  26. self.tts = ConfigurationManager.get_default_text_to_speech()
  27. # get tts args
  28. self.tts_args = ConfigurationManager.get_tts_args(self.tts)
  29. # capitalise for loading module name
  30. self.tts = self.tts.capitalize()
  31. # load the module
  32. self.tts_instance = self._get_tts_instance()
  33. def say(self, message, kwargs):
  34. # get the tts if is specified otherwise use default
  35. tts = kwargs.get('tts', None)
  36. if tts is not None:
  37. self.tts = tts
  38. self.tts_args = ConfigurationManager.get_tts_args(self.tts)
  39. # check if it's a single message or multiple one
  40. if isinstance(message, list):
  41. # then we pick randomly one message
  42. message = random.choice(message)
  43. # Check if there is a template associate to the output message
  44. say_template = kwargs.get('say_template', None)
  45. # check if there is a template file associate to the output message
  46. file_template = kwargs.get('file_template', None)
  47. # we check if the user provide a say_template or a file_template, Not both
  48. if say_template is not None and file_template is not None:
  49. raise MultipleTemplateException("You must provide a say_template or a file_template, not both")
  50. # check on of the two option is set
  51. if isinstance(message, dict):
  52. if (say_template is not None and file_template is None) or \
  53. (say_template is None and file_template is not None):
  54. if say_template is not None: # the user choose a say_template option
  55. if isinstance(say_template, list):
  56. # then we pick randomly one template
  57. say_template = random.choice(say_template)
  58. t = Template(say_template)
  59. message = t.render(**message)
  60. if file_template is not None: # the user choose a file_template option
  61. real_file_template_path = "templates/%s" % file_template
  62. if os.path.isfile(real_file_template_path):
  63. # load the content of the file as template
  64. t = Template(self._get_content_of_file(real_file_template_path))
  65. message = t.render(**message)
  66. else:
  67. raise TemplateFileNotFoundException("Template file %s not found in templates folder"
  68. % real_file_template_path)
  69. else:
  70. raise NoTemplateException("You must specify a say_template or a file_template", message.keys())
  71. # here we use the tts to make jarvis talk
  72. # the module is imported on fly, depending on the selected tts from settings
  73. self.tts_instance.say(words=message, **(self.tts_args if self.tts_args is not None else {}))
  74. def _get_tts_instance(self):
  75. print "Import TTS module named %s " % self.tts
  76. mod = __import__('tts', fromlist=[self.tts])
  77. try:
  78. klass = getattr(mod, self.tts)
  79. except ImportError, e:
  80. raise TTSModuleNotFound("The TTS not found: %s" % e)
  81. if klass is not None:
  82. # run the plugin
  83. return klass()
  84. else:
  85. raise TTSNotInstantiable("TTS module %s not instantiable" % self.tts)
  86. @staticmethod
  87. def _check_file_exist(real_file_template):
  88. return os.path.isfile(real_file_template)
  89. @staticmethod
  90. def _get_content_of_file(real_file_template_path):
  91. with open(real_file_template_path, 'r') as content_file:
  92. return content_file.read()