|
@@ -10,11 +10,34 @@ from argparse import ArgumentParser, Namespace
|
10
|
10
|
from gi.repository import GLib
|
11
|
11
|
|
12
|
12
|
class Config:
|
13
|
|
- conf_dir = os.path.expanduser(os.path.join(GLib.get_user_config_dir(),
|
14
|
|
- "blather"))
|
|
13
|
+ """Keep track of the configuration of Kaylee"""
|
|
14
|
+ # Name of the program, for later use
|
|
15
|
+ program_name = "kaylee"
|
|
16
|
+
|
|
17
|
+ # Directories
|
|
18
|
+ conf_dir = os.path.join(GLib.get_user_config_dir(), program_name)
|
|
19
|
+ cache_dir = os.path.join(GLib.get_user_cache_dir(), program_name)
|
|
20
|
+ data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
|
|
21
|
+
|
|
22
|
+ # Configuration files
|
|
23
|
+ command_file = os.path.join(conf_dir, "commands.conf")
|
15
|
24
|
opt_file = os.path.join(conf_dir, "options.json")
|
16
|
25
|
|
|
26
|
+ # Cache files
|
|
27
|
+ history_file = os.path.join(cache_dir, program_name + "history")
|
|
28
|
+ hash_file = os.path.join(cache_dir, "hash.json")
|
|
29
|
+
|
|
30
|
+ # Data files
|
|
31
|
+ strings_file = os.path.join(data_dir, "sentences.corpus")
|
|
32
|
+ lang_file = os.path.join(data_dir, 'lm')
|
|
33
|
+ dic_file = os.path.join(data_dir, 'dic')
|
|
34
|
+
|
17
|
35
|
def __init__(self):
|
|
36
|
+ # Ensure necessary directories exist
|
|
37
|
+ self._make_dir(self.conf_dir)
|
|
38
|
+ self._make_dir(self.cache_dir)
|
|
39
|
+ self._make_dir(self.data_dir)
|
|
40
|
+
|
18
|
41
|
# Set up the argument parser
|
19
|
42
|
self.parser = ArgumentParser()
|
20
|
43
|
self.parser.add_argument("-i", "--interface", type=str,
|
|
@@ -24,16 +47,16 @@ class Config:
|
24
|
47
|
|
25
|
48
|
self.parser.add_argument("-c", "--continuous",
|
26
|
49
|
action="store_true", dest="continuous", default=False,
|
27
|
|
- help="starts interface with 'continuous' listen enabled")
|
|
50
|
+ help="Start interface with 'continuous' listen enabled")
|
28
|
51
|
|
29
|
52
|
self.parser.add_argument("-p", "--pass-words",
|
30
|
53
|
action="store_true", dest="pass_words", default=False,
|
31
|
|
- help="passes the recognized words as arguments to the shell" +
|
|
54
|
+ help="Pass the recognized words as arguments to the shell" +
|
32
|
55
|
" command")
|
33
|
56
|
|
34
|
57
|
self.parser.add_argument("-H", "--history", type=int,
|
35
|
58
|
action="store", dest="history",
|
36
|
|
- help="number of commands to store in history file")
|
|
59
|
+ help="Number of commands to store in history file")
|
37
|
60
|
|
38
|
61
|
self.parser.add_argument("-m", "--microphone", type=int,
|
39
|
62
|
action="store", dest="microphone", default=None,
|
|
@@ -41,16 +64,29 @@ class Config:
|
41
|
64
|
|
42
|
65
|
self.parser.add_argument("--valid-sentence-command", type=str,
|
43
|
66
|
dest="valid_sentence_command", action='store',
|
44
|
|
- help="command to run when a valid sentence is detected")
|
|
67
|
+ help="Command to run when a valid sentence is detected")
|
45
|
68
|
|
46
|
69
|
self.parser.add_argument("--invalid-sentence-command", type=str,
|
47
|
70
|
dest="invalid_sentence_command", action='store',
|
48
|
|
- help="command to run when an invalid sentence is detected")
|
|
71
|
+ help="Command to run when an invalid sentence is detected")
|
49
|
72
|
|
50
|
73
|
# Read the configuration file
|
51
|
|
- with open(self.opt_file, 'r') as f:
|
52
|
|
- self.options = json.load(f)
|
53
|
|
- self.options = Namespace(**self.options)
|
|
74
|
+ self._read_options_file()
|
54
|
75
|
|
55
|
76
|
# Parse command-line arguments, overriding config file as appropriate
|
56
|
77
|
self.args = self.parser.parse_args(namespace=self.options)
|
|
78
|
+ print(self.args)
|
|
79
|
+ print(self.options)
|
|
80
|
+
|
|
81
|
+ def _make_dir(self, directory):
|
|
82
|
+ if not os.path.exists(directory):
|
|
83
|
+ os.makedirs(directory)
|
|
84
|
+
|
|
85
|
+ def _read_options_file(self):
|
|
86
|
+ try:
|
|
87
|
+ with open(self.opt_file, 'r') as f:
|
|
88
|
+ self.options = json.load(f)
|
|
89
|
+ self.options = Namespace(**self.options)
|
|
90
|
+ except FileNotFoundError:
|
|
91
|
+ # Make an empty options namespace
|
|
92
|
+ self.options = Namespace()
|