| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import Queue
- import threading
- import console
- import conf
- LANGUAGE = 'en-GB'
- PREFERRED_PHRASES = None
- _TOKEN_STREAM = None
- _RESET_SERVICES = []
- class TokenStream(object):
- def __init__(self):
- self.preferred_phrases = None
- self.buffer = Queue.Queue()
-
- def add_token(self, token):
- self.buffer.put(token)
-
- def get_token(self, timeout):
- try:
- return self.buffer.get(timeout=timeout).strip().lower()
- except Queue.Empty:
- return None
- def get_line(self, timeout):
- return self.get_token(timeout)
-
- def reset(self):
- self.buffer = Queue.Queue()
- def launch_input_sources(curses_client, interrupt_check):
- global _TOKEN_STREAM
- _TOKEN_STREAM = TokenStream()
- global _RESET_SERVICES
- _RESET_SERVICES = []
- services = [
- console.ConsoleInput(curses_client, _TOKEN_STREAM),
- ]
- if conf.SPEECH_INPUT_ENABLED and conf.SNOWBOY_HOTWORD_MODEL:
- import google_cloud_speech
- import snowboy_hotword_detector
- speech_detector = google_cloud_speech.GoogleCloudSpeech(_TOKEN_STREAM, interrupt_check)
- hotword_detector = snowboy_hotword_detector.HotwordDetector(speech_detector, interrupt_check)
- services.append(hotword_detector)
- _RESET_SERVICES.append(hotword_detector)
-
- if conf.SLACK_BOT_API_KEY:
- import slack_bot
- services.append(slack_bot.SlackBot(_TOKEN_STREAM))
- for s in services:
- t = threading.Thread(target=s.run)
- t.setDaemon(True)
- t.start()
- def get_token(timeout=120, preferred_phrases=None):
- global PREFERRED_PHRASES
- PREFERRED_PHRASES = preferred_phrases
- return _TOKEN_STREAM.get_token(timeout)
- def get_line(timeout=120, preferred_phrases=None):
- global PREFERRED_PHRASES
- PREFERRED_PHRASES = preferred_phrases
- return _TOKEN_STREAM.get_line(timeout)
- def token_available():
- return not _TOKEN_STREAM.buffer.empty()
- def get_until_terminated(timeout=120, terminators=['done', 'cancel'], preferred_phrases=None):
- global PREFERRED_PHRASES
- PREFERRED_PHRASES = preferred_phrases
- res = []
- t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
- while t not in terminators:
- res.append(t)
- t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
- return res, t
- def reset():
- for service in _RESET_SERVICES:
- service.reset()
- _TOKEN_STREAM.reset()
|