summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-10-04 13:05:09 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2014-10-04 13:05:09 +0200
commit3966d2f86a251f372a7a17f39108925ab9459974 (patch)
tree6b8c45a01ceb00f12faa21d02ad07977894813df
parent4c22cd55978cd6d3b2abe51a12447f75992d9f1f (diff)
parent5ce3b07d0bc52c06dc1b184a144e87bab22c1d5d (diff)
downloadjasper-client-3966d2f86a251f372a7a17f39108925ab9459974.tar.gz
jasper-client-3966d2f86a251f372a7a17f39108925ab9459974.zip
Merge pull request #203 from Holzhaus/good-riddance-dear-writable-app-dir
Use config dir for non-temporary writable files
-rw-r--r--client/populate.py7
-rw-r--r--client/stt.py5
-rwxr-xr-xjasper.py53
-rw-r--r--static/dictionary_persona.dic (renamed from client/dictionary_persona.dic)0
-rw-r--r--static/languagemodel_persona.lm (renamed from client/languagemodel_persona.lm)0
5 files changed, 45 insertions, 20 deletions
diff --git a/client/populate.py b/client/populate.py
index d1cbb80..e871f26 100644
--- a/client/populate.py
+++ b/client/populate.py
@@ -1,10 +1,11 @@
# -*- coding: utf-8-*-
+import os
import re
from getpass import getpass
import yaml
from pytz import timezone
import feedparser
-
+import jasperpath
def run():
profile = {}
@@ -105,7 +106,9 @@ def run():
# write to profile
print("Writing to profile...")
- outputFile = open("profile.yml", "w")
+ if not os.path.exists(jasperpath.CONFIG_PATH):
+ os.makedirs(jasperpath.CONFIG_PATH)
+ outputFile = open(jasperpath.config("profile.yml"), "w")
yaml.dump(profile, outputFile, default_flow_style=False)
print("Done.")
diff --git a/client/stt.py b/client/stt.py
index b4cc038..c5caf99 100644
--- a/client/stt.py
+++ b/client/stt.py
@@ -10,6 +10,7 @@ import logging
from abc import ABCMeta, abstractmethod
import requests
import yaml
+import jasperpath
"""
The default Speech-to-Text implementation which relies on PocketSphinx.
@@ -42,8 +43,8 @@ class PocketSphinxSTT(AbstractSTTEngine):
SLUG = 'sphinx'
- def __init__(self, lmd="languagemodel.lm", dictd="dictionary.dic",
- lmd_persona="languagemodel_persona.lm", dictd_persona="dictionary_persona.dic",
+ def __init__(self, lmd=jasperpath.config("languagemodel.lm"), dictd=jasperpath.config("dictionary.dic"),
+ lmd_persona=jasperpath.data("languagemodel_persona.lm"), dictd_persona=jasperpath.data("dictionary_persona.dic"),
lmd_music=None, dictd_music=None,
hmm_dir="/usr/local/share/pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k"):
"""
diff --git a/jasper.py b/jasper.py
index 93fd3c0..689f6ee 100755
--- a/jasper.py
+++ b/jasper.py
@@ -28,17 +28,44 @@ if args.local:
else:
from client.mic import Mic
-# Change CWD to jasperpath.LIB_PATH
-os.chdir(jasperpath.LIB_PATH)
-
class Jasper(object):
def __init__(self):
self._logger = logging.getLogger(__name__)
+
+ # Create config dir if it does not exist yet
+ if not os.path.exists(jasperpath.CONFIG_PATH):
+ try:
+ os.makedirs(jasperpath.CONFIG_PATH)
+ except OSError:
+ self._logger.error("Could not create config dir: '%s'", jasperpath.CONFIG_PATH, exc_info=True)
+ raise
+
+ # Check if config dir is writable
+ if not os.access(jasperpath.CONFIG_PATH, os.W_OK):
+ self._logger.critical("Config dir %s is not writable. Jasper won't work correctly.")
+
+ # FIXME: For backwards compatibility, move old config file to newly created config dir
+ old_configfile = os.path.join(jasperpath.LIB_PATH, 'profile.yml')
+ new_configfile = jasperpath.config('profile.yml')
+ if os.path.exists(old_configfile):
+ if os.path.exists(new_configfile):
+ self._logger.warning("Deprecated profile file found: '%s'. Please remove it.", old_configfile)
+ else:
+ self._logger.warning("Deprecated profile file found: '%s'. Trying to copy it to new location '%s'.", old_configfile, new_configfile)
+ try:
+ shutil.copy2(old_configfile, new_configfile)
+ except shutil.Error:
+ self._logger.error("Unable to copy config file. Please copy it manually.", exc_info=True)
+ raise
+
# Read config
- config_file = os.path.abspath(os.path.join(jasperpath.LIB_PATH, 'profile.yml'))
- self._logger.debug("Trying to read config file: '%s'", config_file)
- with open(config_file, "r") as f:
- self.config = yaml.safe_load(f)
+ self._logger.debug("Trying to read config file: '%s'", new_configfile)
+ try:
+ with open(new_configfile, "r") as f:
+ self.config = yaml.safe_load(f)
+ except OSError:
+ self._logger.error("Can't open config file: '%s'", new_configfile)
+ raise
try:
api_key = self.config['keys']['GOOGLE_SPEECH']
@@ -59,7 +86,7 @@ class Jasper(object):
tts_engine_class = tts.get_engine_by_slug(tts_engine_slug)
# Compile dictionary
- sentences, dictionary, languagemodel = [os.path.abspath(os.path.join(jasperpath.LIB_PATH, filename)) for filename in ("sentences.txt", "dictionary.dic", "languagemodel.lm")]
+ sentences, dictionary, languagemodel = [jasperpath.config(filename) for filename in ("sentences.txt", "dictionary.dic", "languagemodel.lm")]
vocabcompiler.compile(sentences, dictionary, languagemodel)
# Initialize Mic
@@ -93,14 +120,8 @@ if __name__ == "__main__":
try:
app = Jasper()
- except IOError:
- logger.exception("Can't read profile file.")
- sys.exit(1)
- except OSError:
- logger.exception("Language model or associated files missing.")
- sys.exit(1)
- except Exception():
- logger.exception("Unknown error occured")
+ except Exception:
+ logger.exception("Error occured!", exc_info=True)
sys.exit(1)
app.run()
diff --git a/client/dictionary_persona.dic b/static/dictionary_persona.dic
index 8b813ac..8b813ac 100644
--- a/client/dictionary_persona.dic
+++ b/static/dictionary_persona.dic
diff --git a/client/languagemodel_persona.lm b/static/languagemodel_persona.lm
index f290a31..f290a31 100644
--- a/client/languagemodel_persona.lm
+++ b/static/languagemodel_persona.lm