# 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, name): super().__init__(config, name) self.options = vars(self.config.options) self.number_parser = NumberParser() self.commands = self.options['plugins'][name] 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: self.emit('processed', text) cmd = self.commands[text] self._run_command(cmd) return True elif numt in self.commands: self.emit('processed', text) cmd = self.commands[numt] cmd = cmd.format(*nums) self._run_command(cmd) 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)