|
@@ -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:
|