Browse Source

Enhancement in settings_loader.py : better support for empty values

when a value is composed only with space tabs etc consider it as None
Yann Weber 8 years ago
parent
commit
2ce93cc452
1 changed files with 20 additions and 14 deletions
  1. 20
    14
      lodel/settings/settings_loader.py

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

70
             conf[section] = dict()
70
             conf[section] = dict()
71
 
71
 
72
         sec = conf[section]
72
         sec = conf[section]
73
+        result = None
73
         if keyname in sec:
74
         if keyname in sec:
74
             result = sec[keyname]['value']
75
             result = sec[keyname]['value']
76
+            if result is not None:
77
+                result = result.strip()
78
+                if len(result) == 0:
79
+                    result = None
75
             try:
80
             try:
76
                 del self.__conf_sv[section + ':' + keyname]
81
                 del self.__conf_sv[section + ':' + keyname]
77
             except KeyError: #allready fetched
82
             except KeyError: #allready fetched
78
                 pass
83
                 pass
79
-        elif default_value is None and mandatory:
80
-            msg = "Default value mandatory for option %s" % keyname
81
-            expt = SettingsError(   msg = msg,
82
-                                    key_id = section+'.'+keyname,
83
-                                    filename = sec[keyname]['file'])
84
-            self.__errors_list.append(expt)
85
-            return
86
-        else:
87
-            sec[keyname]=dict()
88
-            sec[keyname]['value'] = default_value
89
-            sec[keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
90
-            result = default_value
91
-            logger.debug("Using default value for configuration key %s:%s" % (
92
-                section, keyname))
84
+        if result is None:
85
+            if default_value is None and mandatory:
86
+                msg = "Default value mandatory for option %s" % keyname
87
+                expt = SettingsError(   msg = msg,
88
+                                        key_id = section+'.'+keyname,
89
+                                        filename = sec[keyname]['file'])
90
+                self.__errors_list.append(expt)
91
+                return
92
+            else:
93
+                sec[keyname]=dict()
94
+                sec[keyname]['value'] = default_value
95
+                sec[keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
96
+                result = default_value
97
+                logger.debug("Using default value for configuration key %s:%s" % (
98
+                    section, keyname))
93
 
99
 
94
         try:
100
         try:
95
             return validator(result)
101
             return validator(result)

Loading…
Cancel
Save