__init__.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Queue
  2. import threading
  3. import console
  4. import conf
  5. LANGUAGE = 'en-GB'
  6. PREFERRED_PHRASES = None
  7. _TOKEN_STREAM = None
  8. class TokenStream(object):
  9. def __init__(self):
  10. self.preferred_phrases = None
  11. self.buffer = Queue.Queue()
  12. def add_token(self, token):
  13. self.buffer.put(token)
  14. def get_token(self, timeout):
  15. try:
  16. return self.buffer.get(timeout=timeout).strip().lower()
  17. except Queue.Empty:
  18. return None
  19. def get_line(self, timeout):
  20. return self.get_token(timeout)
  21. def launch_input_sources(curses_client):
  22. global _TOKEN_STREAM
  23. _TOKEN_STREAM = TokenStream()
  24. services = [
  25. console.ConsoleInput(curses_client, _TOKEN_STREAM),
  26. ]
  27. if conf.ENABLE_SPEECH_INPUT:
  28. import google_cloud_speech
  29. services.append(google_cloud_speech.GoogleCloudSpeech(_TOKEN_STREAM))
  30. if conf.SLACK_BOT_API_KEY:
  31. import slack_bot
  32. services.append(slack_bot.SlackBot(_TOKEN_STREAM))
  33. for s in services:
  34. t = threading.Thread(target=s.run)
  35. t.setDaemon(True)
  36. t.start()
  37. def get_token(timeout=120, preferred_phrases=None):
  38. global PREFERRED_PHRASES
  39. PREFERRED_PHRASES = preferred_phrases
  40. return _TOKEN_STREAM.get_token(timeout)
  41. def get_line(timeout=120, preferred_phrases=None):
  42. global PREFERRED_PHRASES
  43. PREFERRED_PHRASES = preferred_phrases
  44. return _TOKEN_STREAM.get_line(timeout)
  45. def token_available():
  46. return not _TOKEN_STREAM.buffer.empty()
  47. def get_until_terminated(timeout=120, terminators=['done', 'cancel'], preferred_phrases=None):
  48. global PREFERRED_PHRASES
  49. PREFERRED_PHRASES = preferred_phrases
  50. res = []
  51. t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
  52. while t not in terminators:
  53. res.append(t)
  54. t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
  55. return res, t