Browse Source

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.
Clara Hobbs 8 years ago
parent
commit
6347c5fdbb
2 changed files with 32 additions and 44 deletions
  1. 4
    5
      README.md
  2. 28
    39
      blather.py

+ 4
- 5
README.md View File

@@ -8,10 +8,9 @@ but adds a lot of features that go beyond the original purpose of Blather.
8 8
 ## Requirements
9 9
 
10 10
 1. pocketsphinx
11
-2. gstreamer-1.0 (and what ever plugin has pocket sphinx support)
12
-3. gstreamer-1.0 base plugins (required for alsa)
13
-5. pygtk (only required for the Gtk based UI)
14
-6. pyyaml (only required for reading the options file)
11
+2. gstreamer-1.0 (and what ever plugin has pocketsphinx support)
12
+3. gstreamer-1.0 base plugins (required for ALSA)
13
+4. python-gobject (required for GStreamer and the GTK-based UI)
15 14
 
16 15
 **Note:** it may also be required to install `pocketsphinx-hmm-en-hub4wsj`
17 16
 
@@ -36,7 +35,7 @@ directory and rename to file to 'dic'
36 35
 8. Start talking
37 36
 
38 37
 **Note:** to start Kaylee without needing to enter command line options all the
39
-time, copy options.yaml.tmp to ~/.config/blather/options.yaml and edit
38
+time, copy options.json.tmp to ~/.config/blather/options.json and edit
40 39
 accordingly.
41 40
 
42 41
 ### Bonus

+ 28
- 39
blather.py View File

@@ -12,23 +12,20 @@ import hashlib
12 12
 import os.path
13 13
 import subprocess
14 14
 from argparse import ArgumentParser
15
-from gi.repository import GObject
15
+from gi.repository import GObject, GLib
16 16
 import json
17
-try:
18
-    import yaml
19
-except:
20
-    print("YAML is not supported; unable to use config file")
21 17
 
22 18
 from recognizer import Recognizer
23 19
 
24 20
 # Where are the files?
25
-conf_dir = os.path.expanduser("~/.config/blather")
21
+conf_dir = os.path.expanduser(os.path.join(GLib.get_user_config_dir(),
22
+                                           "blather"))
26 23
 lang_dir = os.path.join(conf_dir, "language")
27 24
 command_file = os.path.join(conf_dir, "commands.conf")
28 25
 strings_file = os.path.join(conf_dir, "sentences.corpus")
29 26
 history_file = os.path.join(conf_dir, "blather.history")
30 27
 opt_file = os.path.join(conf_dir, "options.json")
31
-hash_file = os.path.join(conf_dir, "hash.yaml")
28
+hash_file = os.path.join(conf_dir, "hash.json")
32 29
 lang_file = os.path.join(lang_dir, 'lm')
33 30
 dic_file = os.path.join(lang_dir, 'dic')
34 31
 # Make the lang_dir if it doesn't exist
@@ -136,39 +133,31 @@ class Blather:
136 133
 
137 134
     def update_language(self):
138 135
         """Update the language if its hash has changed"""
136
+        # Load the stored hash from the hash file
139 137
         try:
140
-            # Load the stored hash from the hash file
141
-            try:
142
-                with open(hash_file, 'r') as f:
143
-                    text = f.read()
144
-                    hashes = yaml.load(text)
145
-                stored_hash = hashes['language']
146
-            except (IOError, KeyError, TypeError):
147
-                # No stored hash
148
-                stored_hash = ''
149
-
150
-            # Calculate the hash the language file has right now
151
-            hasher = hashlib.sha256()
152
-            with open(strings_file, 'rb') as sfile:
153
-                buf = sfile.read()
154
-                hasher.update(buf)
155
-            new_hash = hasher.hexdigest()
156
-
157
-            # If the hashes differ
158
-            if stored_hash != new_hash:
159
-                # Update the language
160
-                # FIXME: Do this with Python, not Bash
161
-                self.run_command('./language_updater.sh')
162
-                # Store the new hash
163
-                new_hashes = {'language': new_hash}
164
-                with open(hash_file, 'w') as f:
165
-                    f.write(yaml.dump(new_hashes))
166
-        except Exception as e:
167
-            # Do nothing if the hash file cannot be loaded
168
-            # FIXME: This is kind of bad; maybe YAML should be mandatory.
169
-            print('error updating language')
170
-            print(e)
171
-            pass
138
+            with open(hash_file, 'r') as f:
139
+                hashes = json.load(f)
140
+            stored_hash = hashes['language']
141
+        except (IOError, KeyError, TypeError):
142
+            # No stored hash
143
+            stored_hash = ''
144
+
145
+        # Calculate the hash the language file has right now
146
+        hasher = hashlib.sha256()
147
+        with open(strings_file, 'rb') as sfile:
148
+            buf = sfile.read()
149
+            hasher.update(buf)
150
+        new_hash = hasher.hexdigest()
151
+
152
+        # If the hashes differ
153
+        if stored_hash != new_hash:
154
+            # Update the language
155
+            # FIXME: Do this with Python, not Bash
156
+            self.run_command('./language_updater.sh')
157
+            # Store the new hash
158
+            new_hashes = {'language': new_hash}
159
+            with open(hash_file, 'w') as f:
160
+                json.dump(new_hashes, f)
172 161
 
173 162
     def run_command(self, cmd):
174 163
         """Print the command, then run it"""

Loading…
Cancel
Save