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 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # Should we be passing words?
  35. # TODO: configuration changes to make this not ridiculous
  36. if self.options['pass_words']:
  37. cmd += " " + text
  38. self._run_command(cmd)
  39. # TODO: Logging can be handled along with valid_sentence_command
  40. #self.log_history(text)
  41. return True
  42. elif numt in self.commands:
  43. # Run the valid_sentence_command if it's set
  44. if self.options['valid_sentence_command']:
  45. subprocess.call(self.options['valid_sentence_command'],
  46. shell=True)
  47. cmd = self.commands[numt]
  48. cmd = cmd.format(*nums)
  49. # Should we be passing words?
  50. if self.options['pass_words']:
  51. cmd += " " + text
  52. self._run_command(cmd)
  53. #self.log_history(text)
  54. return True
  55. else:
  56. # TODO: This could be implemented as a plugin, implicitly loaded
  57. # last
  58. # Run the invalid_sentence_command if it's set
  59. if self.options['invalid_sentence_command']:
  60. subprocess.call(self.options['invalid_sentence_command'],
  61. shell=True)
  62. print("no matching command {0}".format(text))
  63. return False
  64. # TODO: Make the D-Bus interface so this can leave the main process
  65. ## If there is a UI and we are not continuous listen
  66. #if self.ui:
  67. # if not self.continuous_listen:
  68. # # Stop listening
  69. # self.recognizer.pause()
  70. # # Let the UI know that there is a finish
  71. # self.ui.finished(text)