# 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 # Is there a matching command with numbers substituted? elif numt in self.commands: self.emit('processed', text) cmd = self.commands[numt] cmd = cmd.format(*nums) self._run_command(cmd) return True else: return False