|
@@ -1,4 +1,4 @@
|
1
|
|
-#!/usr/bin/env python2
|
|
1
|
+#!/usr/bin/env python3
|
2
|
2
|
|
3
|
3
|
# This is part of Kaylee
|
4
|
4
|
# -- this code is licensed GPLv3
|
|
@@ -11,8 +11,9 @@ import signal
|
11
|
11
|
import hashlib
|
12
|
12
|
import os.path
|
13
|
13
|
import subprocess
|
14
|
|
-from optparse import OptionParser
|
|
14
|
+from argparse import ArgumentParser
|
15
|
15
|
from gi.repository import GObject
|
|
16
|
+import json
|
16
|
17
|
try:
|
17
|
18
|
import yaml
|
18
|
19
|
except:
|
|
@@ -26,7 +27,7 @@ lang_dir = os.path.join(conf_dir, "language")
|
26
|
27
|
command_file = os.path.join(conf_dir, "commands.conf")
|
27
|
28
|
strings_file = os.path.join(conf_dir, "sentences.corpus")
|
28
|
29
|
history_file = os.path.join(conf_dir, "blather.history")
|
29
|
|
-opt_file = os.path.join(conf_dir, "options.yaml")
|
|
30
|
+opt_file = os.path.join(conf_dir, "options.json")
|
30
|
31
|
hash_file = os.path.join(conf_dir, "hash.yaml")
|
31
|
32
|
lang_file = os.path.join(lang_dir, 'lm')
|
32
|
33
|
dic_file = os.path.join(lang_dir, 'dic')
|
|
@@ -50,9 +51,11 @@ class Blather:
|
50
|
51
|
# Load the options file
|
51
|
52
|
self.load_options()
|
52
|
53
|
|
|
54
|
+ print(opts)
|
53
|
55
|
# Merge the options with the ones provided by command-line arguments
|
54
|
|
- for k, v in opts.__dict__.items():
|
55
|
|
- self.options[k] = v
|
|
56
|
+ for k, v in vars(opts).items():
|
|
57
|
+ if v is not None:
|
|
58
|
+ self.options[k] = v
|
56
|
59
|
|
57
|
60
|
if self.options['interface'] != None:
|
58
|
61
|
if self.options['interface'] == "g":
|
|
@@ -111,15 +114,11 @@ class Blather:
|
111
|
114
|
strings.close()
|
112
|
115
|
|
113
|
116
|
def load_options(self):
|
114
|
|
- """If possible, load options from the options.yaml file"""
|
|
117
|
+ """Load options from the options.json file"""
|
115
|
118
|
# Is there an opt file?
|
116
|
|
- try:
|
117
|
|
- opt_fh = open(opt_file)
|
118
|
|
- text = opt_fh.read()
|
119
|
|
- self.options = yaml.load(text)
|
120
|
|
- except:
|
121
|
|
- # Do nothing if the options file cannot be loaded
|
122
|
|
- pass
|
|
119
|
+ with open(opt_file, 'r') as f:
|
|
120
|
+ self.options = json.load(f)
|
|
121
|
+ print(self.options)
|
123
|
122
|
|
124
|
123
|
def log_history(self, text):
|
125
|
124
|
if self.options['history']:
|
|
@@ -240,38 +239,38 @@ class Blather:
|
240
|
239
|
|
241
|
240
|
|
242
|
241
|
if __name__ == "__main__":
|
243
|
|
- parser = OptionParser()
|
244
|
|
- parser.add_option("-i", "--interface", type="string", dest="interface",
|
|
242
|
+ parser = ArgumentParser()
|
|
243
|
+ parser.add_argument("-i", "--interface", type=str, dest="interface",
|
245
|
244
|
action='store',
|
246
|
245
|
help="Interface to use (if any). 'g' for GTK or 'gt' for GTK system tray icon")
|
247
|
246
|
|
248
|
|
- parser.add_option("-c", "--continuous",
|
|
247
|
+ parser.add_argument("-c", "--continuous",
|
249
|
248
|
action="store_true", dest="continuous", default=False,
|
250
|
249
|
help="starts interface with 'continuous' listen enabled")
|
251
|
250
|
|
252
|
|
- parser.add_option("-p", "--pass-words",
|
|
251
|
+ parser.add_argument("-p", "--pass-words",
|
253
|
252
|
action="store_true", dest="pass_words", default=False,
|
254
|
253
|
help="passes the recognized words as arguments to the shell command")
|
255
|
254
|
|
256
|
|
- parser.add_option("-H", "--history", type="int",
|
|
255
|
+ parser.add_argument("-H", "--history", type=int,
|
257
|
256
|
action="store", dest="history",
|
258
|
257
|
help="number of commands to store in history file")
|
259
|
258
|
|
260
|
|
- parser.add_option("-m", "--microphone", type="int",
|
|
259
|
+ parser.add_argument("-m", "--microphone", type=int,
|
261
|
260
|
action="store", dest="microphone", default=None,
|
262
|
261
|
help="Audio input card to use (if other than system default)")
|
263
|
262
|
|
264
|
|
- parser.add_option("--valid-sentence-command", type="string", dest="valid_sentence_command",
|
|
263
|
+ parser.add_argument("--valid-sentence-command", type=str, dest="valid_sentence_command",
|
265
|
264
|
action='store',
|
266
|
265
|
help="command to run when a valid sentence is detected")
|
267
|
266
|
|
268
|
|
- parser.add_option( "--invalid-sentence-command", type="string", dest="invalid_sentence_command",
|
|
267
|
+ parser.add_argument( "--invalid-sentence-command", type=str, dest="invalid_sentence_command",
|
269
|
268
|
action='store',
|
270
|
269
|
help="command to run when an invalid sentence is detected")
|
271
|
270
|
|
272
|
|
- (options, args) = parser.parse_args()
|
|
271
|
+ args = parser.parse_args()
|
273
|
272
|
# Make our blather object
|
274
|
|
- blather = Blather(options)
|
|
273
|
+ blather = Blather(args)
|
275
|
274
|
# Init gobject threads
|
276
|
275
|
GObject.threads_init()
|
277
|
276
|
# We want a main loop
|