# This is part of Kaylee # -- this code is licensed GPLv3 # Copyright 2015-2016 Clayton G. Hobbs # Portions Copyright 2013 Jezra import subprocess from .pluginbase import PluginBase from ..numbers import NumberParser class Plugin(PluginBase): def __init__(self, config): super().__init__(config) self.options = vars(self.config.options) self.number_parser = NumberParser() self.commands = self.options['plugins']['.shell'] for voice_cmd in self.commands.keys(): self.corpus_strings.add(voice_cmd.strip().replace('%d', '')) def _run_command(self, cmd): """Print the command, then run it""" print(cmd) subprocess.call(cmd, shell=True) def recognizer_finished(self, recognizer, text): """Process a recognized voice command Recognized commands have the corresponding shell commands executed, possibly with number(s) substituted. """ numt, nums = self.number_parser.parse_all_numbers(text) # Is there a matching command? if text in self.commands: # Run the valid_sentence_command if it's set # TODO: determine how to support this with plugins if self.options['valid_sentence_command']: subprocess.call(self.options['valid_sentence_command'], shell=True) cmd = self.commands[text] # Should we be passing words? # TODO: configuration changes to make this not ridiculous if self.options['pass_words']: cmd += " " + text self._run_command(cmd) # TODO: Logging can be handled along with valid_sentence_command #self.log_history(text) return True elif numt in self.commands: # Run the valid_sentence_command if it's set if self.options['valid_sentence_command']: subprocess.call(self.options['valid_sentence_command'], shell=True) cmd = self.commands[numt] cmd = cmd.format(*nums) # Should we be passing words? if self.options['pass_words']: cmd += " " + text self._run_command(cmd) #self.log_history(text) return True else: # TODO: This could be implemented as a plugin, implicitly loaded # last # Run the invalid_sentence_command if it's set if self.options['invalid_sentence_command']: subprocess.call(self.options['invalid_sentence_command'], shell=True) print("no matching command {0}".format(text)) return False # TODO: Make the D-Bus interface so this can leave the main process ## If there is a UI and we are not continuous listen #if self.ui: # if not self.continuous_listen: # # Stop listening # self.recognizer.pause() # # Let the UI know that there is a finish # self.ui.finished(text)