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
-#!/usr/bin/env python2
1
+#!/usr/bin/env python3
2
 
2
 
3
 # This is part of Kaylee
3
 # This is part of Kaylee
4
 # -- this code is licensed GPLv3
4
 # -- this code is licensed GPLv3
11
 import hashlib
11
 import hashlib
12
 import os.path
12
 import os.path
13
 import subprocess
13
 import subprocess
14
-from optparse import OptionParser
14
+from argparse import ArgumentParser
15
 from gi.repository import GObject
15
 from gi.repository import GObject
16
+import json
16
 try:
17
 try:
17
     import yaml
18
     import yaml
18
 except:
19
 except:
26
 command_file = os.path.join(conf_dir, "commands.conf")
27
 command_file = os.path.join(conf_dir, "commands.conf")
27
 strings_file = os.path.join(conf_dir, "sentences.corpus")
28
 strings_file = os.path.join(conf_dir, "sentences.corpus")
28
 history_file = os.path.join(conf_dir, "blather.history")
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
 hash_file = os.path.join(conf_dir, "hash.yaml")
31
 hash_file = os.path.join(conf_dir, "hash.yaml")
31
 lang_file = os.path.join(lang_dir, 'lm')
32
 lang_file = os.path.join(lang_dir, 'lm')
32
 dic_file = os.path.join(lang_dir, 'dic')
33
 dic_file = os.path.join(lang_dir, 'dic')
50
         # Load the options file
51
         # Load the options file
51
         self.load_options()
52
         self.load_options()
52
 
53
 
54
+        print(opts)
53
         # Merge the options with the ones provided by command-line arguments
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
         if self.options['interface'] != None:
60
         if self.options['interface'] != None:
58
             if self.options['interface'] == "g":
61
             if self.options['interface'] == "g":
111
         strings.close()
114
         strings.close()
112
 
115
 
113
     def load_options(self):
116
     def load_options(self):
114
-        """If possible, load options from the options.yaml file"""
117
+        """Load options from the options.json file"""
115
         # Is there an opt file?
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
     def log_history(self, text):
123
     def log_history(self, text):
125
         if self.options['history']:
124
         if self.options['history']:
240
 
239
 
241
 
240
 
242
 if __name__ == "__main__":
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
             action='store',
244
             action='store',
246
             help="Interface to use (if any). 'g' for GTK or 'gt' for GTK system tray icon")
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
             action="store_true", dest="continuous", default=False,
248
             action="store_true", dest="continuous", default=False,
250
             help="starts interface with 'continuous' listen enabled")
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
             action="store_true", dest="pass_words", default=False,
252
             action="store_true", dest="pass_words", default=False,
254
             help="passes the recognized words as arguments to the shell command")
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
             action="store", dest="history",
256
             action="store", dest="history",
258
             help="number of commands to store in history file")
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
             action="store", dest="microphone", default=None,
260
             action="store", dest="microphone", default=None,
262
             help="Audio input card to use (if other than system default)")
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
             action='store',
264
             action='store',
266
             help="command to run when a valid sentence is detected")
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
             action='store',
268
             action='store',
270
             help="command to run when an invalid sentence is detected")
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
     # Make our blather object
272
     # Make our blather object
274
-    blather = Blather(options)
273
+    blather = Blather(args)
275
     # Init gobject threads
274
     # Init gobject threads
276
     GObject.threads_init()
275
     GObject.threads_init()
277
     # We want a main loop
276
     # We want a main loop

+ 2
- 0
gtktrayui.py View File

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

+ 2
- 0
gtkui.py View File

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

+ 8
- 0
options.json.tmp View File

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
-#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
             audio_src = 'autoaudiosrc'
29
             audio_src = 'autoaudiosrc'
30
 
30
 
31
         # Build the pipeline
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
         try:
33
         try:
34
-            self.pipeline=Gst.parse_launch( cmd )
34
+            self.pipeline = Gst.parse_launch( cmd )
35
         except Exception as e:
35
         except Exception as e:
36
             print(e.message)
36
             print(e.message)
37
             print("You may need to install gstreamer1.0-pocketsphinx")
37
             print("You may need to install gstreamer1.0-pocketsphinx")

Loading…
Cancel
Save