Somewhat fancy voice command recognition software
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

config.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.parser.parse_args(namespace=self.options)
  61. def _make_dir(self, directory):
  62. if not os.path.exists(directory):
  63. os.makedirs(directory)
  64. def _read_options_file(self):
  65. try:
  66. with open(self.opt_file, 'r') as f:
  67. self.options = json.load(f)
  68. self.options = Namespace(**self.options)
  69. except FileNotFoundError:
  70. # Make an empty options namespace
  71. self.options = Namespace()