Neurone.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import importlib
  2. from jinja2 import Template
  3. import random
  4. import os.path
  5. from core import ConfigurationManager
  6. class NoTemplateException(Exception):
  7. pass
  8. class MultipleTemplateException(Exception):
  9. pass
  10. class TemplateFileNotFoundException(Exception):
  11. pass
  12. class Neurone:
  13. def __init__(self, **kwargs):
  14. # get the name of the plugin
  15. # print self.__class__.__name__
  16. # load the tts from settings
  17. self.tts = ConfigurationManager.get_default_text_to_speech()
  18. # get tts args
  19. self.tts_args = ConfigurationManager.get_tts_args(self.tts)
  20. def say(self, message, kwargs):
  21. # get the tts if is specified otherwise use default
  22. tts = kwargs.get('tts', None)
  23. if tts is not None:
  24. self.tts = tts
  25. self.tts_args = ConfigurationManager.get_tts_args(self.tts)
  26. # check if it's a single message or multiple one
  27. if isinstance(message, list):
  28. # then we pick randomly one message
  29. message = random.choice(message)
  30. # Check if there is a template associate to the output message
  31. say_template = kwargs.get('say_template', None)
  32. # check if there is a template file associate to the output message
  33. file_template = kwargs.get('file_template', None)
  34. # we check if the user provide a say_template or a file_template, Not both
  35. if say_template is not None and file_template is not None:
  36. raise MultipleTemplateException("You must provide a say_template or a file_template, not both")
  37. # check on of the two option is set
  38. if isinstance(message, dict):
  39. if (say_template is not None and file_template is None) or \
  40. (say_template is None and file_template is not None):
  41. if say_template is not None: # the user choose a say_template option
  42. if isinstance(say_template, list):
  43. # then we pick randomly one template
  44. say_template = random.choice(say_template)
  45. t = Template(say_template)
  46. message = t.render(**message)
  47. if file_template is not None: # the user choose a file_template option
  48. real_file_template_path = "templates/%s" % file_template
  49. if os.path.isfile(real_file_template_path):
  50. # load the content of the file as template
  51. t = Template(self._get_content_of_file(real_file_template_path))
  52. message = t.render(**message)
  53. else:
  54. raise TemplateFileNotFoundException("Template file %s not found in templates folder"
  55. % real_file_template_path)
  56. else:
  57. raise NoTemplateException("You must specify a say_template or a file_template", message.keys())
  58. # here we use the tts to make jarvis talk
  59. # the module is imported on fly, depending on the selected tts from settings
  60. tts_backend = importlib.import_module("tts." + self.tts)
  61. tts_backend.say(words=message, **self.tts_args)
  62. @staticmethod
  63. def _check_file_exist(real_file_template):
  64. return os.path.isfile(real_file_template)
  65. @staticmethod
  66. def _get_content_of_file(real_file_template_path):
  67. with open(real_file_template_path, 'r') as content_file:
  68. return content_file.read()