summaryrefslogtreecommitdiff
path: root/boot
diff options
context:
space:
mode:
Diffstat (limited to 'boot')
-rwxr-xr-xboot/boot.py91
-rwxr-xr-xboot/boot.sh2
-rwxr-xr-xboot/test.py54
-rw-r--r--boot/vocabcompiler.py71
4 files changed, 8 insertions, 210 deletions
diff --git a/boot/boot.py b/boot/boot.py
index 5291ea2..6ec7137 100755
--- a/boot/boot.py
+++ b/boot/boot.py
@@ -1,88 +1,11 @@
#!/usr/bin/env python2
# -*- coding: utf-8-*-
-
+# This file exists for backwards compatibility with older versions of jasper.
+# It might be removed in future versions.
import os
import sys
-
-# Set $JASPER_HOME
-jasper_home = os.getenv("JASPER_HOME")
-if not jasper_home or not os.path.exists(jasper_home):
- if os.path.exists("/home/pi"):
- jasper_home = "/home/pi"
- os.environ["JASPER_HOME"] = jasper_home
- else:
- print("Error: $JASPER_HOME is not set.")
- sys.exit(0)
-
-# Change CWD to $JASPER_HOME/jasper/boot
-os.chdir(os.path.join(os.getenv("JASPER_HOME"), "jasper", "boot"))
-
-# Set $LD_LIBRARY_PATH
-os.environ["LD_LIBRARY_PATH"] = "/usr/local/lib"
-
-# Set $PATH
-path = os.getenv("PATH")
-if path:
- path = os.pathsep.join([path, "/usr/local/lib/"])
-else:
- path = "/usr/local/lib/"
-os.environ["PATH"] = path
-
-import urllib2
-import vocabcompiler
-import traceback
-
-lib_path = os.path.abspath('../client')
-sys.path.append(lib_path)
-
-import speaker as speak
-speaker = speak.newSpeaker()
-
-
-def testConnection():
- try:
- urllib2.urlopen("http://www.google.com").getcode()
- print "CONNECTED TO INTERNET"
-
- except urllib2.URLError:
- print "COULD NOT CONNECT TO NETWORK"
- speaker.say(
- "Warning: I was unable to connect to a network. Parts of the system may not work correctly, depending on your setup.")
-
-
-def fail(message):
- traceback.print_exc()
- speaker.say(message)
-
-
-def configure():
- try:
- print "COMPILING DICTIONARY"
- vocabcompiler.compile(
- "../client/sentences.txt", "../client/dictionary.dic", "../client/languagemodel.lm")
- print "STARTING CLIENT PROGRAM"
- os.system("$JASPER_HOME/jasper/client/start.sh &")
-
- except OSError:
- print "BOOT FAILURE: OSERROR"
- fail(
- "There was a problem starting Jasper. You may be missing the language model and associated files. Please read the documentation to configure your Raspberry Pi.")
-
- except IOError:
- print "BOOT FAILURE: IOERROR"
- fail(
- "There was a problem starting Jasper. You may have set permissions incorrectly on some part of the filesystem. Please read the documentation to configure your Raspberry Pi.")
-
- except:
- print "BOOT FAILURE"
- fail(
- "There was a problem starting Jasper. Please read the documentation to configure your Raspberry Pi.")
-
-if __name__ == "__main__":
- print "==========STARTING JASPER CLIENT=========="
- print "=========================================="
- print "COPYRIGHT 2013 SHUBHRO SAHA, CHARLIE MARSH"
- print "=========================================="
- speaker.say("Hello.... I am Jasper... Please wait one moment.")
- testConnection()
- configure()
+import runpy
+script_path = os.path.join(os.path.dirname(__file__), os.pardir, "jasper.py")
+sys.path.remove(os.path.dirname(__file__))
+sys.path.insert(0, os.path.dirname(script_path))
+runpy.run_path(script_path, run_name="__main__")
diff --git a/boot/boot.sh b/boot/boot.sh
index 9fc8af8..240a7f7 100755
--- a/boot/boot.sh
+++ b/boot/boot.sh
@@ -1,4 +1,4 @@
#!/bin/bash
# This file exists for backwards compatibility with older versions of Jasper.
# It might be removed in future versions.
-"${0%/*}/boot.py"
+"${0%/*}/../jasper.py"
diff --git a/boot/test.py b/boot/test.py
deleted file mode 100755
index 638ca77..0000000
--- a/boot/test.py
+++ /dev/null
@@ -1,54 +0,0 @@
-#!/usr/bin/env python2
-# -*- coding: utf-8-*-
-import os
-
-if os.environ.get('JASPER_HOME') is None:
- os.environ['JASPER_HOME'] = '/home/pi'
-
-import sys
-import unittest
-from mock import patch
-import vocabcompiler
-
-lib_path = os.path.abspath('../client')
-mod_path = os.path.abspath('../client/modules/')
-
-sys.path.append(lib_path)
-sys.path.append(mod_path)
-
-import g2p
-
-
-class UnorderedList(list):
-
- def __eq__(self, other):
- return sorted(self) == sorted(other)
-
-
-class TestVocabCompiler(unittest.TestCase):
-
- def testWordExtraction(self):
- sentences = "temp_sentences.txt"
- dictionary = "temp_dictionary.dic"
- languagemodel = "temp_languagemodel.lm"
-
- words = [
- 'HACKER', 'LIFE', 'FACEBOOK', 'THIRD', 'NO', 'JOKE',
- 'NOTIFICATION', 'MEANING', 'TIME', 'TODAY', 'SECOND',
- 'BIRTHDAY', 'KNOCK KNOCK', 'INBOX', 'OF', 'NEWS', 'YES',
- 'TOMORROW', 'EMAIL', 'WEATHER', 'FIRST', 'MUSIC', 'SPOTIFY'
- ]
-
- with patch.object(g2p, 'translateWords') as translateWords:
- with patch.object(vocabcompiler, 'text2lm') as text2lm:
- vocabcompiler.compile(sentences, dictionary, languagemodel)
-
- # 'words' is appended with ['MUSIC', 'SPOTIFY']
- # so must be > 2 to have received WORDS from modules
- translateWords.assert_called_once_with(UnorderedList(words))
- self.assertTrue(text2lm.called)
- os.remove(sentences)
- os.remove(dictionary)
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/boot/vocabcompiler.py b/boot/vocabcompiler.py
deleted file mode 100644
index f22da1a..0000000
--- a/boot/vocabcompiler.py
+++ /dev/null
@@ -1,71 +0,0 @@
-# -*- coding: utf-8-*-
-"""
- Iterates over all the WORDS variables in the modules and creates a dictionary for the client.
-"""
-
-import os
-import sys
-import glob
-
-lib_path = os.path.abspath('../client')
-mod_path = os.path.abspath('../client/modules/')
-
-sys.path.append(lib_path)
-sys.path.append(mod_path)
-
-import g2p
-
-
-def text2lm(in_filename, out_filename):
- """Wrapper around the language model compilation tools"""
- def text2idngram(in_filename, out_filename):
- cmd = "text2idngram -vocab %s < %s -idngram temp.idngram" % (out_filename,
- in_filename)
- os.system(cmd)
-
- def idngram2lm(in_filename, out_filename):
- cmd = "idngram2lm -idngram temp.idngram -vocab %s -arpa %s" % (
- in_filename, out_filename)
- os.system(cmd)
-
- text2idngram(in_filename, in_filename)
- idngram2lm(in_filename, out_filename)
-
-
-def compile(sentences, dictionary, languagemodel):
- """
- Gets the words and creates the dictionary
- """
-
- m = [os.path.basename(f)[:-3]
- for f in glob.glob(os.path.dirname("../client/modules/") + "/*.py")]
-
- words = []
- for module_name in m:
- try:
- exec("import %s" % module_name)
- eval("words.extend(%s.WORDS)" % module_name)
- except:
- pass # module probably doesn't have the property
-
- words = list(set(words))
-
- # for spotify module
- words.extend(["MUSIC", "SPOTIFY"])
-
- # create the dictionary
- pronounced = g2p.translateWords(words)
- zipped = zip(words, pronounced)
- lines = ["%s %s" % (x, y) for x, y in zipped]
-
- with open(dictionary, "w") as f:
- f.write("\n".join(lines) + "\n")
-
- # create the language model
- with open(sentences, "w") as f:
- f.write("\n".join(words) + "\n")
- f.write("<s> \n </s> \n")
- f.close()
-
- # make language model
- text2lm(sentences, languagemodel)