Pārlūkot izejas kodu

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 gadus atpakaļ
vecāks
revīzija
6347c5fdbb
2 mainītis faili ar 32 papildinājumiem un 44 dzēšanām
  1. 4
    5
      README.md
  2. 28
    39
      blather.py

+ 4
- 5
README.md Parādīt failu

8
 ## Requirements
8
 ## Requirements
9
 
9
 
10
 1. pocketsphinx
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
 **Note:** it may also be required to install `pocketsphinx-hmm-en-hub4wsj`
15
 **Note:** it may also be required to install `pocketsphinx-hmm-en-hub4wsj`
17
 
16
 
36
 8. Start talking
35
 8. Start talking
37
 
36
 
38
 **Note:** to start Kaylee without needing to enter command line options all the
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
 accordingly.
39
 accordingly.
41
 
40
 
42
 ### Bonus
41
 ### Bonus

+ 28
- 39
blather.py Parādīt failu

12
 import os.path
12
 import os.path
13
 import subprocess
13
 import subprocess
14
 from argparse import ArgumentParser
14
 from argparse import ArgumentParser
15
-from gi.repository import GObject
15
+from gi.repository import GObject, GLib
16
 import json
16
 import json
17
-try:
18
-    import yaml
19
-except:
20
-    print("YAML is not supported; unable to use config file")
21
 
17
 
22
 from recognizer import Recognizer
18
 from recognizer import Recognizer
23
 
19
 
24
 # Where are the files?
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
 lang_dir = os.path.join(conf_dir, "language")
23
 lang_dir = os.path.join(conf_dir, "language")
27
 command_file = os.path.join(conf_dir, "commands.conf")
24
 command_file = os.path.join(conf_dir, "commands.conf")
28
 strings_file = os.path.join(conf_dir, "sentences.corpus")
25
 strings_file = os.path.join(conf_dir, "sentences.corpus")
29
 history_file = os.path.join(conf_dir, "blather.history")
26
 history_file = os.path.join(conf_dir, "blather.history")
30
 opt_file = os.path.join(conf_dir, "options.json")
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
 lang_file = os.path.join(lang_dir, 'lm')
29
 lang_file = os.path.join(lang_dir, 'lm')
33
 dic_file = os.path.join(lang_dir, 'dic')
30
 dic_file = os.path.join(lang_dir, 'dic')
34
 # Make the lang_dir if it doesn't exist
31
 # Make the lang_dir if it doesn't exist
136
 
133
 
137
     def update_language(self):
134
     def update_language(self):
138
         """Update the language if its hash has changed"""
135
         """Update the language if its hash has changed"""
136
+        # Load the stored hash from the hash file
139
         try:
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
     def run_command(self, cmd):
162
     def run_command(self, cmd):
174
         """Print the command, then run it"""
163
         """Print the command, then run it"""

Notiek ielāde…
Atcelt
Saglabāt