diff options
author | Clayton G. Hobbs <clay@lakeserv.net> | 2015-12-27 14:40:53 -0500 |
---|---|---|
committer | Clayton G. Hobbs <clay@lakeserv.net> | 2015-12-27 14:40:53 -0500 |
commit | 25ebc8fe8b34939507d741636a11816cb4f11db2 (patch) | |
tree | 38b21450fd2eef723fd59ea16dafe02dd6220226 /config.py | |
parent | 0d67d50252849eca6e659f2daf20616542ec2a05 (diff) |
Moved paths to Config class
Paths of important files and directories are part of the program's configuration, no? We're making better use of XDG paths now. Only configuration-y things go in $XDG_CONFIG_HOME now, with cache-y and data-y things going in the appropriate places instead of just being crammed in with configuration.
Diffstat (limited to 'config.py')
-rw-r--r-- | config.py | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/config.py b/config.py index 21b4ec2..482e929 100644 --- a/config.py +++ b/config.py @@ -10,11 +10,34 @@ from argparse import ArgumentParser, Namespace from gi.repository import GLib class Config: - conf_dir = os.path.expanduser(os.path.join(GLib.get_user_config_dir(), - "blather")) + """Keep track of the configuration of Kaylee""" + # Name of the program, for later use + program_name = "kaylee" + + # Directories + conf_dir = os.path.join(GLib.get_user_config_dir(), program_name) + cache_dir = os.path.join(GLib.get_user_cache_dir(), program_name) + data_dir = os.path.join(GLib.get_user_data_dir(), program_name) + + # Configuration files + command_file = os.path.join(conf_dir, "commands.conf") opt_file = os.path.join(conf_dir, "options.json") + # Cache files + history_file = os.path.join(cache_dir, program_name + "history") + hash_file = os.path.join(cache_dir, "hash.json") + + # Data files + strings_file = os.path.join(data_dir, "sentences.corpus") + lang_file = os.path.join(data_dir, 'lm') + dic_file = os.path.join(data_dir, 'dic') + def __init__(self): + # Ensure necessary directories exist + self._make_dir(self.conf_dir) + self._make_dir(self.cache_dir) + self._make_dir(self.data_dir) + # Set up the argument parser self.parser = ArgumentParser() self.parser.add_argument("-i", "--interface", type=str, @@ -24,16 +47,16 @@ class Config: self.parser.add_argument("-c", "--continuous", action="store_true", dest="continuous", default=False, - help="starts interface with 'continuous' listen enabled") + help="Start interface with 'continuous' listen enabled") self.parser.add_argument("-p", "--pass-words", action="store_true", dest="pass_words", default=False, - help="passes the recognized words as arguments to the shell" + + help="Pass the recognized words as arguments to the shell" + " command") self.parser.add_argument("-H", "--history", type=int, action="store", dest="history", - help="number of commands to store in history file") + help="Number of commands to store in history file") self.parser.add_argument("-m", "--microphone", type=int, action="store", dest="microphone", default=None, @@ -41,16 +64,29 @@ class Config: self.parser.add_argument("--valid-sentence-command", type=str, dest="valid_sentence_command", action='store', - help="command to run when a valid sentence is detected") + help="Command to run when a valid sentence is detected") self.parser.add_argument("--invalid-sentence-command", type=str, dest="invalid_sentence_command", action='store', - help="command to run when an invalid sentence is detected") + help="Command to run when an invalid sentence is detected") # Read the configuration file - with open(self.opt_file, 'r') as f: - self.options = json.load(f) - self.options = Namespace(**self.options) + self._read_options_file() # Parse command-line arguments, overriding config file as appropriate self.args = self.parser.parse_args(namespace=self.options) + print(self.args) + print(self.options) + + def _make_dir(self, directory): + if not os.path.exists(directory): + os.makedirs(directory) + + def _read_options_file(self): + try: + with open(self.opt_file, 'r') as f: + self.options = json.load(f) + self.options = Namespace(**self.options) + except FileNotFoundError: + # Make an empty options namespace + self.options = Namespace() |