Browse Source

Add SettingsLoader.getremains() test + code refactoring in SettingsLoader

Yann Weber 8 years ago
parent
commit
7897203830

+ 28
- 30
lodel/settings/settings_loader.py View File

@@ -9,6 +9,11 @@ from lodel.settings.utils import *
9 9
    
10 10
 ##@brief Merges and loads configuration files
11 11
 class SettingsLoader(object):
12
+    
13
+    ## To avoid the DEFAULT section whose values are found in all sections, we
14
+    # have to give it an unsual name
15
+    DEFAULT_SECTION = 'lodel2_default_passaway_tip'
16
+
12 17
     ##@brief Constructor
13 18
     # @param conf_path str : conf.d path
14 19
     def __init__(self,conf_path):
@@ -17,39 +22,28 @@ class SettingsLoader(object):
17 22
         self.__conf=self.__merge()
18 23
     
19 24
     ##@brief Lists and merges files in settings_loader.conf_path
20
-    #
21
-    # 
22 25
     # @return dict()
23
-    # 
24 26
     def __merge(self):
25 27
         conf = dict()
26
-        dir_conf = os.open(self.__conf_path, os.O_RDONLY)
27
- 
28 28
         l_dir = glob.glob(self.__conf_path+'/*.ini')  
29 29
 
30 30
         for f_ini in l_dir:  
31
-            #To avoid the DEFAULT section whose values are found in all sections, we have to give it an unsual name
32
-            config = configparser.ConfigParser(default_section = 'lodel2_default_passaway_tip',interpolation=None)
31
+            config = configparser.ConfigParser(default_section = self.DEFAULT_SECTION ,interpolation=None)
33 32
             config.read(f_ini)
34
-
35
-            for sect in config:
36
-                if sect in conf:
37
-                    for param in config[sect]:
38
-                        if param not in conf[sect]: 
39
-                            conf[sect][param] = config[sect][param]
40
-                            if sect != 'DEFAULT': self.__conf_sv[sect + ':' + param]=f_ini
41
-                        else:
42
-                            raise SettingsError("Key attribute already defined : %s " % sect + '.' + param + ' dans ' + f_ini + ' et ' + self.__conf_sv[sect + ':' + param])                        
43
-                else:
44
-                    opts={}
45
-                    for key in config[sect]:
46
-                        opts[key] = config[sect].get(key)
47
-                        if sect != 'lodel2_default_passaway_tip': self.__conf_sv[sect + ':' + key]=f_ini
48
-                    conf.update({sect: opts})
49
-        os.close(dir_conf)
33
+            for section in [ s for s in config if s != self.DEFAULT_SECTION ]:
34
+                if section not in conf:
35
+                    conf[section] = dict()
36
+                for param in config[section]:
37
+                    if param not in conf[section]: 
38
+                        conf[section][param] = config[section][param]
39
+                        self.__conf_sv[section + ':' + param]=f_ini
40
+                    else:
41
+                        raise SettingsError("Error redeclaration of key %s in section %s. Found in %s and %s" % (
42
+            section,
43
+            param,
44
+            f_ini,
45
+            self.__conf_sv[section + ':' + param]))
50 46
         return conf
51
-        
52
-        
53 47
     
54 48
     ##@brief Returns option if exists default_value else and validates
55 49
     # @param section str : name of the section
@@ -70,7 +64,6 @@ class SettingsLoader(object):
70 64
              raise SettingsError("Default value mandatory for option %s" % keyname)
71 65
         else:
72 66
              return default_value
73
-                              
74 67
     
75 68
     ##@brief Returns the section to be configured
76 69
     # @param section_prefix str
@@ -94,8 +87,13 @@ class SettingsLoader(object):
94 87
             
95 88
         return sections;
96 89
     
97
-    ##@brief Returns the sections which have not been configured
98
-    # @return list of missing options
90
+    ## @brief Returns invalid settings
91
+    #
92
+    # This method returns all the settings that was not fecthed by 
93
+    # getsection() method. For the Settings object it allows to know
94
+    # the list of invalids settings keys
95
+    # @return a dict with SECTION_NAME+":"+KEY_NAME as key and the filename
96
+    # where the settings was found as value
99 97
     def getremains(self):
100
-        return list(self.__conf_sv)
101
-        
98
+        return self.__conf_sv
99
+    

+ 1
- 1
tests/settings/settings_examples/remains.conf.d/file1.ini View File

@@ -1,4 +1,4 @@
1
-[lodel2]
1
+[lodel2.section]
2 2
 a = a
3 3
 b = b
4 4
 c = c

+ 1
- 1
tests/settings/settings_examples/remains.conf.d/file2.ini View File

@@ -1,3 +1,3 @@
1
-[lodel2]
1
+[lodel2.section]
2 2
 d = d
3 3
 e = e

+ 20
- 3
tests/settings/test_settings_loader.py View File

@@ -35,7 +35,7 @@ class SettingsLoaderTestCase(unittest.TestCase):
35 35
         f=settings.getoption('lodel2.A.e','a',maFonction)
36 36
         f=settings.getoption('lodel2.A.e','titi',maFonction)
37 37
         g=settings.getremains()
38
-        self.assertEqual(g,[])
38
+        self.assertEqual(len(g),0)
39 39
         with self.assertRaises(SettingsError):
40 40
             loader = SettingsLoader('tests/settings/settings_examples/conf_raise.d')
41 41
     
@@ -118,8 +118,25 @@ class SettingsLoaderTestCase(unittest.TestCase):
118 118
         with self.assertRaises(NameError):
119 119
             sections = loader.getsection('lodel2.notexisting')
120 120
     
121
-    @unittest.skip("Waiting implementation")
122 121
     def test_remains(self):
123 122
         """ Testing the remains method of SettingsLoader """
124 123
         loader = SettingsLoader('tests/settings/settings_examples/remains.conf.d')
125
-        pass #TO BE DONE LATER
124
+        values = {
125
+            'lodel2.section': [ chr(i) for i in range(ord('a'), ord('f')) ],
126
+            'lodel2.othersection': [ chr(i) for i in range(ord('a'), ord('f')) ],
127
+        }
128
+        
129
+        expt_rem = []
130
+        for section in values:
131
+            for val in values[section]:
132
+                expt_rem.append('%s:%s' % (section, val))
133
+
134
+        self.assertEqual(sorted(expt_rem), sorted(loader.getremains().keys()))
135
+
136
+        for section in values:
137
+            for val in values[section]:
138
+                loader.getoption(section, val, dummy_validator)
139
+                expt_rem.remove('%s:%s' % (section, val))
140
+                self.assertEqual(   sorted(expt_rem),
141
+                                    sorted(loader.getremains().keys()))
142
+

Loading…
Cancel
Save