diff options
Diffstat (limited to 'client')
| -rwxr-xr-x | client/diagnose.py | 64 | ||||
| -rw-r--r-- | client/g2p.py | 4 | ||||
| -rw-r--r-- | client/mic.py | 6 | ||||
| -rw-r--r-- | client/modules/Joke.py | 3 | ||||
| -rw-r--r-- | client/test.py | 15 |
5 files changed, 63 insertions, 29 deletions
diff --git a/client/diagnose.py b/client/diagnose.py index 28cd9c8..9e2c68f 100755 --- a/client/diagnose.py +++ b/client/diagnose.py @@ -3,8 +3,15 @@ import time import re import socket import os -from subprocess import check_output, call import jasperpath +import subprocess +import logging +import sys +from distutils.spawn import find_executable +from pip.req import parse_requirements +import pip.util + +logger = logging.getLogger(__name__) class Diagnostics: @@ -36,11 +43,36 @@ class Diagnostics: @classmethod def check_phonetisaurus_program(cls): - return call(['which', 'phonetisaurus-g2p']) == 0 + return cls.do_check_program('phonetisaurus-g2p') + + @classmethod + def check_espeak_program(cls): + return cls.do_check_program('espeak') + + @classmethod + def check_say_program(cls): + return cls.do_check_program('say') + + @classmethod + def do_check_program(cls, program): + return find_executable(program) is not None + + @classmethod + def check_all_pip_requirements_installed(cls): + distributions = pip.util.get_installed_distributions() + requirements_lines = [line.strip() for line in open('requirements.txt').readlines()] + requirements = [ name.split('==')[0] for name in list(filter(None, requirements_lines))] + installed_packages = [ pkg.project_name for pkg in list(distributions)] + missing_packages = [ pkg for pkg in requirements if pkg not in installed_packages ] + if missing_packages: + logger.info("Missing packages: "+', '.join(missing_packages)) + return False + else: + return True @classmethod def info_git_revision(cls): - return check_output(['git', 'rev-parse', 'HEAD']) + return subprocess.check_output(['git', 'rev-parse', 'HEAD']) class DiagnosticRunner: @@ -64,9 +96,9 @@ class DiagnosticRunner: for info in self.select_methods('info'): self.get_info(info) if self.failed_checks == 0: - self.log("All checks passed\n") + logger.info("All checks passed") else: - self.log("%d checks failed\n" % self.failed_checks) + logger.info("%d checks failed" % self.failed_checks) def select_methods(self, prefix): def is_match(method_name): @@ -75,31 +107,29 @@ class DiagnosticRunner: return [method_name for method_name in dir(self.diagnostics) if is_match(method_name)] def initialize_log(self): - self.output = open('diagnostic.log', 'w') - self.log("Starting jasper diagnostic\n") - self.log(time.strftime("%c") + "\n") - - def log(self, msg): - print msg, - self.output.write(msg) + logger.info("Starting jasper diagnostic at %s" % time.strftime("%c")) def get_info(self, info_name): message = info_name.replace("info_", "").replace("_", " ") info_method = getattr(self.diagnostics, info_name) info = info_method() - self.log("%s: %s" % (message, info)) + logger.info("%s: %s" % (message, info)) def do_check(self, check_name): message = check_name.replace("check_", "").replace("_", " ") check = getattr(self.diagnostics, check_name) - self.log("Checking %s... " % message) if check(): - self.log("OK") + result = "OK" else: self.failed_checks += 1 - self.log("FAILED") - self.log("\n") + result = "FAILED" + + logger.info("Checking %s... %s" % (message, result)) if __name__ == '__main__': + logging.basicConfig(stream=sys.stdout, level=logging.INFO) + DiagnosticRunner(Diagnostics).run() + + diff --git a/client/g2p.py b/client/g2p.py index 276f681..6e308ef 100644 --- a/client/g2p.py +++ b/client/g2p.py @@ -5,6 +5,8 @@ import subprocess import re import yaml +import jasperpath + PHONE_MATCH = re.compile(r'<s> (.*) </s>') FST_MODEL = None @@ -18,7 +20,7 @@ if os.path.exists(profile_path): FST_MODEL = profile['pocketsphinx']['fst_model'] if not FST_MODEL: - FST_MODEL = os.environ['JASPER_HOME'] + "/phonetisaurus/g014b2b.fst" + FST_MODEL = os.path.join(jasperpath.APP_PATH, os.pardir, 'phonetisaurus', 'g014b2b.fst') def parseLine(line): diff --git a/client/mic.py b/client/mic.py index 3150d4e..6c428c0 100644 --- a/client/mic.py +++ b/client/mic.py @@ -8,7 +8,7 @@ from wave import open as open_audio import audioop import pyaudio import alteration - +import jasperpath class Mic: @@ -190,7 +190,7 @@ class Mic: if THRESHOLD == None: THRESHOLD = self.fetchThreshold() - self.speaker.play("../static/audio/beep_hi.wav") + self.speaker.play(jasperpath.data('audio', 'beep_hi.wav')) # prepare recording stream audio = pyaudio.PyAudio() @@ -219,7 +219,7 @@ class Mic: if average < THRESHOLD * 0.8: break - self.speaker.play("../static/audio/beep_lo.wav") + self.speaker.play(jasperpath.data('audio', 'beep_lo.wav')) # save the audio data stream.stop_stream() diff --git a/client/modules/Joke.py b/client/modules/Joke.py index 802047c..c560f4e 100644 --- a/client/modules/Joke.py +++ b/client/modules/Joke.py @@ -1,11 +1,12 @@ # -*- coding: utf-8-*- import random import re +import jasperpath WORDS = ["JOKE", "KNOCK KNOCK"] -def getRandomJoke(filename="../static/text/JOKES.txt"): +def getRandomJoke(filename=jasperpath.data('text','JOKES.txt')): jokeFile = open(filename, "r") jokes = [] start = "" diff --git a/client/test.py b/client/test.py index 8925e66..0160b76 100644 --- a/client/test.py +++ b/client/test.py @@ -2,18 +2,16 @@ # -*- coding: utf-8-*- import os import sys - -if os.environ.get('JASPER_HOME') is None: - os.environ['JASPER_HOME'] = '/home/pi' - import unittest import argparse from mock import patch from urllib2 import URLError, urlopen + import test_mic import vocabcompiler import g2p import brain +import jasperpath from diagnose import Diagnostics DEFAULT_PROFILE = { @@ -59,8 +57,8 @@ class TestVocabCompiler(unittest.TestCase): class TestMic(unittest.TestCase): def setUp(self): - self.jasper_clip = "../static/audio/jasper.wav" - self.time_clip = "../static/audio/time.wav" + self.jasper_clip = jasperpath.data('audio', 'jasper.wav') + self.time_clip = jasperpath.data('audio', 'time.wav') from stt import PocketSphinxSTT self.stt = PocketSphinxSTT() @@ -134,7 +132,7 @@ class TestModules(unittest.TestCase): inputs = ["Who's there?", "Random response"] outputs = self.runConversation(query, inputs, Joke) self.assertEqual(len(outputs), 3) - allJokes = open("../static/text/JOKES.txt", "r").read() + allJokes = open(jasperpath.data('text','JOKES.txt'), 'r').read() self.assertTrue(outputs[2] in allJokes) def testTime(self): @@ -235,6 +233,9 @@ if __name__ == '__main__': help='runs a subset of the tests (only requires Python dependencies)') args = parser.parse_args() + # Change CWD to jasperpath.LIB_PATH + os.chdir(jasperpath.LIB_PATH) + test_cases = [TestBrain, TestModules, TestVocabCompiler] if not args.light: test_cases.append(TestG2P) |
