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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
#!/usr/bin/env python
"""Provides a mixin/base class for collecting and managing application settings
Meta-Data
==========
Author: Tavis Rudd <tavis@damnsimple.com>
Version: $Revision: 1.28 $
Start Date: 2001/05/30
Last Revision Date: $Date: 2006/01/29 07:19:12 $
"""
# $Id: SettingsManager.py,v 1.28 2006/01/29 07:19:12 tavis_rudd Exp $
__author__ = "Tavis Rudd <tavis@damnsimple.com>"
__revision__ = "$Revision: 1.28 $"[11:-2]
##################################################
## DEPENDENCIES ##
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 tempfile
import imp
import time
from StringIO import StringIO # not cStringIO because of unicode support
import imp # used by SettingsManager.updateSettingsFromPySrcFile()
try:
import threading
from threading import Lock # used for thread lock on sys.path manipulations
except:
## provide a dummy for non-threading Python systems
class Lock:
def acquire(self):
pass
def release(self):
pass
class BaseErrorClass: pass
##################################################
## CONSTANTS & GLOBALS ##
try:
True,False
except NameError:
True, False = (1==1),(1==0)
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, {}, {})
######
ident = r'[_a-zA-Z][_a-zA-Z0-9]*'
firstChunk = r'^(?P<indent>\s*)(?P<class>[_a-zA-Z][_a-zA-Z0-9]*)'
customClassRe = re.compile(firstChunk + r'\s*:')
baseClasses = r'(?P<bases>\(\s*([_a-zA-Z][_a-zA-Z0-9]*\s*(,\s*[_a-zA-Z][_a-zA-Z0-9]*\s*)*)\))'
customClassWithBasesRe = re.compile(firstChunk + baseClasses + '\s*:')
def translateClassBasedConfigSyntax(src):
"""Compiles a config file in the custom class-based SettingsContainer syntax
to Vanilla Python
# WebKit.config
Applications:
MyApp:
Dirs:
ROOT = '/home/www/Home'
Products = '/home/www/Products'
becomes:
# WebKit.config
from Cheetah.SettingsManager import SettingsContainer
class Applications(SettingsContainer):
class MyApp(SettingsContainer):
class Dirs(SettingsContainer):
ROOT = '/home/www/Home'
Products = '/home/www/Products'
"""
outputLines = []
for line in src.splitlines():
if customClassRe.match(line) and \
line.strip().split(':')[0] not in ('else','try', 'except', 'finally'):
line = customClassRe.sub(
r'\g<indent>class \g<class>(SettingsContainer):', line)
elif customClassWithBasesRe.match(line) and not line.strip().startswith('except'):
line = customClassWithBasesRe.sub(
r'\g<indent>class \g<class>\g<bases>:', line)
outputLines.append(line)
## prepend this to the first line to make sure that tracebacks report the right line nums
if outputLines[0].find('class ') == -1:
initLine = 'from Cheetah.SettingsManager import SettingsContainer; True, False = 1, 0; '
else:
initLine = 'from Cheetah.SettingsManager import SettingsContainer; True, False = 1, 0\n'
return initLine + '\n'.join(outputLines) + '\n'
##################################################
## CLASSES ##
class Error(BaseErrorClass):
pass
class NoDefault:
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 SettingsContainer:
"""An abstract base class for 'classes' that are used to house settings."""
pass
class _SettingsCollector:
"""An abstract base class that provides the methods SettingsManager uses to
collect settings from config files and SettingsContainers.
This class only collects settings it doesn't modify the _settings dictionary
of SettingsManager instances in any way.
SettingsCollector is designed to:
- be able to read settings from Python src files (or strings) so that
complex Python objects can be stored in the application's settings
dictionary. For example, you might want to store references to various
classes that are used by the application and plugins to the application
might want to substitute one class for another.
- be able to read/write .ini style config files (or strings)
- allow sections in .ini config files to be extended by settings in Python
src files
- allow python literals to be used values in .ini config files
- maintain the case of setting names, unlike the ConfigParser module
"""
_sysPathLock = Lock() # used by the updateSettingsFromPySrcFile() method
_ConfigParserClass = ConfigParserCaseSensitive
def __init__(self):
pass
def normalizePath(self, path):
"""A hook for any neccessary path manipulations.
For example, when this is used with WebKit servlets all relative paths
must be converted so they are relative to the servlet's directory rather
than relative to the program's current working dir.
The default implementation just normalizes the path for the current
operating system."""
return os.path.normpath(path.replace("\\",'/'))
def readSettingsFromContainer(self, container, ignoreUnderscored=True):
"""Returns all settings from a SettingsContainer or Python
module.
This method is recursive.
"""
S = {}
if type(container) == ModuleType:
attrs = vars(container)
else:
attrs = self._getAllAttrsFromContainer(container)
for k, v in attrs.items():
if (ignoreUnderscored and k.startswith('_')) or v is SettingsContainer:
continue
if self._isContainer(v):
S[k] = self.readSettingsFromContainer(v)
else:
S[k] = v
return S
# provide an alias
readSettingsFromModule = readSettingsFromContainer
def _isContainer(self, thing):
"""Check if 'thing' is a Python module or a subclass of
SettingsContainer."""
return type(thing) == ModuleType or (
type(thing) == ClassType and issubclass(thing, SettingsContainer)
)
def _getAllAttrsFromContainer(self, container):
"""Extract all the attributes of a SettingsContainer subclass.
The 'container' is a class, so extracting all attributes from it, an
instance of it, and all its base classes.
This method is not recursive.
"""
attrs = container.__dict__.copy()
# init an instance of the container and get all attributes
attrs.update( container().__dict__ )
for base in container.__bases__:
for k, v in base.__dict__.items():
if not attrs.has_key(k):
attrs[k] = v
return attrs
def readSettingsFromPySrcFile(self, path):
"""Return new settings dict from variables in a Python source file.
This method will temporarily add the directory of src file to sys.path so
that import statements relative to that dir will work properly."""
path = self.normalizePath(path)
dirName = os.path.dirname(path)
tmpPath = tempfile.mkstemp('webware_temp')
pySrc = translateClassBasedConfigSyntax(open(path).read())
modName = path.replace('.','_').replace('/','_').replace('\\','_')
open(tmpPath, 'w').write(pySrc)
try:
fp = open(tmpPath)
self._sysPathLock.acquire()
sys.path.insert(0, dirName)
module = imp.load_source(modName, path, fp)
newSettings = self.readSettingsFromModule(module)
del sys.path[0]
self._sysPathLock.release()
return newSettings
finally:
fp.close()
try:
os.remove(tmpPath)
except:
pass
if os.path.exists(tmpPath + 'c'):
try:
os.remove(tmpPath + 'c')
except:
pass
if os.path.exists(path + 'c'):
try:
os.remove(path + 'c')
except:
pass
def readSettingsFromPySrcStr(self, theString):
"""Return a dictionary of the settings in a Python src string."""
globalsDict = {'True':1,
'False':0,
'SettingsContainer':SettingsContainer,
}
newSettings = {'self':self}
exec theString in globalsDict, newSettings
del newSettings['self'], newSettings['True'], newSettings['False']
module = new.module('temp_settings_module')
module.__dict__.update(newSettings)
return self.readSettingsFromModule(module)
def readSettingsFromConfigFile(self, path, convert=True):
path = self.normalizePath(path)
fp = open(path)
settings = self.readSettingsFromConfigFileObj(fp, convert=convert)
fp.close()
return settings
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.
"""
## init methods
def __init__(self):
"""MUST BE CALLED BY SUBCLASSES"""
_SettingsCollector.__init__(self)
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 self._settings.has_key(key)
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 copy(self._settings)
def deepcopySettings(self):
"""Returns a deep copy of the settings dictionary"""
return 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 updateSettingsFromPySrcFile(self, path, merge=True):
"""Update the settings from variables in a Python source file.
This method will temporarily add the directory of src file to sys.path so
that import statements relative to that dir will work properly."""
newSettings = self.readSettingsFromPySrcFile(path)
self.updateSettings(newSettings,
merge=newSettings.get('mergeSettings',merge) )
def updateSettingsFromConfigFile(self, path, **kw):
"""Update the settings from a text file using the syntax accepted by
Python's standard ConfigParser module (like Windows .ini files).
"""
path = self.normalizePath(path)
fp = open(path)
self.updateSettingsFromConfigFileObj(fp, **kw)
fp.close()
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))
## methods for output representations of the settings
def _createConfigFile(self, outFile=None):
"""
Write all the settings that can be represented as strings to an .ini
style config string.
This method can only handle one level of nesting and will only work with
numbers, strings, and None.
"""
if outFile is None:
outFile = StringIO()
iniSettings = {'Globals':{}}
globals = iniSettings['Globals']
for key, theSetting in self.settings().items():
if type(theSetting) in convertableToStrTypes:
globals[key] = theSetting
if type(theSetting) is DictType:
iniSettings[key] = {}
for subKey, subSetting in theSetting.items():
if type(subSetting) in convertableToStrTypes:
iniSettings[key][subKey] = subSetting
sections = iniSettings.keys()
sections.sort()
outFileWrite = outFile.write # short-cut namebinding for efficiency
for section in sections:
outFileWrite("[" + section + "]\n")
sectDict = iniSettings[section]
keys = sectDict.keys()
keys.sort()
for key in keys:
if key == "__name__":
continue
outFileWrite("%s = %s\n" % (key, sectDict[key]))
outFileWrite("\n")
return outFile
def writeConfigFile(self, path):
"""Write all the settings that can be represented as strings to an .ini
style config file."""
path = self.normalizePath(path)
fp = open(path,'w')
self._createConfigFile(fp)
fp.close()
def getConfigString(self):
"""Return a string with the settings in .ini file format."""
return self._createConfigFile().getvalue()
# vim: shiftwidth=4 tabstop=4 expandtab
|