|
@@ -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"""
|