| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import logging
- import threading
- import traceback
- import input_sources
- import output
- import services.help
- import echo
- import duckduckgo_answers
- import conf
- def _get_services(cclient):
- res = {
- 'echo': echo.Echo(),
- 'duck': duckduckgo_answers.DuckDuckGo(),
- }
- res['help'] = services.help.Help(res)
- if conf.WEATHER_LAT and conf.WEATHER_LON:
- import weather
- res['weather'] = weather.Weather()
- if conf.MAILGUN_API_KEY:
- import email_mailgun
- res['email'] = email_mailgun.Email()
- if conf.WUNDERLIST_ACCESS_TOKEN:
- import todo_wunderlist
- res['food'] = todo_wunderlist.Wunderlist(True, 'food')
- res['todo'] = todo_wunderlist.Wunderlist(False)
- if conf.MUSIC_FOLDERS:
- import music
- res['play'] = music.Music(cclient)
- if conf.TZ:
- import current_time
- res['time'] = current_time.CurrentTime(conf.TZ)
- return res
- def launch_services(cclient, interrupted, trigger_interrupted):
- _services = _get_services(cclient)
- for s in _services.values():
- if not s.initialized:
- t = threading.Thread(target=s.init)
- t.start()
- preferred_phrases = _services.keys() + ['quit', 'cancel']
- output.text('Ready. Available services: %s.' % ', '.join(sorted(_services.keys())))
- while not interrupted():
- t = input_sources.get_token(1, preferred_phrases=preferred_phrases)
- if t is not None and t in _services and _services[t].initialized:
- try:
- _services[t].call()
- output.success()
- except Exception, e:
- output.failure(msg='Error: %s' % traceback.format_exc())
- elif t == 'quit':
- trigger_interrupted()
- return
-
- elif t == 'cancel':
- pass
- elif t is not None:
- output.failure('"%s" is not a valid service.' % t)
- if t is not None:
- input_sources.reset()
|