From f727c00430b796a80dbd3ac459a15c9ed302dfb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Fri, 2 Aug 2019 18:16:29 +0200 Subject: [PATCH] Move pairing check to the relevant stt backends The pairing trigger should only be triggered by the 401 status for the Mycroft hosted STT backends. Doing it for other STT's is only confusing. This creates the decorator 'requires_pairing' that can be applied to the STT modules where it's relevant. This adds the decorator to the MycroftSTT and the MycroftDeepSpeechSTT --- mycroft/client/speech/listener.py | 8 +------- mycroft/stt/__init__.py | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/mycroft/client/speech/listener.py b/mycroft/client/speech/listener.py index 9473874940..31602701fc 100644 --- a/mycroft/client/speech/listener.py +++ b/mycroft/client/speech/listener.py @@ -16,7 +16,7 @@ import time from threading import Thread import speech_recognition as sr from pyee import EventEmitter -from requests import RequestException, HTTPError +from requests import RequestException from requests.exceptions import ConnectionError from mycroft import dialog @@ -215,12 +215,6 @@ class AudioConsumer(Thread): LOG.error("Connection Error: {0}".format(e)) self.emitter.emit("recognizer_loop:no_internet") - except HTTPError as e: - if e.response.status_code == 401: - LOG.warning("Access Denied at mycroft.ai") - return "pair my device" # phrase to start the pairing process - else: - LOG.error(e.__class__.__name__ + ': ' + str(e)) except RequestException as e: LOG.error(e.__class__.__name__ + ': ' + str(e)) except Exception as e: diff --git a/mycroft/stt/__init__.py b/mycroft/stt/__init__.py index ef0865397e..6282b3930d 100644 --- a/mycroft/stt/__init__.py +++ b/mycroft/stt/__init__.py @@ -20,7 +20,7 @@ from speech_recognition import Recognizer from queue import Queue from threading import Thread -from mycroft.api import STTApi +from mycroft.api import STTApi, HTTPError from mycroft.configuration import Configuration from mycroft.util.log import LOG @@ -118,11 +118,33 @@ class IBMSTT(BasicSTT): self.password, self.lang) +def requires_pairing(func): + """Decorator kicking of pairing sequence if client is not allowed access. + + Checks the http status of the response if an HTTP error is recieved. If + a 401 status is detected returns "pair my device" to trigger the pairing + skill. + """ + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except HTTPError as e: + if e.response.status_code == 401: + LOG.warning('Access Denied at mycroft.ai') + # phrase to start the pairing process + return 'pair my device' + else: + raise + return wrapper + + class MycroftSTT(STT): + """Default mycroft STT.""" def __init__(self): super(MycroftSTT, self).__init__() self.api = STTApi("stt") + @requires_pairing def execute(self, audio, language=None): self.lang = language or self.lang try: @@ -138,6 +160,7 @@ class MycroftDeepSpeechSTT(STT): super(MycroftDeepSpeechSTT, self).__init__() self.api = STTApi("deepspeech") + @requires_pairing def execute(self, audio, language=None): language = language or self.lang if not language.startswith("en"):