Browse Source

Merge branch 'newlodel' of git.labocleo.org:lodel2 into newlodel

prieto 9 years ago
parent
commit
72c69d6769

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

9
    
9
    
10
 ##@brief Merges and loads configuration files
10
 ##@brief Merges and loads configuration files
11
 class SettingsLoader(object):
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
     ##@brief Constructor
17
     ##@brief Constructor
13
     # @param conf_path str : conf.d path
18
     # @param conf_path str : conf.d path
14
     def __init__(self,conf_path):
19
     def __init__(self,conf_path):
17
         self.__conf=self.__merge()
22
         self.__conf=self.__merge()
18
     
23
     
19
     ##@brief Lists and merges files in settings_loader.conf_path
24
     ##@brief Lists and merges files in settings_loader.conf_path
20
-    #
21
-    # 
22
     # @return dict()
25
     # @return dict()
23
-    # 
24
     def __merge(self):
26
     def __merge(self):
25
         conf = dict()
27
         conf = dict()
26
-        dir_conf = os.open(self.__conf_path, os.O_RDONLY)
27
- 
28
         l_dir = glob.glob(self.__conf_path+'/*.ini')  
28
         l_dir = glob.glob(self.__conf_path+'/*.ini')  
29
 
29
 
30
         for f_ini in l_dir:  
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
             config.read(f_ini)
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
         return conf
46
         return conf
51
-        
52
-        
53
     
47
     
54
     ##@brief Returns option if exists default_value else and validates
48
     ##@brief Returns option if exists default_value else and validates
55
     # @param section str : name of the section
49
     # @param section str : name of the section
70
              raise SettingsError("Default value mandatory for option %s" % keyname)
64
              raise SettingsError("Default value mandatory for option %s" % keyname)
71
         else:
65
         else:
72
              return default_value
66
              return default_value
73
-                              
74
     
67
     
75
     ##@brief Returns the section to be configured
68
     ##@brief Returns the section to be configured
76
     # @param section_prefix str
69
     # @param section_prefix str
94
             
87
             
95
         return sections;
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
     def getremains(self):
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
-[lodel2]
1
+[lodel2.section]
2
 a = a
2
 a = a
3
 b = b
3
 b = b
4
 c = c
4
 c = c

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

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

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

35
         f=settings.getoption('lodel2.A.e','a',maFonction)
35
         f=settings.getoption('lodel2.A.e','a',maFonction)
36
         f=settings.getoption('lodel2.A.e','titi',maFonction)
36
         f=settings.getoption('lodel2.A.e','titi',maFonction)
37
         g=settings.getremains()
37
         g=settings.getremains()
38
-        self.assertEqual(g,[])
38
+        self.assertEqual(len(g),0)
39
         with self.assertRaises(SettingsError):
39
         with self.assertRaises(SettingsError):
40
             loader = SettingsLoader('tests/settings/settings_examples/conf_raise.d')
40
             loader = SettingsLoader('tests/settings/settings_examples/conf_raise.d')
41
     
41
     
118
         with self.assertRaises(NameError):
118
         with self.assertRaises(NameError):
119
             sections = loader.getsection('lodel2.notexisting')
119
             sections = loader.getsection('lodel2.notexisting')
120
     
120
     
121
-    @unittest.skip("Waiting implementation")
122
     def test_remains(self):
121
     def test_remains(self):
123
         """ Testing the remains method of SettingsLoader """
122
         """ Testing the remains method of SettingsLoader """
124
         loader = SettingsLoader('tests/settings/settings_examples/remains.conf.d')
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