summary refs log tree commit diff
diff options
context:
space:
mode:
authorClayton G. Hobbs <clay@lakeserv.net>2015-12-27 12:12:19 -0500
committerClayton G. Hobbs <clay@lakeserv.net>2015-12-27 12:12:19 -0500
commit6347c5fdbb9b963dbd6c2cb5541ef047468806b4 (patch)
tree2eb12a8ba207ca8b870fe6b6d61a231b0ca35e04
parent5a944237bdc6bdbdb3d630be8819373c3b9508dd (diff)
Replaced last YAML bit with JSON
Hashes are now stored in hash.json, not hash.yaml.  Also, we use the XDG
configuration directory rather than assuming we should use ~/.config/.
Maybe I should also make use of XDG data and/or cache directories.
-rw-r--r--README.md9
-rwxr-xr-xblather.py67
2 files changed, 32 insertions, 44 deletions
diff --git a/README.md b/README.md
index bb0b409..b2949b6 100644
--- a/README.md
+++ b/README.md
@@ -8,10 +8,9 @@ but adds a lot of features that go beyond the original purpose of Blather.
 ## Requirements
 
 1. pocketsphinx
-2. gstreamer-1.0 (and what ever plugin has pocket sphinx support)
-3. gstreamer-1.0 base plugins (required for alsa)
-5. pygtk (only required for the Gtk based UI)
-6. pyyaml (only required for reading the options file)
+2. gstreamer-1.0 (and what ever plugin has pocketsphinx support)
+3. gstreamer-1.0 base plugins (required for ALSA)
+4. python-gobject (required for GStreamer and the GTK-based UI)
 
 **Note:** it may also be required to install `pocketsphinx-hmm-en-hub4wsj`
 
@@ -36,7 +35,7 @@ directory and rename to file to 'dic'
 8. Start talking
 
 **Note:** to start Kaylee without needing to enter command line options all the
-time, copy options.yaml.tmp to ~/.config/blather/options.yaml and edit
+time, copy options.json.tmp to ~/.config/blather/options.json and edit
 accordingly.
 
 ### Bonus
diff --git a/blather.py b/blather.py
index a31f1ec..b1c3777 100755
--- a/blather.py
+++ b/blather.py
@@ -12,23 +12,20 @@ import hashlib
 import os.path
 import subprocess
 from argparse import ArgumentParser
-from gi.repository import GObject
+from gi.repository import GObject, GLib
 import json
-try:
-    import yaml
-except:
-    print("YAML is not supported; unable to use config file")
 
 from recognizer import Recognizer
 
 # Where are the files?
-conf_dir = os.path.expanduser("~/.config/blather")
+conf_dir = os.path.expanduser(os.path.join(GLib.get_user_config_dir(),
+                                           "blather"))
 lang_dir = os.path.join(conf_dir, "language")
 command_file = os.path.join(conf_dir, "commands.conf")
 strings_file = os.path.join(conf_dir, "sentences.corpus")
 history_file = os.path.join(conf_dir, "blather.history")
 opt_file = os.path.join(conf_dir, "options.json")
-hash_file = os.path.join(conf_dir, "hash.yaml")
+hash_file = os.path.join(conf_dir, "hash.json")
 lang_file = os.path.join(lang_dir, 'lm')
 dic_file = os.path.join(lang_dir, 'dic')
 # Make the lang_dir if it doesn't exist
@@ -136,39 +133,31 @@ class Blather:
 
     def update_language(self):
         """Update the language if its hash has changed"""
+        # Load the stored hash from the hash file
         try:
-            # Load the stored hash from the hash file
-            try:
-                with open(hash_file, 'r') as f:
-                    text = f.read()
-                    hashes = yaml.load(text)
-                stored_hash = hashes['language']
-            except (IOError, KeyError, TypeError):
-                # No stored hash
-                stored_hash = ''
-
-            # Calculate the hash the language file has right now
-            hasher = hashlib.sha256()
-            with open(strings_file, 'rb') as sfile:
-                buf = sfile.read()
-                hasher.update(buf)
-            new_hash = hasher.hexdigest()
-
-            # If the hashes differ
-            if stored_hash != new_hash:
-                # Update the language
-                # FIXME: Do this with Python, not Bash
-                self.run_command('./language_updater.sh')
-                # Store the new hash
-                new_hashes = {'language': new_hash}
-                with open(hash_file, 'w') as f:
-                    f.write(yaml.dump(new_hashes))
-        except Exception as e:
-            # Do nothing if the hash file cannot be loaded
-            # FIXME: This is kind of bad; maybe YAML should be mandatory.
-            print('error updating language')
-            print(e)
-            pass
+            with open(hash_file, 'r') as f:
+                hashes = json.load(f)
+            stored_hash = hashes['language']
+        except (IOError, KeyError, TypeError):
+            # No stored hash
+            stored_hash = ''
+
+        # Calculate the hash the language file has right now
+        hasher = hashlib.sha256()
+        with open(strings_file, 'rb') as sfile:
+            buf = sfile.read()
+            hasher.update(buf)
+        new_hash = hasher.hexdigest()
+
+        # If the hashes differ
+        if stored_hash != new_hash:
+            # Update the language
+            # FIXME: Do this with Python, not Bash
+            self.run_command('./language_updater.sh')
+            # Store the new hash
+            new_hashes = {'language': new_hash}
+            with open(hash_file, 'w') as f:
+                json.dump(new_hashes, f)
 
     def run_command(self, cmd):
         """Print the command, then run it"""