Browse Source

Added a default configuration file

Previously, the default configuration file was only provided in a form
that was not included in distributions.  This could potentially
frustrate users by making it difficult to find a configuration file
example.  To remedy this, I moved it to be a package resource, imported
pkg_resources (setuptools is now a dependancy!), and made Kaylee load
both the default and the local configuration files, with the local of
course overriding the default.
Clara Hobbs 7 years ago
parent
commit
6e44e97135
4 changed files with 25 additions and 26 deletions
  1. 11
    8
      README.rst
  2. 0
    0
      kayleevc/conf/options.json
  3. 13
    17
      kayleevc/util.py
  4. 1
    1
      setup.py

+ 11
- 8
README.rst View File

15
 3. gstreamer-1.0 (and what ever plugin has pocketsphinx support)
15
 3. gstreamer-1.0 (and what ever plugin has pocketsphinx support)
16
 4. gstreamer-1.0 base plugins (required for ALSA)
16
 4. gstreamer-1.0 base plugins (required for ALSA)
17
 5. python-gobject (required for GStreamer and the GTK-based UI)
17
 5. python-gobject (required for GStreamer and the GTK-based UI)
18
-6. python-requests (required for automatic language updating)
18
+6. python-requests
19
+7. python-setuptools
20
+
21
+**Note:** it may also be required to install
22
+``pocketsphinx-hmm-en-hub4wsj``
19
 
23
 
20
 Optional
24
 Optional
21
 ~~~~~~~~
25
 ~~~~~~~~
22
 
26
 
23
 1. python-pydbus (required for MPRIS plugin)
27
 1. python-pydbus (required for MPRIS plugin)
24
 
28
 
25
-**Note:** it may also be required to install
26
-``pocketsphinx-hmm-en-hub4wsj``
27
-
28
 Usage
29
 Usage
29
 -----
30
 -----
30
 
31
 
31
-1. Copy options.json.tmp to ~/.config/kaylee/options.json.  Default
32
-   values for command-line arguments may be specified in this file.
33
-2. Copy plugins.json.tmp to ~/.config/kaylee/plugins.json and fill the
32
+1. Copy plugins.json.tmp to ~/.config/kaylee/plugins.json and fill the
34
    ".shell" section of the file with sentences to speak and commands
33
    ".shell" section of the file with sentences to speak and commands
35
    to run.
34
    to run.
36
-3. Run Kaylee with ``./kaylee.py``. This generates a language model and
35
+2. Run Kaylee with ``./kaylee.py``. This generates a language model and
37
    dictionary using the `Sphinx Knowledge Base Tool
36
    dictionary using the `Sphinx Knowledge Base Tool
38
    <http://www.speech.cs.cmu.edu/tools/lmtool.html>`__, then listens for
37
    <http://www.speech.cs.cmu.edu/tools/lmtool.html>`__, then listens for
39
    commands with the system default microphone.
38
    commands with the system default microphone.
60
 -  To run a command when an invalid sentence has been detected:
59
 -  To run a command when an invalid sentence has been detected:
61
    ``./kaylee.py --invalid-sentence-command=/path/to/command``
60
    ``./kaylee.py --invalid-sentence-command=/path/to/command``
62
 
61
 
62
+Default values for command-line arguments may be specified in
63
+~/.config/kaylee/options.json.  It is recommended to base this file on
64
+the default configuration file, kayleevc/conf/options.json.
65
+
63
 Finding the Device Number of a USB microphone
66
 Finding the Device Number of a USB microphone
64
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65
 
68
 

options.json.tmp → kayleevc/conf/options.json View File


+ 13
- 17
kayleevc/util.py View File

10
 from argparse import ArgumentParser, Namespace
10
 from argparse import ArgumentParser, Namespace
11
 from collections import OrderedDict
11
 from collections import OrderedDict
12
 
12
 
13
+import pkg_resources
13
 import requests
14
 import requests
14
 
15
 
15
 from gi.repository import GLib
16
 from gi.repository import GLib
26
     data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
27
     data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
27
 
28
 
28
     # Configuration files
29
     # Configuration files
29
-    opt_file = os.path.join(conf_dir, "options.json")
30
+    default_opt_file = pkg_resources.resource_filename('kayleevc',
31
+        'conf/options.json')
32
+    local_opt_file = os.path.join(conf_dir, "options.json")
30
     plugins_file = os.path.join(conf_dir, "plugins.json")
33
     plugins_file = os.path.join(conf_dir, "plugins.json")
31
 
34
 
32
     # Cache files
35
     # Cache files
71
                 dest="invalid_sentence_command", action='store',
74
                 dest="invalid_sentence_command", action='store',
72
                 help="Command to run when an invalid sentence is detected")
75
                 help="Command to run when an invalid sentence is detected")
73
 
76
 
74
-        # Read the configuration file
75
-        self._read_options_file()
77
+        # Read the configuration files
78
+        self.options = self._read_json_file(self.default_opt_file)
79
+        self.options.update(self._read_json_file(self.local_opt_file))
80
+        self.options = Namespace(**self.options)
76
 
81
 
77
         # Parse command-line arguments, overriding config file as appropriate
82
         # Parse command-line arguments, overriding config file as appropriate
78
         self._parser.parse_args(namespace=self.options)
83
         self._parser.parse_args(namespace=self.options)
79
 
84
 
80
         # Read the plugins file
85
         # Read the plugins file
81
-        self._read_plugins_file()
86
+        self.plugins = self._read_json_file(self.plugins_file)
82
 
87
 
83
     def _make_dir(self, directory):
88
     def _make_dir(self, directory):
84
         if not os.path.exists(directory):
89
         if not os.path.exists(directory):
85
             os.makedirs(directory)
90
             os.makedirs(directory)
86
 
91
 
87
-    def _read_options_file(self):
92
+    def _read_json_file(self, filename):
88
         try:
93
         try:
89
-            with open(self.opt_file, 'r') as f:
90
-                self.options = json.load(f, object_pairs_hook=OrderedDict)
91
-                self.options = Namespace(**self.options)
94
+            with open(filename, 'r') as f:
95
+                return json.load(f, object_pairs_hook=OrderedDict)
92
         except FileNotFoundError:
96
         except FileNotFoundError:
93
-            # Make an empty options namespace
94
-            self.options = Namespace()
95
-
96
-    def _read_plugins_file(self):
97
-        try:
98
-            with open(self.plugins_file, 'r') as f:
99
-                self.plugins = json.load(f, object_pairs_hook=OrderedDict)
100
-        except FileNotFoundError:
101
-            self.plugins = None
97
+            return {}
102
 
98
 
103
 
99
 
104
 class Hasher:
100
 class Hasher:

+ 1
- 1
setup.py View File

25
         "Programming Language :: Python :: 3.5",
25
         "Programming Language :: Python :: 3.5",
26
         "Topic :: Home Automation"
26
         "Topic :: Home Automation"
27
     ],
27
     ],
28
-    install_requires=["requests"],
28
+    install_requires=["requests", "setuptools"],
29
     data_files = [
29
     data_files = [
30
         ("/usr/share/kaylee", ["data/icon_inactive.png", "data/icon.png",
30
         ("/usr/share/kaylee", ["data/icon_inactive.png", "data/icon.png",
31
             "options.json.tmp"]),
31
             "options.json.tmp"]),

Loading…
Cancel
Save