|
@@ -3,15 +3,16 @@
|
3
|
3
|
# Copyright 2015-2016 Clayton G. Hobbs
|
4
|
4
|
# Portions Copyright 2013 Jezra
|
5
|
5
|
|
|
6
|
+import importlib
|
6
|
7
|
import sys
|
7
|
8
|
import signal
|
8
|
9
|
import os.path
|
9
|
|
-import subprocess
|
10
|
10
|
from gi.repository import GObject, GLib
|
11
|
11
|
|
12
|
12
|
from kayleevc.recognizer import Recognizer
|
13
|
13
|
from kayleevc.util import *
|
14
|
14
|
from kayleevc.numbers import NumberParser
|
|
15
|
+import kayleevc.plugins
|
15
|
16
|
|
16
|
17
|
|
17
|
18
|
class Kaylee:
|
|
@@ -24,10 +25,13 @@ class Kaylee:
|
24
|
25
|
# Load configuration
|
25
|
26
|
self.config = Config()
|
26
|
27
|
self.options = vars(self.config.options)
|
27
|
|
- self.commands = self.options['commands']
|
|
28
|
+ self.commands = self.options['plugins']['.shell']
|
28
|
29
|
|
29
|
|
- # Create number parser for later use
|
30
|
|
- self.number_parser = NumberParser()
|
|
30
|
+ # Load plugins
|
|
31
|
+ self.plugins = []
|
|
32
|
+ for plugin in self.options['plugins'].keys():
|
|
33
|
+ pmod = importlib.import_module(plugin, 'kayleevc.plugins')
|
|
34
|
+ self.plugins.append(pmod.Plugin(self.config))
|
31
|
35
|
|
32
|
36
|
# Create a hasher
|
33
|
37
|
self.hasher = Hasher(self.config)
|
|
@@ -64,7 +68,10 @@ class Kaylee:
|
64
|
68
|
|
65
|
69
|
# Create the recognizer
|
66
|
70
|
self.recognizer = Recognizer(self.config)
|
67
|
|
- self.recognizer.connect('finished', self.recognizer_finished)
|
|
71
|
+
|
|
72
|
+ # Connect the recognizer's finished signal to all the plugins
|
|
73
|
+ for plugin in self.plugins:
|
|
74
|
+ self.recognizer.connect('finished', plugin.recognizer_finished)
|
68
|
75
|
|
69
|
76
|
def update_voice_commands_if_changed(self):
|
70
|
77
|
"""Use hashes to test if the voice commands have changed"""
|
|
@@ -72,10 +79,11 @@ class Kaylee:
|
72
|
79
|
|
73
|
80
|
# Calculate the hash the voice commands have right now
|
74
|
81
|
hasher = self.hasher.get_hash_object()
|
75
|
|
- for voice_cmd in self.commands.keys():
|
76
|
|
- hasher.update(voice_cmd.encode('utf-8'))
|
77
|
|
- # Add a separator to avoid odd behavior
|
78
|
|
- hasher.update('\n'.encode('utf-8'))
|
|
82
|
+ for plugin in self.plugins:
|
|
83
|
+ for string in sorted(plugin.corpus_strings):
|
|
84
|
+ hasher.update(string.encode('utf-8'))
|
|
85
|
+ # Add a separator to avoid odd behavior
|
|
86
|
+ hasher.update('\n'.encode('utf-8'))
|
79
|
87
|
new_hash = hasher.hexdigest()
|
80
|
88
|
|
81
|
89
|
if new_hash != stored_hash:
|
|
@@ -87,11 +95,14 @@ class Kaylee:
|
87
|
95
|
# Open the strings file
|
88
|
96
|
with open(self.config.strings_file, 'w') as strings:
|
89
|
97
|
# Add command words to the corpus
|
90
|
|
- for voice_cmd in sorted(self.commands.keys()):
|
91
|
|
- strings.write(voice_cmd.strip().replace('%d', '') + "\n")
|
|
98
|
+ # FIXME: Doing this twice is a silly thing
|
|
99
|
+ for plugin in self.plugins:
|
|
100
|
+ for string in sorted(plugin.corpus_strings):
|
|
101
|
+ strings.write(string + "\n")
|
92
|
102
|
# Add number words to the corpus
|
93
|
|
- for word in self.number_parser.number_words:
|
94
|
|
- strings.write(word + " ")
|
|
103
|
+ if NumberParser.number_words is not None:
|
|
104
|
+ for word in NumberParser.number_words:
|
|
105
|
+ strings.write(word + " ")
|
95
|
106
|
strings.write("\n")
|
96
|
107
|
|
97
|
108
|
def log_history(self, text):
|
|
@@ -106,51 +117,8 @@ class Kaylee:
|
106
|
117
|
for line in self.history:
|
107
|
118
|
hfile.write(line + '\n')
|
108
|
119
|
|
109
|
|
- def run_command(self, cmd):
|
110
|
|
- """Print the command, then run it"""
|
111
|
|
- print(cmd)
|
112
|
|
- subprocess.call(cmd, shell=True)
|
113
|
|
-
|
114
|
120
|
def recognizer_finished(self, recognizer, text):
|
115
|
|
- t = text.lower()
|
116
|
|
- numt, nums = self.number_parser.parse_all_numbers(t)
|
117
|
|
- # Is there a matching command?
|
118
|
|
- if t in self.commands:
|
119
|
|
- # Run the valid_sentence_command if it's set
|
120
|
|
- if self.options['valid_sentence_command']:
|
121
|
|
- subprocess.call(self.options['valid_sentence_command'],
|
122
|
|
- shell=True)
|
123
|
|
- cmd = self.commands[t]
|
124
|
|
- # Should we be passing words?
|
125
|
|
- if self.options['pass_words']:
|
126
|
|
- cmd += " " + t
|
127
|
|
- self.run_command(cmd)
|
128
|
|
- self.log_history(text)
|
129
|
|
- elif numt in self.commands:
|
130
|
|
- # Run the valid_sentence_command if it's set
|
131
|
|
- if self.options['valid_sentence_command']:
|
132
|
|
- subprocess.call(self.options['valid_sentence_command'],
|
133
|
|
- shell=True)
|
134
|
|
- cmd = self.commands[numt]
|
135
|
|
- cmd = cmd.format(*nums)
|
136
|
|
- # Should we be passing words?
|
137
|
|
- if self.options['pass_words']:
|
138
|
|
- cmd += " " + t
|
139
|
|
- self.run_command(cmd)
|
140
|
|
- self.log_history(text)
|
141
|
|
- else:
|
142
|
|
- # Run the invalid_sentence_command if it's set
|
143
|
|
- if self.options['invalid_sentence_command']:
|
144
|
|
- subprocess.call(self.options['invalid_sentence_command'],
|
145
|
|
- shell=True)
|
146
|
|
- print("no matching command {0}".format(t))
|
147
|
|
- # If there is a UI and we are not continuous listen
|
148
|
|
- if self.ui:
|
149
|
|
- if not self.continuous_listen:
|
150
|
|
- # Stop listening
|
151
|
|
- self.recognizer.pause()
|
152
|
|
- # Let the UI know that there is a finish
|
153
|
|
- self.ui.finished(t)
|
|
121
|
+ pass
|
154
|
122
|
|
155
|
123
|
def run(self):
|
156
|
124
|
if self.ui:
|