Пример конфига с использованием exec.

 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
class Config(object):
def __init__(self):
self.values = self.default()
self.basedir = ''
self.context = {}
def __getitem__(self, key):
return self.values[key]
def __setitem__(self, key, value):
self.values[key] = value
_basedir = ''
def set_basedir(self, path):
self._basedir = os.path.abspath(path).decode('cp1251')
basedir = property(lambda self: self._basedir, set_basedir)
def as_path(self, key, default = None):
path = self.get(key, default)
if type(path) in types.StringTypes:
path = os.path.abspath(os.path.join(self.basedir, path))
return path
def get(self, key, default = None):
return self.values.get(key, default)
def default(self):
return {
'logfile': 'staber.conf',
'logdebug': False}
def upload(self, filename):
filename = os.path.abspath(filename)
if not os.path.isfile(filename):
print '[error] File "%s" not found'%filename
try:
code = open(filename, 'r').read()
values = self.context.copy()
exec code in values
values = dict([(key, value) for key, value in values.items()
if not key.startswith('_') and not key in self.context])
self.values.update(values)
return True
except:
etype, value, tb = sys.exc_info()
print '[error] %s: %s'%(etype, value)
return False