Browse Source

Activated raise on invalid settings + enhancement in Settings exceptions

Yann Weber 8 years ago
parent
commit
67c53443b5

+ 3
- 0
lodel/settings/settings.py View File

213
                 if section not in self.__confs:
213
                 if section not in self.__confs:
214
                     self.__confs[section] = dict()
214
                     self.__confs[section] = dict()
215
                 self.__confs[section][kname] = loader.getoption(section, kname, validator, default)
215
                 self.__confs[section][kname] = loader.getoption(section, kname, validator, default)
216
+        # Checking unfectched values
217
+        loader.raise_errors()
218
+
216
         self.__confs_to_namedtuple()
219
         self.__confs_to_namedtuple()
217
         pass
220
         pass
218
     
221
     

+ 45
- 9
lodel/settings/settings_loader.py View File

6
 
6
 
7
 from lodel.settings.utils import *
7
 from lodel.settings.utils import *
8
 from lodel.settings.validator import SettingsValidationError
8
 from lodel.settings.validator import SettingsValidationError
9
+from lodel.settings.utils import SettingsError, SettingsErrors
9
 
10
 
10
    
11
    
11
 ##@brief Merges and loads configuration files
12
 ##@brief Merges and loads configuration files
14
     ## To avoid the DEFAULT section whose values are found in all sections, we
15
     ## To avoid the DEFAULT section whose values are found in all sections, we
15
     # have to give it an unsual name
16
     # have to give it an unsual name
16
     DEFAULT_SECTION = 'lodel2_default_passaway_tip'
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
     ##@brief Constructor
22
     ##@brief Constructor
19
     # @param conf_path str : conf.d path
23
     # @param conf_path str : conf.d path
21
         self.__conf_path=conf_path
25
         self.__conf_path=conf_path
22
         self.__conf_sv=dict()
26
         self.__conf_sv=dict()
23
         self.__conf=self.__merge()
27
         self.__conf=self.__merge()
28
+        # Stores errors
29
+        self.__errors_list = []
24
     
30
     
25
     ##@brief Lists and merges files in settings_loader.conf_path
31
     ##@brief Lists and merges files in settings_loader.conf_path
26
     # @return dict()
32
     # @return dict()
64
                 try:
70
                 try:
65
                     option= validator(sec[keyname]['value'])
71
                     option= validator(sec[keyname]['value'])
66
                 except Exception as e:
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
                 try:
89
                 try:
73
                     del self.__conf_sv[section + ':' + keyname]
90
                     del self.__conf_sv[section + ':' + keyname]
75
                     pass
92
                     pass
76
                 return option
93
                 return option
77
             elif default_value is None and mandatory:
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
             sec[keyname]=dict()
101
             sec[keyname]=dict()
80
             sec[keyname]['value'] = default_value
102
             sec[keyname]['value'] = default_value
81
-            sec[keyname]['file'] = 'default_value'
103
+            sec[keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
82
             return default_value
104
             return default_value
83
         else:
105
         else:
84
             conf[section]=dict()
106
             conf[section]=dict()
85
             conf[section][keyname]=dict()
107
             conf[section][keyname]=dict()
86
             conf[section][keyname]['value'] = default_value
108
             conf[section][keyname]['value'] = default_value
87
-            conf[section][keyname]['file'] = 'default_value'
109
+            conf[section][keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
88
             return default_value
110
             return default_value
89
     ##@brief Sets option in a config section. Writes in the conf file
111
     ##@brief Sets option in a config section. Writes in the conf file
90
     # @param section str : name of the section
112
     # @param section str : name of the section
94
     # @return the option
116
     # @return the option
95
     def setoption(self,section,keyname,value,validator):
117
     def setoption(self,section,keyname,value,validator):
96
         f_conf=copy.copy(self.__conf[section][keyname]['file'])
118
         f_conf=copy.copy(self.__conf[section][keyname]['file'])
97
-        if f_conf == 'default_value':
119
+        if f_conf == SettingsLoader.DEFAULT_FILENAME:
98
             f_conf = self.__conf_path + '/generated.ini'
120
             f_conf = self.__conf_path + '/generated.ini'
99
 
121
 
100
         conf=self.__conf
122
         conf=self.__conf
138
             
160
             
139
         return sections;
161
         return sections;
140
     
162
     
141
-    ## @brief Returns invalid settings
163
+    ##@brief Returns invalid settings
142
     #
164
     #
143
     # This method returns all the settings that was not fecthed by 
165
     # This method returns all the settings that was not fecthed by 
144
     # getsection() method. For the Settings object it allows to know
166
     # getsection() method. For the Settings object it allows to know
148
     def getremains(self):
170
     def getremains(self):
149
         return self.__conf_sv
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
+

+ 5
- 0
tests/settings/settings_examples/bad_conf.d/bad.ini View File

1
+[lodel2.bad]
2
+foo = bar
3
+bar = foo
4
+[badbad]
5
+bad = bad

+ 22
- 0
tests/settings/settings_examples/bad_conf.d/lodel2.ini View File

1
+[lodel2]
2
+debug = False
3
+sitename = noname
4
+plugins_path = plugins
5
+plugins = dummy, webui, dummy_datasource, datasources
6
+
7
+[lodel2.logging.stderr]
8
+level = DEBUG
9
+filename = -
10
+context = True
11
+
12
+[lodel2.editorialmodel]
13
+groups = 
14
+emfile = editorial_model.pickle
15
+dyncode = leapi_dyncode.py
16
+editormode = True
17
+
18
+[lodel2.datasources.main]
19
+identifier = dummy.example
20
+
21
+[lodel2.datasource.dummy.example]
22
+dummy =

+ 1
- 4
tests/settings/test_settings.py View File

6
 import tests.loader_utils
6
 import tests.loader_utils
7
 from lodel.settings.settings import Settings
7
 from lodel.settings.settings import Settings
8
 from lodel.settings.settings import SettingsLoader
8
 from lodel.settings.settings import SettingsLoader
9
+from lodel.settings.utils import SettingsError, SettingsErrors
9
 
10
 
10
 def dummy_validator(value): return value
11
 def dummy_validator(value): return value
11
 
12
 
26
         Settings.set('lodel2.editorialmodel.emfile','examples/em_test.pickle', dummy_validator)
27
         Settings.set('lodel2.editorialmodel.emfile','examples/em_test.pickle', dummy_validator)
27
         Settings.set('lodel2.editorialmodel.editormode','True', dummy_validator)
28
         Settings.set('lodel2.editorialmodel.editormode','True', dummy_validator)
28
         
29
         
29
-    def test_conf(self):
30
-        pass
31
-        
32
-

+ 6
- 1
tests/settings/test_settings_loader.py View File

251
         self.assertEqual(value, 'test ok')
251
         self.assertEqual(value, 'test ok')
252
         
252
         
253
         os.remove('tests/settings/settings_examples/conf_setdef.d/generated.ini')
253
         os.remove('tests/settings/settings_examples/conf_setdef.d/generated.ini')
254
-        
254
+        
255
+    def test_invalid_conf(self):
256
+        from lodel.settings.settings import Settings
257
+        Settings.stop()
258
+        with self.assertRaises(SettingsErrors):
259
+            Settings('tests/settings/settings_examples/bad_conf.d')

Loading…
Cancel
Save