diff options
author | Clayton G. Hobbs <clay@lakeserv.net> | 2015-12-27 12:56:57 -0500 |
---|---|---|
committer | Clayton G. Hobbs <clay@lakeserv.net> | 2015-12-27 12:56:57 -0500 |
commit | 87c0a1bfe93ffbe7eb67c41ea2b46fd5a7c1ab28 (patch) | |
tree | fcdcd1506922f520bd305a09c0329ba909d5534a /config.py | |
parent | 443883b6898f2a75e64d8d4797dd448ef3aeda70 (diff) |
Improved config/argument behaviour
The configuration file is now properly overridden by argument parsing. This was accomplished by loading the config file, then treating the specified options as a namespace for the ArgumentParser. This makes things from the config file get overridden iff they were specified on the command line (not simply from defaults set in the ArgumentParser).
Diffstat (limited to 'config.py')
-rw-r--r-- | config.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/config.py b/config.py new file mode 100644 index 0000000..21b4ec2 --- /dev/null +++ b/config.py @@ -0,0 +1,56 @@ +# This is part of Kaylee +# -- this code is licensed GPLv3 +# Copyright 2013 Jezra +# Copyright 2015 Clayton G. Hobbs + +import json +import os +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")) + opt_file = os.path.join(conf_dir, "options.json") + + def __init__(self): + # Set up the argument parser + self.parser = ArgumentParser() + self.parser.add_argument("-i", "--interface", type=str, + dest="interface", action='store', + help="Interface to use (if any). 'g' for GTK or 'gt' for GTK" + + " system tray icon") + + self.parser.add_argument("-c", "--continuous", + action="store_true", dest="continuous", default=False, + help="starts 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" + + " command") + + self.parser.add_argument("-H", "--history", type=int, + action="store", dest="history", + help="number of commands to store in history file") + + self.parser.add_argument("-m", "--microphone", type=int, + action="store", dest="microphone", default=None, + help="Audio input card to use (if other than system default)") + + 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") + + 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") + + # Read the configuration file + with open(self.opt_file, 'r') as f: + self.options = json.load(f) + self.options = Namespace(**self.options) + + # Parse command-line arguments, overriding config file as appropriate + self.args = self.parser.parse_args(namespace=self.options) |