Neurone.py 5.2 KB

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