Permalink
Browse files

Refactor tts_worker; add a config variable; add a test

  • Loading branch information...
osteele committed Feb 10, 2018
1 parent 8d380d4 commit 16c53d2a5dbf685a0cdd54942e9a268432bf081c
Showing with 36 additions and 9 deletions.
  1. +19 −9 scripts/tts_worker.py
  2. +17 −0 tests/tts_worker_test.py
@@ -7,29 +7,39 @@

import click

sys.path.append(os.path.join(os.path.dirname(__file__), './..')) # noqa: I003
# noqa: I003
# Allow this program to even if it's invoked from a current working directory
# different from the project root.
# FIXME Maybe this mode of operation isn't worth it.
sys.path.append(os.path.join(os.path.dirname(__file__), './..'))
import mqtt_json # noqa: E402,I001

logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger('speaker')
logger.setLevel(logging.INFO)


SPEECH_COMMAND = 'say' if platform.system() == 'Darwin' else 'espeak'
# The command-line program to use for speech synthesis.
DEFAULT_SPEECH_COMMAND = 'say' if platform.system() == 'Darwin' else 'espeak'
SPEECH_COMMAND = os.getenv('BEAR_SPEECH_COMMAND', DEFAULT_SPEECH_COMMAND)

mqtt_client = mqtt_json.Client()


def process_speech_message(message):
logger.info(message)
message_text = message['message']
res = subprocess.run([SPEECH_COMMAND, message_text],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if res.returncode != 0:
logger.error(res.stderr.decode().strip())


@click.command()
@click.option('--topic', default='speak')
def main(topic):
for msg in mqtt_client.create_subscription_queue(topic):
message = msg['message']
logger.info(msg)
res = subprocess.run([SPEECH_COMMAND, message],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.returncode != 0:
logger.error(res.stderr.decode().strip())
process_speech_message(msg)


if __name__ == '__main__':
@@ -0,0 +1,17 @@
import os
import subprocess
import sys
from unittest.mock import patch

# noqa: I003
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'scripts'))
import tts_worker # noqa: E402,I001,I003


@patch('subprocess.run')
def test_process_speech_message(run):
with patch('tts_worker.SPEECH_COMMAND', new='speech-command'):
tts_worker.process_speech_message(dict(message='Hello world'))
run.assert_called_with(['speech-command', 'Hello world'],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)

0 comments on commit 16c53d2

Please sign in to comment.