-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.py
125 lines (87 loc) · 3.59 KB
/
config.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
# coding: utf-8
# This file is part of https://github.com/marcus67/gitsynchista
import copy
import string
import six
import log
if six.PY2:
from ConfigParser import ConfigParser
else:
from configparser import ConfigParser
from importlib import reload
reload(log)
global logger
logger = log.open_logging(__name__)
class BaseConfig(object):
def getIntAttributes(self):
return list()
def getBooleanAttributes(self):
return list()
def dump(self):
self._dump(conf=self, parent_prefix=type(self).__name__ + '.')
def _dump(self, conf, parent_prefix):
global logger
for (key, value) in conf.__dict__.items():
attr_type = type(getattr(conf, key)).__name__
name = parent_prefix + key
#print attr_type
if attr_type in ('int', 'bool', 'str'):
logger.debug('%s=%s' % (name, str(value)))
elif attr_type == 'NoneType':
logger.debug('%s=<NONE>' % name)
else:
self._dump(value, name + '.')
class ConfigHandler(object):
def __init__(self, config_template):
self.config_template = config_template
def scan_section(self, sectionName, model):
for option in self.config_file.options(sectionName):
if not option in model.__dict__:
fmt = "configuration file contains invalid option '%s' in section '%s'"
raise Exception(fmt % (option, sectionName))
if option in model.getBooleanAttributes():
optionValue = self.config_file.get(sectionName, option).upper()
if optionValue in '1 TRUE YES WAHR JA J'.split():
setattr(model, option, True)
elif optionValue in '0 FALSE NO FALSCH NEIN N'.split():
setattr(model, option, False)
else:
fmt = "Invalid Boolean value '%s' in option '%s' of section '%s'"
raise Exception(fmt % (self.config_file.get(sectionName, option), option, sectionName))
elif option in model.getIntAttributes():
try:
intValue = int(self.config_file.get(sectionName, option))
setattr(model, option, intValue)
except Exception as e:
fmt = "Invalid numeric value '%s' in option '%s' of section '%s'"
raise Exception(fmt % (self.config_file.get(sectionName, option), option, sectionName))
else:
setattr(model, option, self.config_file.get(sectionName, option))
def read_config_file(self, filename):
global logger
fmt = "reading configuration file '%s' for config '%s'"
logger.info(fmt % (filename, type(self.config_template).__name__))
self.config_file = ConfigParser()
self.config_file.optionxform = str # make options case sensitive
config = copy.deepcopy(self.config_template)
error_message = None
try:
files_read = self.config_file.read([filename])
if len(files_read) != 1:
error_message = "Error while reading configuration file '%s'" % filename
except Exception as e:
fmt = "Error '%s' while reading configuration file '%s'"
error_message = fmt % (str(e), filename )
if error_message:
raise Exception(error_message)
for section_name in self.config_file.sections():
if section_name in self.config_template.__dict__:
sub_config = getattr(config, section_name)
self.scan_section(section_name, sub_config)
else:
raise Exception("Configuration file contains invalid section '%s'" % section_name)
return config
def test():
pass
if __name__ == '__main__':
test()