Neurone.py 4.9 KB

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