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,25 +15,24 @@ Requirements
15 15
 3. gstreamer-1.0 (and what ever plugin has pocketsphinx support)
16 16
 4. gstreamer-1.0 base plugins (required for ALSA)
17 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 24
 Optional
21 25
 ~~~~~~~~
22 26
 
23 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 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 33
    ".shell" section of the file with sentences to speak and commands
35 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 36
    dictionary using the `Sphinx Knowledge Base Tool
38 37
    <http://www.speech.cs.cmu.edu/tools/lmtool.html>`__, then listens for
39 38
    commands with the system default microphone.
@@ -60,6 +59,10 @@ Examples
60 59
 -  To run a command when an invalid sentence has been detected:
61 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 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,6 +10,7 @@ import os
10 10
 from argparse import ArgumentParser, Namespace
11 11
 from collections import OrderedDict
12 12
 
13
+import pkg_resources
13 14
 import requests
14 15
 
15 16
 from gi.repository import GLib
@@ -26,7 +27,9 @@ class Config:
26 27
     data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
27 28
 
28 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 33
     plugins_file = os.path.join(conf_dir, "plugins.json")
31 34
 
32 35
     # Cache files
@@ -71,34 +74,27 @@ class Config:
71 74
                 dest="invalid_sentence_command", action='store',
72 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 82
         # Parse command-line arguments, overriding config file as appropriate
78 83
         self._parser.parse_args(namespace=self.options)
79 84
 
80 85
         # Read the plugins file
81
-        self._read_plugins_file()
86
+        self.plugins = self._read_json_file(self.plugins_file)
82 87
 
83 88
     def _make_dir(self, directory):
84 89
         if not os.path.exists(directory):
85 90
             os.makedirs(directory)
86 91
 
87
-    def _read_options_file(self):
92
+    def _read_json_file(self, filename):
88 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 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 100
 class Hasher:

+ 1
- 1
setup.py View File

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

Loading…
Cancel
Save