Browse Source

Beginning work moving from YAML to JSON

Mostly working for the options file now.  Still some difficulty with
command-line arguments, though; they're overriding the config file even
when not specified.  If I made them simply not get stored at all when
not specified, there would be further problems when a configuration file
is not present.  Maybe I should make a whole new class to handle this.
Clara Hobbs 8 years ago
parent
commit
5a944237bd
6 changed files with 36 additions and 33 deletions
  1. 22
    23
      blather.py
  2. 2
    0
      gtktrayui.py
  3. 2
    0
      gtkui.py
  4. 8
    0
      options.json.tmp
  5. 0
    8
      options.yaml.tmp
  6. 2
    2
      recognizer.py

+ 22
- 23
blather.py View File

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

+ 2
- 0
gtktrayui.py View File

@@ -4,8 +4,10 @@
4 4
 # Copyright 2015 Clayton G. Hobbs
5 5
 
6 6
 import sys
7
+import gi
7 8
 from gi.repository import GObject
8 9
 # Gtk
10
+gi.require_version('Gtk', '3.0')
9 11
 from gi.repository import Gtk, Gdk
10 12
 
11 13
 class UI(GObject.GObject):

+ 2
- 0
gtkui.py View File

@@ -4,8 +4,10 @@
4 4
 # Copyright 2015 Clayton G. Hobbs
5 5
 
6 6
 import sys
7
+import gi
7 8
 from gi.repository import GObject
8 9
 # Gtk
10
+gi.require_version('Gtk', '3.0')
9 11
 from gi.repository import Gtk, Gdk
10 12
 
11 13
 class UI(GObject.GObject):

+ 8
- 0
options.json.tmp View File

@@ -0,0 +1,8 @@
1
+{
2
+    "continuous": false,
3
+    "history": null,
4
+    "microphone": null,
5
+    "interface": null,
6
+    "valid_sentence_command": null,
7
+    "invalid_sentence_command": null
8
+}

+ 0
- 8
options.yaml.tmp View File

@@ -1,8 +0,0 @@
1
-#This is a YAML file
2
-#these options can be over-ridden by commandline arguments
3
-continuous: false
4
-history: null
5
-microphone: null
6
-interface: null
7
-valid_sentence_command: null
8
-invalid_sentence_command: null

+ 2
- 2
recognizer.py View File

@@ -29,9 +29,9 @@ class Recognizer(GObject.GObject):
29 29
             audio_src = 'autoaudiosrc'
30 30
 
31 31
         # Build the pipeline
32
-        cmd = audio_src+' ! audioconvert ! audioresample ! pocketsphinx name=asr ! appsink sync=false'
32
+        cmd = audio_src + ' ! audioconvert ! audioresample ! pocketsphinx name=asr ! appsink sync=false'
33 33
         try:
34
-            self.pipeline=Gst.parse_launch( cmd )
34
+            self.pipeline = Gst.parse_launch( cmd )
35 35
         except Exception as e:
36 36
             print(e.message)
37 37
             print("You may need to install gstreamer1.0-pocketsphinx")

Loading…
Cancel
Save