prieto 8 years ago
parent
commit
48165e0fcb

+ 14
- 7
lodel/settings/settings_loader.py View File

@@ -55,7 +55,7 @@ class SettingsLoader(object):
55 55
     # @param mandatory bool
56 56
     # @return the option
57 57
     def getoption(self,section,keyname,validator,default_value=None,mandatory=False):
58
-        conf=copy.copy(self.__conf)
58
+        conf=self.__conf
59 59
         if section in conf:
60 60
             sec=conf[section]
61 61
             if keyname in sec:
@@ -68,11 +68,15 @@ class SettingsLoader(object):
68 68
                 return option
69 69
             elif default_value is None and mandatory:
70 70
                  raise SettingsError("Default value mandatory for option %s" % keyname)
71
-            sec['keyname']=dict()
72
-            sec['keyname']['value'] = default_value
73
-            sec['keyname']['file'] = 'default_value'
71
+            sec[keyname]=dict()
72
+            sec[keyname]['value'] = default_value
73
+            sec[keyname]['file'] = 'default_value'
74 74
             return default_value
75 75
         else:
76
+            conf[section]=dict()
77
+            conf[section][keyname]=dict()
78
+            conf[section][keyname]['value'] = default_value
79
+            conf[section][keyname]['file'] = 'default_value'
76 80
             return default_value
77 81
     ##@brief Sets option in a config section. Writes in the conf file
78 82
     # @param section str : name of the section
@@ -82,16 +86,19 @@ class SettingsLoader(object):
82 86
     # @return the option
83 87
     def setoption(self,section,keyname,value,validator):
84 88
         f_conf=copy.copy(self.__conf[section][keyname]['file'])
89
+        if f_conf == 'default_value':
90
+            f_conf = self.__conf_path + '/generated.ini'
91
+
85 92
         conf=self.__conf
86 93
         conf[section][keyname] = value
87
-        if f_conf == 'default_value':
88
-            f_conf = self.conf_path + '/generated.ini'
89 94
         config = configparser.ConfigParser()
90 95
         config.read(f_conf)
96
+        config[section]={}
91 97
         config[section][keyname] = validator(value)
92
- 
98
+        
93 99
         with open(f_conf, 'w') as configfile:
94 100
             config.write(configfile)
101
+            
95 102
     ##@brief Saves new partial configuration. Writes in the conf files corresponding
96 103
     # @param sections dict
97 104
     # @param validators dict of callable : takes one argument value and raises validation fail

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

@@ -0,0 +1,5 @@
1
+[lodel2.foo.bar]
2
+foo = 42
3
+foobar = hello world
4
+foo_bar = foobar
5
+foo.bar = barfoo

+ 28
- 2
tests/settings/test_settings_loader.py View File

@@ -1,10 +1,13 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3 3
 import unittest
4
+import os.path
4 5
 
5 6
 from lodel.settings.utils import *
6 7
 from lodel.settings.settings_loader import SettingsLoader
7 8
 
9
+
10
+
8 11
 #A dummy validator that only returns the value
9 12
 def dummy_validator(value): return value
10 13
 #A dummy validator that always fails
@@ -86,8 +89,8 @@ class SettingsLoaderTestCase(unittest.TestCase):
86 89
         value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator, 'hello 42', False)
87 90
         self.assertEqual(value, 'hello 42')
88 91
         # for non existing section in file
89
-        # value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
90
-        # self.assertEqual(value, 'hello 42')
92
+        value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
93
+        self.assertEqual(value, 'hello 42')
91 94
 
92 95
     def test_getoption_complex(self):
93 96
         """ Testing behavior of getoption with less simple files & confs """
@@ -226,3 +229,26 @@ class SettingsLoaderTestCase(unittest.TestCase):
226 229
         self.assertEqual(option,'toto')
227 230
         option=loader.getoption('lodel2.A.e','a',dummy_validator)
228 231
         self.assertEqual(option,'ft')
232
+
233
+    def test_setoption_default_value(self):
234
+        loader = SettingsLoader('tests/settings/settings_examples/conf_setdef.d')
235
+            
236
+        # for non existing keys in file
237
+        value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator, 'hello 42', False)
238
+        self.assertEqual(value, 'hello 42')
239
+        # for non existing section in file
240
+        value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
241
+        self.assertEqual(value, 'hello 42')
242
+        
243
+        loader.setoption('lodel2.foo.bar', 'foofoofoo', 'test ok', dummy_validator)
244
+        loader.setoption('lodel2.foofoo', 'foofoofoo', 'test ok', dummy_validator)
245
+        self.assertTrue(os.path.isfile('tests/settings/settings_examples/conf_setdef.d/generated.ini'))
246
+        
247
+        loader = SettingsLoader('tests/settings/settings_examples/conf_setdef.d')
248
+        value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator)
249
+        self.assertEqual(value, 'test ok')
250
+        value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator)
251
+        self.assertEqual(value, 'test ok')
252
+        
253
+        os.remove('tests/settings/settings_examples/conf_setdef.d/generated.ini')
254
+        

Loading…
Cancel
Save