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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
import sys
import os.path
import copy as copyModule
from ConfigParser import ConfigParser
import re
from tokenize import Intnumber, Floatnumber, Number
from types import *
import types
import new
import time
from StringIO import StringIO # not cStringIO because of unicode support
import imp # used by SettingsManager.updateSettingsFromPySrcFile()
numberRE = re.compile(Number)
complexNumberRE = re.compile('[\(]*' +Number + r'[ \t]*\+[ \t]*' + Number + '[\)]*')
convertableToStrTypes = (StringType, IntType, FloatType,
LongType, ComplexType, NoneType,
UnicodeType)
##################################################
## FUNCTIONS ##
def mergeNestedDictionaries(dict1, dict2, copy=False, deepcopy=False):
"""Recursively merge the values of dict2 into dict1.
This little function is very handy for selectively overriding settings in a
settings dictionary that has a nested structure.
"""
if copy:
dict1 = copyModule.copy(dict1)
elif deepcopy:
dict1 = copyModule.deepcopy(dict1)
for key,val in dict2.items():
if dict1.has_key(key) and type(val) == types.DictType and \
type(dict1[key]) == types.DictType:
dict1[key] = mergeNestedDictionaries(dict1[key], val)
else:
dict1[key] = val
return dict1
def stringIsNumber(S):
"""Return True if theString represents a Python number, False otherwise.
This also works for complex numbers and numbers with +/- in front."""
S = S.strip()
if S[0] in '-+' and len(S) > 1:
S = S[1:].strip()
match = complexNumberRE.match(S)
if not match:
match = numberRE.match(S)
if not match or (match.end() != len(S)):
return False
else:
return True
def convStringToNum(theString):
"""Convert a string representation of a Python number to the Python version"""
if not stringIsNumber(theString):
raise Error(theString + ' cannot be converted to a Python number')
return eval(theString, {}, {})
class Error(Exception):
pass
class NoDefault(object):
pass
class ConfigParserCaseSensitive(ConfigParser):
"""A case sensitive version of the standard Python ConfigParser."""
def optionxform(self, optionstr):
"""Don't change the case as is done in the default implemenation."""
return optionstr
class _SettingsCollector(object):
"""An abstract base class that provides the methods SettingsManager uses to
collect settings from config files and strings.
This class only collects settings it doesn't modify the _settings dictionary
of SettingsManager instances in any way.
"""
_ConfigParserClass = ConfigParserCaseSensitive
def readSettingsFromModule(self, mod, ignoreUnderscored=True):
"""Returns all settings from a Python module.
"""
S = {}
attrs = vars(mod)
for k, v in attrs.items():
if (ignoreUnderscored and k.startswith('_')):
continue
else:
S[k] = v
return S
def readSettingsFromPySrcStr(self, theString):
"""Return a dictionary of the settings in a Python src string."""
globalsDict = {'True':(1==1),
'False':(0==1),
}
newSettings = {'self':self}
exec (theString+os.linesep) in globalsDict, newSettings
del newSettings['self']
module = new.module('temp_settings_module')
module.__dict__.update(newSettings)
return self.readSettingsFromModule(module)
def readSettingsFromConfigFileObj(self, inFile, convert=True):
"""Return the settings from a config file that uses the syntax accepted by
Python's standard ConfigParser module (like Windows .ini files).
NOTE:
this method maintains case unlike the ConfigParser module, unless this
class was initialized with the 'caseSensitive' keyword set to False.
All setting values are initially parsed as strings. However, If the
'convert' arg is True this method will do the following value
conversions:
* all Python numeric literals will be coverted from string to number
* The string 'None' will be converted to the Python value None
* The string 'True' will be converted to a Python truth value
* The string 'False' will be converted to a Python false value
* Any string starting with 'python:' will be treated as a Python literal
or expression that needs to be eval'd. This approach is useful for
declaring lists and dictionaries.
If a config section titled 'Globals' is present the options defined
under it will be treated as top-level settings.
"""
p = self._ConfigParserClass()
p.readfp(inFile)
sects = p.sections()
newSettings = {}
sects = p.sections()
newSettings = {}
for s in sects:
newSettings[s] = {}
for o in p.options(s):
if o != '__name__':
newSettings[s][o] = p.get(s,o)
## loop through new settings -> deal with global settings, numbers,
## booleans and None ++ also deal with 'importSettings' commands
for sect, subDict in newSettings.items():
for key, val in subDict.items():
if convert:
if val.lower().startswith('python:'):
subDict[key] = eval(val[7:],{},{})
if val.lower() == 'none':
subDict[key] = None
if val.lower() == 'true':
subDict[key] = True
if val.lower() == 'false':
subDict[key] = False
if stringIsNumber(val):
subDict[key] = convStringToNum(val)
## now deal with any 'importSettings' commands
if key.lower() == 'importsettings':
if val.find(';') < 0:
importedSettings = self.readSettingsFromPySrcFile(val)
else:
path = val.split(';')[0]
rest = ''.join(val.split(';')[1:]).strip()
parentDict = self.readSettingsFromPySrcFile(path)
importedSettings = eval('parentDict["' + rest + '"]')
subDict.update(mergeNestedDictionaries(subDict,
importedSettings))
if sect.lower() == 'globals':
newSettings.update(newSettings[sect])
del newSettings[sect]
return newSettings
class SettingsManager(_SettingsCollector):
"""A mixin class that provides facilities for managing application settings.
SettingsManager is designed to work well with nested settings dictionaries
of any depth.
"""
def __init__(self):
super(SettingsManager, self).__init__()
self._settings = {}
self._initializeSettings()
def _defaultSettings(self):
return {}
def _initializeSettings(self):
"""A hook that allows for complex setting initialization sequences that
involve references to 'self' or other settings. For example:
self._settings['myCalcVal'] = self._settings['someVal'] * 15
This method should be called by the class' __init__() method when needed.
The dummy implementation should be reimplemented by subclasses.
"""
pass
## core post startup methods
def setting(self, name, default=NoDefault):
"""Get a setting from self._settings, with or without a default value."""
if default is NoDefault:
return self._settings[name]
else:
return self._settings.get(name, default)
def hasSetting(self, key):
"""True/False"""
return key in self._settings
def setSetting(self, name, value):
"""Set a setting in self._settings."""
self._settings[name] = value
def settings(self):
"""Return a reference to the settings dictionary"""
return self._settings
def copySettings(self):
"""Returns a shallow copy of the settings dictionary"""
return copyModule.copy(self._settings)
def deepcopySettings(self):
"""Returns a deep copy of the settings dictionary"""
return copyModule.deepcopy(self._settings)
def updateSettings(self, newSettings, merge=True):
"""Update the settings with a selective merge or a complete overwrite."""
if merge:
mergeNestedDictionaries(self._settings, newSettings)
else:
self._settings.update(newSettings)
## source specific update methods
def updateSettingsFromPySrcStr(self, theString, merge=True):
"""Update the settings from a code in a Python src string."""
newSettings = self.readSettingsFromPySrcStr(theString)
self.updateSettings(newSettings,
merge=newSettings.get('mergeSettings',merge) )
def updateSettingsFromConfigFileObj(self, inFile, convert=True, merge=True):
"""See the docstring for .updateSettingsFromConfigFile()
The caller of this method is responsible for closing the inFile file
object."""
newSettings = self.readSettingsFromConfigFileObj(inFile, convert=convert)
self.updateSettings(newSettings,
merge=newSettings.get('mergeSettings',merge))
def updateSettingsFromConfigStr(self, configStr, convert=True, merge=True):
"""See the docstring for .updateSettingsFromConfigFile()
"""
configStr = '[globals]\n' + configStr
inFile = StringIO(configStr)
newSettings = self.readSettingsFromConfigFileObj(inFile, convert=convert)
self.updateSettings(newSettings,
merge=newSettings.get('mergeSettings',merge))
|