def error(self): """ Inform the user that an error occurred """ tts.speak(settings.ERROR) text = input('Continue? (Y/N) ') # response = stt.active_listen() if 'y' in text.lower(): log.error(traceback.format_exc())
def run(self): """ Listen for input, match the modules and respond """ while True: if self.quit_flag: break try: if settings.USE_STT: stt.listen_keyword() text = stt.active_listen() else: text = input('> ') if not text: log.info('No text input received.') continue else: log.info("'" + text + "'") self.match_mods(text) self.execute_mods(text) except OSError as e: if 'Invalid input device' in str(e): log.error(settings.NO_MIC + '\n') settings.USE_STT = False continue else: raise Exception except (EOFError, KeyboardInterrupt): log.info('Shutting down...') break except: log.error("(runtime error)") self.error() log.info('Arrivederci.')
def find_mods(): """ Find and import modules from the module directories """ global mod_lib mod_lib = [] log.debug('Looking for modules in: ' + str(settings.MOD_DIRS)) for finder, name, _ in pkgutil.iter_modules(settings.MOD_DIRS): try: mod = finder.find_module(name).load_module(name) for member in dir(mod): obj = getattr(mod, member) if inspect.isclass(obj): for parent in obj.__bases__: if 'Module' is parent.__name__: mod_lib.append(obj()) except Exception as e: print(traceback.format_exc()) log.error('Error loading \'' + name + '\' ' + str(e)) mod_lib.sort(key=lambda mod: mod.priority, reverse=True)
def find_apis(): """ Find APIs """ global api_lib api_lib = {} log.debug('Looking for APIs in: ' + str(settings.API_DIRS)) for finder, name, _ in pkgutil.iter_modules(settings.API_DIRS): try: file = finder.find_module(name).load_module(name) for member in dir(file): obj = getattr(file, member) if inspect.isclass(obj): for parent in obj.__bases__: if 'Api' is parent.__name__: api = obj() api_lib[api.key] = api except Exception as e: print(traceback.format_exc()) log.error('Error loading \'' + name + '\' ' + str(e))
def speak(phrase, cache=False, filename='default', show_text=True, log_text=True): """Speaks a given text phrase :param phrase: text string to speak :param cache: if True, store .mp3 in 'media/responses' :param filename: filename if cached :param show_text: if True, store .mp3 in 'media/responses' :param cache: if True, store .mp3 in 'media/responses' """ if show_text: log.info(phrase) if not settings.USE_TTS: log.info('SPOKEN: ' + phrase) return try: phrase = phrase[:settings.MAX_CHAR] tts = gTTS(text=phrase, lang=settings.LANG_CODE) if not cache: with tempfile.NamedTemporaryFile(mode='wb', suffix='.mp3', delete=False) as f: (temp_path, temp_name) = os.path.split(f.name) tts.write_to_fp(f) play_mp3(temp_name, temp_path) os.remove(os.path.join(temp_path, temp_name)) else: filename = os.path.join(settings.RESPONSES_DIR, filename + '.mp3') tts.save(filename) log.info('Saved to: ' + filename) except HTTPError as e: log.error('Google TTS might not be updated: ' + str(e)) except Exception as e: log.error('Unknown Google TTS issue: ' + str(e))