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() output.text('Ready. Available services: %s.' % ', '.join(sorted(_services.keys()))) while not interrupted(): input_sources.PREFERRED_PHRASES = _services.keys() + ['quit', 'cancel'] t = input_sources.get_token(1) if t is not None and t in _services and _services[t].initialized: try: _services[t].call() output.success() except Exception, e: output.failure(s='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()