Selaa lähdekoodia

Reads default startup options from an options.yaml file

Added some missing files because I'm an idiot
Jezra 10 vuotta sitten
vanhempi
commit
810d17c7a6
5 muutettua tiedostoa jossa 94 lisäystä ja 22 poistoa
  1. 40
    14
      Blather.py
  2. 20
    2
      GtkUI.py
  3. 19
    0
      QtUI.py
  4. 9
    6
      README.md
  5. 6
    0
      options.yaml.tmp

+ 40
- 14
Blather.py Näytä tiedosto

@@ -9,7 +9,10 @@ import gobject
9 9
 import os.path
10 10
 import subprocess
11 11
 from optparse import OptionParser
12
-
12
+try:
13
+	import yaml
14
+except:
15
+	print "YAML is not supported. ~/.config/blather/options.yaml will not function"
13 16
 
14 17
 #where are the files?
15 18
 conf_dir = os.path.expanduser("~/.config/blather")
@@ -17,6 +20,7 @@ lang_dir = os.path.join(conf_dir, "language")
17 20
 command_file = os.path.join(conf_dir, "commands.conf")
18 21
 strings_file = os.path.join(conf_dir, "sentences.corpus")
19 22
 history_file = os.path.join(conf_dir, "blather.history")
23
+opt_file = os.path.join(conf_dir, "options.yaml")
20 24
 lang_file = os.path.join(lang_dir,'lm')
21 25
 dic_file = os.path.join(lang_dir,'dic')
22 26
 #make the lang_dir if it doesn't exist
@@ -28,8 +32,7 @@ class Blather:
28 32
 		#import the recognizer so Gst doesn't clobber our -h
29 33
 		from Recognizer import Recognizer
30 34
 		self.ui = None
31
-		#keep track of the opts
32
-		self.opts = opts
35
+		self.options = {}
33 36
 		ui_continuous_listen = False
34 37
 		self.continuous_listen = False
35 38
 		self.commands = {}
@@ -37,27 +40,40 @@ class Blather:
37 40
 		self.recognizer = Recognizer(lang_file, dic_file, opts.microphone )
38 41
 		self.recognizer.connect('finished',self.recognizer_finished)
39 42
 
40
-		if opts.interface != None:
41
-			if opts.interface == "q":
42
-				#import the ui from qt
43
+		#load the options file
44
+		self.load_options()
45
+		#merge the opts
46
+		for k,v in opts.__dict__.items():
47
+			if not k in self.options:
48
+				self.options[k] = v
49
+
50
+		print "Using Options: ", self.options
51
+
52
+		if self.options['interface'] != None:
53
+			if self.options['interface'] == "q":
43 54
 				from QtUI import UI
44
-			elif opts.interface == "g":
55
+			elif self.options['interface'] == "g":
45 56
 				from GtkUI import UI
57
+			elif self.options['interface'] == "gt":
58
+				from GtkTrayUI import UI
46 59
 			else:
47 60
 				print "no GUI defined"
48 61
 				sys.exit()
49 62
 
50
-			self.ui = UI(args,opts.continuous)
63
+			self.ui = UI(args, self.options['continuous'])
51 64
 			self.ui.connect("command", self.process_command)
52 65
 			#can we load the icon resource?
53 66
 			icon = self.load_resource("icon.png")
54 67
 			if icon:
55
-				self.ui.set_icon(icon)
68
+				self.ui.set_icon_active_asset(icon)
69
+			#can we load the icon_inactive resource?
70
+			icon_inactive = self.load_resource("icon_inactive.png")
71
+			if icon_inactive:
72
+				self.ui.set_icon_inactive_asset(icon_inactive)
56 73
 
57
-		if self.opts.history:
74
+		if self.options['history']:
58 75
 			self.history = []
59 76
 
60
-
61 77
 	def read_commands(self):
62 78
 		#read the.commands file
63 79
 		file_lines = open(command_file)
@@ -76,10 +92,20 @@ class Blather:
76 92
 		#close the strings file
77 93
 		strings.close()
78 94
 
95
+	def load_options(self):
96
+		#is there an opt file?
97
+		try:
98
+			opt_fh = open(opt_file)
99
+			text = opt_fh.read()
100
+			self.options = yaml.load(text)
101
+		except:
102
+			pass
103
+
104
+
79 105
 	def log_history(self,text):
80
-		if self.opts.history:
106
+		if self.options['history']:
81 107
 			self.history.append(text)
82
-			if len(self.history) > self.opts.history:
108
+			if len(self.history) > self.options['history']:
83 109
 				#pop off the first item
84 110
 				self.history.pop(0)
85 111
 
@@ -148,7 +174,7 @@ if __name__ == "__main__":
148 174
 	parser = OptionParser()
149 175
 	parser.add_option("-i", "--interface",  type="string", dest="interface",
150 176
 		action='store',
151
-		help="Interface to use (if any). 'q' for Qt, 'g' for GTK")
177
+		help="Interface to use (if any). 'q' for Qt, 'g' for GTK, 'gt' for GTK system tray icon")
152 178
 	parser.add_option("-c", "--continuous",
153 179
 		action="store_true", dest="continuous", default=False,
154 180
 		help="starts interface with 'continuous' listen enabled")

+ 20
- 2
GtkUI.py Näytä tiedosto

@@ -54,8 +54,10 @@ class UI(gobject.GObject):
54 54
 		if checked:
55 55
 			self.lsbutton_stopped()
56 56
 			self.emit('command', "continuous_listen")
57
+			self.set_icon_active()
57 58
 		else:
58 59
 			self.emit('command', "continuous_stop")
60
+			self.set_icon_inactive()
59 61
 
60 62
 	def lsbutton_stopped(self):
61 63
 		self.lsbutton.set_label("Listen")
@@ -67,13 +69,18 @@ class UI(gobject.GObject):
67 69
 			self.lsbutton.set_label("Stop")
68 70
 			#clear the label
69 71
 			self.label.set_text("")
72
+			self.set_icon_active()
70 73
 		else:
71 74
 			self.lsbutton_stopped()
72 75
 			self.emit("command", "stop")
76
+			self.set_icon_inactive()
73 77
 
74 78
 	def run(self):
79
+		#set the default icon
80
+		self.set_icon_inactive()
75 81
 		self.window.show_all()
76 82
 		if self.continuous:
83
+			self.set_icon_active()
77 84
 			self.ccheckbox.set_active(True)
78 85
 
79 86
 	def accel_quit(self, accel_group, acceleratable, keyval, modifier):
@@ -87,8 +94,19 @@ class UI(gobject.GObject):
87 94
 		#if the continuous isn't pressed
88 95
 		if not self.ccheckbox.get_active():
89 96
 			self.lsbutton_stopped()
97
+			self.set_icon_inactive()
90 98
 		self.label.set_text(text)
91 99
 
92
-	def set_icon(self, icon):
93
-		gtk.window_set_default_icon_from_file(icon)
100
+	def set_icon_active_asset(self, i):
101
+		self.icon_active = i
102
+
103
+	def set_icon_inactive_asset(self, i):
104
+		self.icon_inactive = i
105
+
106
+	def set_icon_active(self):
107
+		gtk.window_set_default_icon_from_file(self.icon_active)
108
+
109
+	def set_icon_inactive(self):
110
+		gtk.window_set_default_icon_from_file(self.icon_inactive)
111
+
94 112
 

+ 19
- 0
QtUI.py Näytä tiedosto

@@ -60,9 +60,11 @@ class UI(gobject.GObject):
60 60
 			self.lsbutton.setEnabled(False)
61 61
 			self.lsbutton_stopped()
62 62
 			self.emit('command', "continuous_listen")
63
+			self.set_icon_active()
63 64
 		else:
64 65
 			self.lsbutton.setEnabled(True)
65 66
 			self.emit('command', "continuous_stop")
67
+			self.set_icon_inactive()
66 68
 
67 69
 	def lsbutton_stopped(self):
68 70
 		self.lsbutton.setText("Listen")
@@ -74,13 +76,17 @@ class UI(gobject.GObject):
74 76
 			self.lsbutton.setText("Stop")
75 77
 			#clear the label
76 78
 			self.label.setText("")
79
+			self.set_icon_active()
77 80
 		else:
78 81
 			self.lsbutton_stopped()
79 82
 			self.emit("command", "stop")
83
+			self.set_icon_inactive()
80 84
 
81 85
 	def run(self):
86
+		self.set_icon_inactive()
82 87
 		self.window.show()
83 88
 		if self.continuous:
89
+			self.set_icon_active()
84 90
 			self.ccheckbox.setCheckState(Qt.Checked)
85 91
 			self.ccheckbox_clicked()
86 92
 		self.app.exec_()
@@ -95,3 +101,16 @@ class UI(gobject.GObject):
95 101
 
96 102
 	def set_icon(self, icon):
97 103
 		self.window.setWindowIcon(QIcon(icon))
104
+
105
+	def set_icon_active_asset(self, i):
106
+		self.icon_active = i
107
+
108
+	def set_icon_inactive_asset(self, i):
109
+		self.icon_inactive = i
110
+
111
+	def set_icon_active(self):
112
+		self.window.setWindowIcon(QIcon(self.icon_active))
113
+
114
+	def set_icon_inactive(self):
115
+		self.window.setWindowIcon(QIcon(self.icon_inactive))
116
+

README → README.md Näytä tiedosto

@@ -7,6 +7,7 @@ Blather is a speech recognizer that will run commands when a user speaks preset
7 7
 3. gstreamer-0.10 base plugins (required for alsa)
8 8
 4. pyside (only required for the Qt based UI)
9 9
 5. pygtk (only required for the Gtk based UI)
10
+6. pyyaml (only required for reading the options file)
10 11
 
11 12
 ##Usage
12 13
 0. move commands.tmp to ~/.config/blather/commands.conf and fill the file with sentences and command to run
@@ -19,20 +20,22 @@ Blather is a speech recognizer that will run commands when a user speaks preset
19 20
     * for Qt GUI, run Blather.py -i q
20 21
     * for Gtk GUI, run Blather.py -i g
21 22
     * to start a UI in 'continuous' listen mode, use the -c flag
22
-    * to use a microphone other than the system default, use the -d flag
23
+    * to use a microphone other than the system default, use the -m flag
23 24
 7. start talking
24 25
 
25
-####Bonus
26
+**Note:** to start Blather without needing to enter command line options all the time, copy options.yaml.tmp to ~/.config/blather/options.yaml and edit accordingly.
27
+
28
+###Bonus
26 29
 once the sentences.corpus file has been created, run the language_updater.sh script to automate the process of creating and downloading language files.
27 30
 
28
-####Examples
31
+###Examples
29 32
 To run blather with the GTK UI and start in continuous listen mode:
30
-./Blather.py -i g -c
33
+`./Blather.py -i g -c`
31 34
 
32 35
 To run blather with no UI and using a USB microphone recognized and device 2:
33
-./Blather.py -d 2
36
+`./Blather.py -m 2`
34 37
 
35
-####Finding the Device Number of a USB microphone
38
+###Finding the Device Number of a USB microphone
36 39
 There are a few ways to find the device number of a USB microphone.
37 40
 
38 41
 * `cat /proc/asound/cards`

+ 6
- 0
options.yaml.tmp Näytä tiedosto

@@ -0,0 +1,6 @@
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

Loading…
Peruuta
Tallenna