-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathConfigurable.py
231 lines (176 loc) · 7.34 KB
/
Configurable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""Configurable.py
Provides configuration file functionality.
"""
import os
import sys
from MiscUtils import AbstractError, NoDefault
from .Funcs import valueForString
class ConfigurationError(Exception):
"""Error in configuration file."""
class Configurable:
"""Abstract superclass for configuration file functionality.
Subclasses should override:
* defaultConfig() to return a dictionary of default settings
such as {'Frequency': 5}
* configFilename() to return the filename by which users can
override the configuration such as 'Pinger.config'
Subclasses typically use the setting() method, for example:
time.sleep(self.setting('Frequency'))
They might also use the printConfig() method, for example:
self.printConfig() # or
self.printConfig(file)
Users of your software can create a file with the same name as
configFilename() and selectively override settings. The format of
the file is a Python dictionary.
Subclasses can also override userConfig() in order to obtain the
user configuration settings from another source.
"""
# region Init
def __init__(self):
self._config = None
# endregion Init
# region Configuration
def config(self):
"""Return the configuration of the object as a dictionary.
This is a combination of defaultConfig() and userConfig().
This method caches the config.
"""
if self._config is None:
self._config = {
**self.defaultConfig(),
**self.userConfig(),
**self.commandLineConfig()}
return self._config
def setting(self, name, default=NoDefault):
"""Return the value of a particular setting in the configuration."""
if default is NoDefault:
try:
return self.config()[name]
except KeyError as e:
keys = ', '.join(sorted(self.config()))
raise KeyError(
f'{name} not found - config keys are: {keys}') from e
else:
return self.config().get(name, default)
def setSetting(self, name, value):
"""Set a particular configuration setting."""
self.config()[name] = value
def hasSetting(self, name):
"""Check whether a configuration setting has been changed."""
return name in self.config()
def defaultConfig(self):
"""Return a dictionary with all the default values for the settings.
This implementation returns {}. Subclasses should override.
"""
return {}
def configFilename(self):
"""Return the full name of the user config file.
Users can override the configuration by this config file.
Subclasses must override to specify a name.
Returning None is valid, in which case no user config file
will be loaded.
"""
raise AbstractError(self.__class__)
def configName(self):
"""Return the name of the configuration file without the extension.
This is the portion of the config file name before the '.config'.
This is used on the command-line.
"""
return os.path.splitext(os.path.basename(self.configFilename()))[0]
def configReplacementValues(self):
"""Return a dictionary for substitutions in the config file.
This must be a dictionary suitable for use with "string % dict"
that should be used on the text in the config file.
If an empty dictionary (or None) is returned, then no substitution
will be attempted.
"""
return {}
def userConfig(self):
"""Return the user config overrides.
These settings can be found in the optional config file.
Returns {} if there is no such file.
The config filename is taken from configFilename().
"""
# pylint: disable=assignment-from-no-return
filename = self.configFilename()
if not filename:
return {}
try:
contents = self.readConfig(filename)
except IOError as e:
print('WARNING: Config file', filename, 'not loaded:', e.strerror)
print()
return {}
if contents.lstrip().startswith('{'):
raise ConfigurationError(
'Configuration via a dict literal is not supported anymore.')
try:
from ImportManager import ImportManager
ImportManager().watchFile(filename)
except Exception as e:
print('WARNING: Config file', filename, 'cannot be watched:', e)
print()
config = self.configReplacementValues().copy()
try:
exec(contents, config)
keys = [key for key in config if key.startswith('_')]
for key in keys:
del config[key]
except Exception as e:
raise ConfigurationError(
f'Invalid configuration file, {filename} ({e}).') from e
return config
@staticmethod
def readConfig(filename):
"""Read the configuration from the file with the given name.
Raises an UIError if the configuration cannot be read.
This implementation assumes the file is stored in utf-8 encoding with
possible BOM at the start, but also tries to read as latin-1 if it
cannot be decoded as utf-8. Subclasses can override this behavior.
"""
try:
with open(filename, encoding='utf-8-sig') as f:
return f.read()
except UnicodeDecodeError:
with open(filename, encoding='latin-1') as f:
return f.read()
def printConfig(self, dest=None):
"""Print the configuration to the given destination.
The default destination is stdout. A fixed with font is assumed
for aligning the values to start at the same column.
"""
if dest is None:
dest = sys.stdout
config = self.config()
width = max(map(len, config))
for key in sorted(config):
dest.write(f'{key.ljust(width)} = {self.setting(key)!r}\n')
dest.write('\n')
def commandLineConfig(self):
"""Return the settings that came from the command-line.
These settings come via addCommandLineSetting().
"""
return _settings.get(self.configName(), {})
# endregion Configuration
# region Command line settings
_settings = {}
def addCommandLineSetting(name, value):
"""Override the configuration with a command-line setting.
Take a setting, like "Application.Verbose=0", and call
addCommandLineSetting('Application.Verbose', '0'), and
it will override any settings in Application.config
"""
configName, settingName = name.split('.', 1)
value = valueForString(value)
if configName not in _settings:
_settings[configName] = {}
_settings[configName][settingName] = value
def commandLineSetting(configName, settingName, default=NoDefault):
"""Retrieve a command-line setting.
You can use this with non-existent classes, like "Context.Root=/Webware",
and then fetch it back with commandLineSetting('Context', 'Root').
"""
if default is NoDefault:
return _settings[configName][settingName]
return _settings.get(configName, {}).get(settingName, default)
# endregion Command line settings