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 /blather.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 'blather.py')
-rwxr-xr-x | blather.py | 55 |
1 files changed, 6 insertions, 49 deletions
diff --git a/blather.py b/blather.py index 00d0c49..d7fa14b 100755 --- a/blather.py +++ b/blather.py @@ -11,11 +11,11 @@ import signal import hashlib import os.path import subprocess -from argparse import ArgumentParser from gi.repository import GObject, GLib import json from recognizer import Recognizer +from config import Config # Where are the files? conf_dir = os.path.expanduser(os.path.join(GLib.get_user_config_dir(), @@ -24,7 +24,6 @@ lang_dir = os.path.join(conf_dir, "language") command_file = os.path.join(conf_dir, "commands.conf") strings_file = os.path.join(conf_dir, "sentences.corpus") history_file = os.path.join(conf_dir, "blather.history") -opt_file = os.path.join(conf_dir, "options.json") hash_file = os.path.join(conf_dir, "hash.json") lang_file = os.path.join(lang_dir, 'lm') dic_file = os.path.join(lang_dir, 'dic') @@ -34,7 +33,7 @@ if not os.path.exists(lang_dir): class Blather: - def __init__(self, opts): + def __init__(self): self.ui = None self.options = {} ui_continuous_listen = False @@ -46,13 +45,8 @@ class Blather: self.read_commands() # Load the options file - self.load_options() - - print(opts) - # Merge the options with the ones provided by command-line arguments - for k, v in vars(opts).items(): - if v is not None: - self.options[k] = v + self.config = Config() + self.options = vars(self.config.options) if self.options['interface'] != None: if self.options['interface'] == "g": @@ -63,7 +57,7 @@ class Blather: print("no GUI defined") sys.exit() - self.ui = UI(args, self.options['continuous']) + self.ui = UI(self.options, self.options['continuous']) self.ui.connect("command", self.process_command) # Can we load the icon resource? icon = self.load_resource("icon.png") @@ -110,13 +104,6 @@ class Blather: # Close the strings file strings.close() - def load_options(self): - """Load options from the options.json file""" - # Is there an opt file? - with open(opt_file, 'r') as f: - self.options = json.load(f) - print(self.options) - def log_history(self, text): if self.options['history']: self.history.append(text) @@ -228,38 +215,8 @@ class Blather: if __name__ == "__main__": - parser = ArgumentParser() - 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") - - parser.add_argument("-c", "--continuous", - action="store_true", dest="continuous", default=False, - help="starts interface with 'continuous' listen enabled") - - 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") - - parser.add_argument("-H", "--history", type=int, - action="store", dest="history", - help="number of commands to store in history file") - - parser.add_argument("-m", "--microphone", type=int, - action="store", dest="microphone", default=None, - help="Audio input card to use (if other than system default)") - - parser.add_argument("--valid-sentence-command", type=str, dest="valid_sentence_command", - action='store', - help="command to run when a valid sentence is detected") - - parser.add_argument("--invalid-sentence-command", type=str, dest="invalid_sentence_command", - action='store', - help="command to run when an invalid sentence is detected") - - args = parser.parse_args() # Make our blather object - blather = Blather(args) + blather = Blather() # Init gobject threads GObject.threads_init() # We want a main loop |