Przeglądaj źródła

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 lat temu
rodzic
commit
2ce93cc452
1 zmienionych plików z 20 dodań i 14 usunięć
  1. 20
    14
      lodel/settings/settings_loader.py

+ 20
- 14
lodel/settings/settings_loader.py Wyświetl plik

@@ -70,26 +70,32 @@ class SettingsLoader(object):
70 70
             conf[section] = dict()
71 71
 
72 72
         sec = conf[section]
73
+        result = None
73 74
         if keyname in sec:
74 75
             result = sec[keyname]['value']
76
+            if result is not None:
77
+                result = result.strip()
78
+                if len(result) == 0:
79
+                    result = None
75 80
             try:
76 81
                 del self.__conf_sv[section + ':' + keyname]
77 82
             except KeyError: #allready fetched
78 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 100
         try:
95 101
             return validator(result)

Loading…
Anuluj
Zapisz