Neurone.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  20. # print self.__class__.__name__
  21. # load the tts from settings
  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. # check if it's a single message or multiple one
  41. if isinstance(message, list):
  42. # then we pick randomly one message
  43. message = random.choice(message)
  44. # Check if there is a template associate to the output message
  45. say_template = kwargs.get('say_template', None)
  46. # check if there is a template file associate to the output message
  47. file_template = kwargs.get('file_template', None)
  48. # we check if the user provide a say_template or a file_template, Not both
  49. if say_template is not None and file_template is not None:
  50. raise MultipleTemplateException("You must provide a say_template or a file_template, not both")
  51. # check on of the two option is set
  52. if isinstance(message, dict):
  53. if (say_template is not None and file_template is None) or \
  54. (say_template is None and file_template is not None):
  55. if say_template is not None: # the user choose a say_template option
  56. if isinstance(say_template, list):
  57. # then we pick randomly one template
  58. say_template = random.choice(say_template)
  59. t = Template(say_template)
  60. message = t.render(**message)
  61. if file_template is not None: # the user choose a file_template option
  62. real_file_template_path = "templates/%s" % file_template
  63. if os.path.isfile(real_file_template_path):
  64. # load the content of the file as template
  65. t = Template(self._get_content_of_file(real_file_template_path))
  66. message = t.render(**message)
  67. else:
  68. raise TemplateFileNotFoundException("Template file %s not found in templates folder"
  69. % real_file_template_path)
  70. else:
  71. raise NoTemplateException("You must specify a say_template or a file_template", message.keys())
  72. # here we use the tts to make jarvis talk
  73. # the module is imported on fly, depending on the selected tts from settings
  74. self.tts_instance.say(words=message, **(self.tts_args if self.tts_args is not None else {}))
  75. def _get_tts_instance(self):
  76. logging.info("Import TTS module named %s " % self.tts)
  77. mod = __import__('tts', fromlist=[str(self.tts)])
  78. try:
  79. klass = getattr(mod, self.tts)
  80. except ImportError, e:
  81. raise TTSModuleNotFound("The TTS not found: %s" % e)
  82. if klass is not None:
  83. # run the plugin
  84. return klass()
  85. else:
  86. raise TTSNotInstantiable("TTS module %s not instantiable" % self.tts)
  87. @staticmethod
  88. def _check_file_exist(real_file_template):
  89. return os.path.isfile(real_file_template)
  90. @staticmethod
  91. def _get_content_of_file(real_file_template_path):
  92. with open(real_file_template_path, 'r') as content_file:
  93. return content_file.read()