Neurone.py 5.1 KB

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