Neurone.py 5.0 KB

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