Somewhat fancy voice command recognition software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shell.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2016 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. import subprocess
  6. from .pluginbase import PluginBase
  7. from ..numbers import NumberParser
  8. class Plugin(PluginBase):
  9. def __init__(self, config):
  10. super().__init__(config)
  11. self.options = vars(self.config.options)
  12. self.number_parser = NumberParser()
  13. self.commands = self.options['plugins']['.shell']
  14. for voice_cmd in self.commands.keys():
  15. self.corpus_strings.add(voice_cmd.strip().replace('%d', ''))
  16. def _run_command(self, cmd):
  17. """Print the command, then run it"""
  18. print(cmd)
  19. subprocess.call(cmd, shell=True)
  20. def recognizer_finished(self, recognizer, text):
  21. """Process a recognized voice command
  22. Recognized commands have the corresponding shell commands executed,
  23. possibly with number(s) substituted.
  24. """
  25. numt, nums = self.number_parser.parse_all_numbers(text)
  26. # Is there a matching command?
  27. if text in self.commands:
  28. # Run the valid_sentence_command if it's set
  29. # TODO: determine how to support this with plugins
  30. if self.options['valid_sentence_command']:
  31. subprocess.call(self.options['valid_sentence_command'],
  32. shell=True)
  33. cmd = self.commands[text]
  34. self._run_command(cmd)
  35. # TODO: Logging can be handled along with valid_sentence_command
  36. #self.log_history(text)
  37. return True
  38. elif numt in self.commands:
  39. # Run the valid_sentence_command if it's set
  40. if self.options['valid_sentence_command']:
  41. subprocess.call(self.options['valid_sentence_command'],
  42. shell=True)
  43. cmd = self.commands[numt]
  44. cmd = cmd.format(*nums)
  45. self._run_command(cmd)
  46. #self.log_history(text)
  47. return True
  48. else:
  49. # TODO: This could be implemented as a plugin, implicitly loaded
  50. # last
  51. # Run the invalid_sentence_command if it's set
  52. if self.options['invalid_sentence_command']:
  53. subprocess.call(self.options['invalid_sentence_command'],
  54. shell=True)
  55. print("no matching command {0}".format(text))
  56. return False
  57. # TODO: Make the D-Bus interface so this can leave the main process
  58. ## If there is a UI and we are not continuous listen
  59. #if self.ui:
  60. # if not self.continuous_listen:
  61. # # Stop listening
  62. # self.recognizer.pause()
  63. # # Let the UI know that there is a finish
  64. # self.ui.finished(text)