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.

config.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2013 Jezra
  4. # Copyright 2015 Clayton G. Hobbs
  5. import json
  6. import os
  7. from argparse import ArgumentParser, Namespace
  8. from gi.repository import GLib
  9. class Config:
  10. """Keep track of the configuration of Kaylee"""
  11. # Name of the program, for later use
  12. program_name = "kaylee"
  13. # Directories
  14. conf_dir = os.path.join(GLib.get_user_config_dir(), program_name)
  15. cache_dir = os.path.join(GLib.get_user_cache_dir(), program_name)
  16. data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
  17. # Configuration files
  18. command_file = os.path.join(conf_dir, "commands.conf")
  19. opt_file = os.path.join(conf_dir, "options.json")
  20. # Cache files
  21. history_file = os.path.join(cache_dir, program_name + "history")
  22. hash_file = os.path.join(cache_dir, "hash.json")
  23. # Data files
  24. strings_file = os.path.join(data_dir, "sentences.corpus")
  25. lang_file = os.path.join(data_dir, 'lm')
  26. dic_file = os.path.join(data_dir, 'dic')
  27. def __init__(self):
  28. # Ensure necessary directories exist
  29. self._make_dir(self.conf_dir)
  30. self._make_dir(self.cache_dir)
  31. self._make_dir(self.data_dir)
  32. # Set up the argument parser
  33. self.parser = ArgumentParser()
  34. self.parser.add_argument("-i", "--interface", type=str,
  35. dest="interface", action='store',
  36. help="Interface to use (if any). 'g' for GTK or 'gt' for GTK" +
  37. " system tray icon")
  38. self.parser.add_argument("-c", "--continuous",
  39. action="store_true", dest="continuous", default=False,
  40. help="Start interface with 'continuous' listen enabled")
  41. self.parser.add_argument("-p", "--pass-words",
  42. action="store_true", dest="pass_words", default=False,
  43. help="Pass the recognized words as arguments to the shell" +
  44. " command")
  45. self.parser.add_argument("-H", "--history", type=int,
  46. action="store", dest="history",
  47. help="Number of commands to store in history file")
  48. self.parser.add_argument("-m", "--microphone", type=int,
  49. action="store", dest="microphone", default=None,
  50. help="Audio input card to use (if other than system default)")
  51. self.parser.add_argument("--valid-sentence-command", type=str,
  52. dest="valid_sentence_command", action='store',
  53. help="Command to run when a valid sentence is detected")
  54. self.parser.add_argument("--invalid-sentence-command", type=str,
  55. dest="invalid_sentence_command", action='store',
  56. help="Command to run when an invalid sentence is detected")
  57. # Read the configuration file
  58. self._read_options_file()
  59. # Parse command-line arguments, overriding config file as appropriate
  60. self.args = self.parser.parse_args(namespace=self.options)
  61. print(self.args)
  62. print(self.options)
  63. def _make_dir(self, directory):
  64. if not os.path.exists(directory):
  65. os.makedirs(directory)
  66. def _read_options_file(self):
  67. try:
  68. with open(self.opt_file, 'r') as f:
  69. self.options = json.load(f)
  70. self.options = Namespace(**self.options)
  71. except FileNotFoundError:
  72. # Make an empty options namespace
  73. self.options = Namespace()