From 263a86b10264518e4190259bcc39004f8ced1f3f Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 10 Sep 2014 18:12:04 +0200 Subject: Move client/main.py to jasper.py and add client/main.py dummy script for backwards compatibility --- boot/boot.py | 5 ++- boot/boot.sh | 2 +- client/main.py | 125 ++++-------------------------------------------------- client/start.sh | 2 +- jasper.py | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 121 deletions(-) create mode 100755 jasper.py diff --git a/boot/boot.py b/boot/boot.py index c7afcfd..6ec7137 100755 --- a/boot/boot.py +++ b/boot/boot.py @@ -3,6 +3,9 @@ # This file exists for backwards compatibility with older versions of jasper. # It might be removed in future versions. import os +import sys import runpy -script_path = os.path.join(os.path.dirname(__file__), os.pardir, "client", "main.py") +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/client/main.py b/client/main.py index b62271c..6ec7137 100755 --- a/client/main.py +++ b/client/main.py @@ -1,122 +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 - -# Change CWD to $JASPER_HOME/jasper/client -jasper_home = os.getenv("JASPER_HOME") -if not jasper_home or not os.path.exists(jasper_home): - print("Error: $JASPER_HOME is not set.") - sys.exit(0) - -os.chdir(os.path.join(jasper_home, "jasper", "client")) - -# 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 traceback - -import vocabcompiler -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) - #sys.exit(1) - -def configure(): - try: - print "COMPILING DICTIONARY" - vocabcompiler.compile( - "sentences.txt", "dictionary.dic", "languagemodel.lm") - print "STARTING CLIENT PROGRAM" - - 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.") - -import shutil - -old_client = os.path.abspath(os.path.join(os.pardir, "old_client")) -if os.path.exists(old_client): - shutil.rmtree(old_client) - -import yaml -import stt -from conversation import Conversation - -def isLocal(): - return len(sys.argv) > 1 and sys.argv[1] == "--local" - -if isLocal(): - from local_mic import Mic -else: - from mic import Mic - -if __name__ == "__main__": - - print "===========================================================" - print " JASPER The Talking Computer " - print " Copyright 2013 Shubhro Saha & Charlie Marsh " - print "===========================================================" - - speaker.say("Hello.... I am Jasper... Please wait one moment.") - testConnection() - configure() - - profile = yaml.safe_load(open("profile.yml", "r")) - - try: - api_key = profile['keys']['GOOGLE_SPEECH'] - except KeyError: - api_key = None - - try: - stt_engine_type = profile['stt_engine'] - except KeyError: - print "stt_engine not specified in profile, defaulting to PocketSphinx" - stt_engine_type = "sphinx" - - mic = Mic(speaker, stt.PocketSphinxSTT(), - stt.newSTTEngine(stt_engine_type, api_key=api_key)) - - addendum = "" - if 'first_name' in profile: - addendum = ", %s" % profile["first_name"] - mic.say("How can I be of service%s?" % addendum) - - conversation = Conversation("JASPER", mic, profile) - - conversation.handleForever() +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/client/start.sh b/client/start.sh index cde8d8d..240a7f7 100755 --- a/client/start.sh +++ b/client/start.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%/*}/main.py" +"${0%/*}/../jasper.py" diff --git a/jasper.py b/jasper.py new file mode 100755 index 0000000..30af4dd --- /dev/null +++ b/jasper.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python2 + +import os +import sys +print __package__ +print __file__ +print sys.path +# 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/client +os.chdir(os.path.join(os.getenv("JASPER_HOME"), "jasper" , "client")) + +# 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 traceback + +from client import vocabcompiler +from client 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) + #sys.exit(1) + +def configure(): + try: + print "COMPILING DICTIONARY" + vocabcompiler.compile( + "sentences.txt", "dictionary.dic", "languagemodel.lm") + print "STARTING CLIENT PROGRAM" + + 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.") + +import shutil + +old_client = os.path.abspath(os.path.join(os.pardir, "old_client")) +if os.path.exists(old_client): + shutil.rmtree(old_client) + +import yaml +from client import stt +from client.conversation import Conversation + +def isLocal(): + return len(sys.argv) > 1 and sys.argv[1] == "--local" + +if isLocal(): + from client.local_mic import Mic +else: + from client.mic import Mic + +if __name__ == "__main__": + + print "===========================================================" + print " JASPER The Talking Computer " + print " Copyright 2013 Shubhro Saha & Charlie Marsh " + print "===========================================================" + + speaker.say("Hello.... I am Jasper... Please wait one moment.") + testConnection() + configure() + + profile = yaml.safe_load(open("profile.yml", "r")) + + try: + api_key = profile['keys']['GOOGLE_SPEECH'] + except KeyError: + api_key = None + + try: + stt_engine_type = profile['stt_engine'] + except KeyError: + print "stt_engine not specified in profile, defaulting to PocketSphinx" + stt_engine_type = "sphinx" + + mic = Mic(speaker, stt.PocketSphinxSTT(), + stt.newSTTEngine(stt_engine_type, api_key=api_key)) + + addendum = "" + if 'first_name' in profile: + addendum = ", %s" % profile["first_name"] + mic.say("How can I be of service%s?" % addendum) + + conversation = Conversation("JASPER", mic, profile) + + conversation.handleForever() -- cgit v1.3.1