|
@@ -6,6 +6,7 @@ import copy
|
6
|
6
|
|
7
|
7
|
from lodel.settings.utils import *
|
8
|
8
|
from lodel.settings.validator import SettingsValidationError
|
|
9
|
+from lodel.settings.utils import SettingsError, SettingsErrors
|
9
|
10
|
|
10
|
11
|
|
11
|
12
|
##@brief Merges and loads configuration files
|
|
@@ -14,6 +15,9 @@ class SettingsLoader(object):
|
14
|
15
|
## To avoid the DEFAULT section whose values are found in all sections, we
|
15
|
16
|
# have to give it an unsual name
|
16
|
17
|
DEFAULT_SECTION = 'lodel2_default_passaway_tip'
|
|
18
|
+
|
|
19
|
+ ## @brief Virtual filename when default value is used
|
|
20
|
+ DEFAULT_FILENAME = 'default_value'
|
17
|
21
|
|
18
|
22
|
##@brief Constructor
|
19
|
23
|
# @param conf_path str : conf.d path
|
|
@@ -21,6 +25,8 @@ class SettingsLoader(object):
|
21
|
25
|
self.__conf_path=conf_path
|
22
|
26
|
self.__conf_sv=dict()
|
23
|
27
|
self.__conf=self.__merge()
|
|
28
|
+ # Stores errors
|
|
29
|
+ self.__errors_list = []
|
24
|
30
|
|
25
|
31
|
##@brief Lists and merges files in settings_loader.conf_path
|
26
|
32
|
# @return dict()
|
|
@@ -64,10 +70,21 @@ class SettingsLoader(object):
|
64
|
70
|
try:
|
65
|
71
|
option= validator(sec[keyname]['value'])
|
66
|
72
|
except Exception as e:
|
67
|
|
- raise SettingsValidationError(
|
68
|
|
- "For %s.%s : %s" %
|
69
|
|
- (section, keyname,e)
|
70
|
|
- )
|
|
73
|
+ # Generating nice exceptions
|
|
74
|
+ if sec[keyname]['file'] == SettingsLoader.DEFAULT_FILENAME:
|
|
75
|
+ expt = SettingsError( msg = 'Mandatory settings not found',
|
|
76
|
+ key_id = section+'.'+keyname)
|
|
77
|
+ self.__errors_list.append(expt)
|
|
78
|
+ else:
|
|
79
|
+ expt = SettingsValidationError(
|
|
80
|
+ "For %s.%s : %s" %
|
|
81
|
+ (section, keyname,e)
|
|
82
|
+ )
|
|
83
|
+ expt2 = SettingsError( msg = str(expt),
|
|
84
|
+ key_id = section+'.'+keyname,
|
|
85
|
+ filename = sec[keyname]['file'])
|
|
86
|
+ self.__errors_list.append(expt2)
|
|
87
|
+ return
|
71
|
88
|
|
72
|
89
|
try:
|
73
|
90
|
del self.__conf_sv[section + ':' + keyname]
|
|
@@ -75,16 +92,21 @@ class SettingsLoader(object):
|
75
|
92
|
pass
|
76
|
93
|
return option
|
77
|
94
|
elif default_value is None and mandatory:
|
78
|
|
- raise SettingsError("Default value mandatory for option %s" % keyname)
|
|
95
|
+ msg = "Default value mandatory for option %s" % keyname
|
|
96
|
+ expt = SettingsError( msg = msg,
|
|
97
|
+ key_id = section+'.'+keyname,
|
|
98
|
+ filename = sec[keyname]['file'])
|
|
99
|
+ self.__errors_list.append(expt)
|
|
100
|
+ return
|
79
|
101
|
sec[keyname]=dict()
|
80
|
102
|
sec[keyname]['value'] = default_value
|
81
|
|
- sec[keyname]['file'] = 'default_value'
|
|
103
|
+ sec[keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
|
82
|
104
|
return default_value
|
83
|
105
|
else:
|
84
|
106
|
conf[section]=dict()
|
85
|
107
|
conf[section][keyname]=dict()
|
86
|
108
|
conf[section][keyname]['value'] = default_value
|
87
|
|
- conf[section][keyname]['file'] = 'default_value'
|
|
109
|
+ conf[section][keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
|
88
|
110
|
return default_value
|
89
|
111
|
##@brief Sets option in a config section. Writes in the conf file
|
90
|
112
|
# @param section str : name of the section
|
|
@@ -94,7 +116,7 @@ class SettingsLoader(object):
|
94
|
116
|
# @return the option
|
95
|
117
|
def setoption(self,section,keyname,value,validator):
|
96
|
118
|
f_conf=copy.copy(self.__conf[section][keyname]['file'])
|
97
|
|
- if f_conf == 'default_value':
|
|
119
|
+ if f_conf == SettingsLoader.DEFAULT_FILENAME:
|
98
|
120
|
f_conf = self.__conf_path + '/generated.ini'
|
99
|
121
|
|
100
|
122
|
conf=self.__conf
|
|
@@ -138,7 +160,7 @@ class SettingsLoader(object):
|
138
|
160
|
|
139
|
161
|
return sections;
|
140
|
162
|
|
141
|
|
- ## @brief Returns invalid settings
|
|
163
|
+ ##@brief Returns invalid settings
|
142
|
164
|
#
|
143
|
165
|
# This method returns all the settings that was not fecthed by
|
144
|
166
|
# getsection() method. For the Settings object it allows to know
|
|
@@ -148,3 +170,17 @@ class SettingsLoader(object):
|
148
|
170
|
def getremains(self):
|
149
|
171
|
return self.__conf_sv
|
150
|
172
|
|
|
173
|
+ ##@brief Raise a SettingsErrors exception if some confs remains
|
|
174
|
+ #@note typically used at the end of Settings bootstrap
|
|
175
|
+ def raise_errors(self):
|
|
176
|
+ remains = self.getremains()
|
|
177
|
+ err_l = self.__errors_list
|
|
178
|
+ for key_id, filename in remains.items():
|
|
179
|
+ err_l.append(SettingsError( msg = "Invalid configuration",
|
|
180
|
+ key_id = key_id,
|
|
181
|
+ filename = filename))
|
|
182
|
+ if len(err_l) > 0:
|
|
183
|
+ raise SettingsErrors(err_l)
|
|
184
|
+ else:
|
|
185
|
+ return
|
|
186
|
+
|