Pārlūkot izejas kodu

Activated raise on invalid settings + enhancement in Settings exceptions

Yann Weber 8 gadus atpakaļ
vecāks
revīzija
67c53443b5

+ 3
- 0
lodel/settings/settings.py Parādīt failu

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

+ 45
- 9
lodel/settings/settings_loader.py Parādīt failu

@@ -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
+

+ 5
- 0
tests/settings/settings_examples/bad_conf.d/bad.ini Parādīt failu

@@ -0,0 +1,5 @@
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 Parādīt failu

@@ -0,0 +1,22 @@
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 Parādīt failu

@@ -6,6 +6,7 @@ from unittest import mock
6 6
 import tests.loader_utils
7 7
 from lodel.settings.settings import Settings
8 8
 from lodel.settings.settings import SettingsLoader
9
+from lodel.settings.utils import SettingsError, SettingsErrors
9 10
 
10 11
 def dummy_validator(value): return value
11 12
 
@@ -26,7 +27,3 @@ class SettingsTestCase(unittest.TestCase):
26 27
         Settings.set('lodel2.editorialmodel.emfile','examples/em_test.pickle', dummy_validator)
27 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 Parādīt failu

@@ -251,4 +251,9 @@ class SettingsLoaderTestCase(unittest.TestCase):
251 251
         self.assertEqual(value, 'test ok')
252 252
         
253 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')

Notiek ielāde…
Atcelt
Saglabāt