Browse Source

Bugfixes on settings default value handling

Yann Weber 8 years ago
parent
commit
c0c1e628fd

+ 39
- 41
lodel/settings/settings_loader.py View File

@@ -63,51 +63,49 @@ class SettingsLoader(object):
63 63
     # @return the option
64 64
     def getoption(self,section,keyname,validator,default_value=None,mandatory=False):
65 65
         conf=self.__conf
66
-        if section in conf:
67
-            sec=conf[section]
68
-            if keyname in sec:
69
-                optionstr=sec[keyname]['value']
70
-                try:
71
-                    option= validator(sec[keyname]['value'])
72
-                except Exception as e:
73
-                    # Generating nice exceptions
74
-                    if sec[keyname]['file'] == SettingsLoader.DEFAULT_FILENAME:
75
-                        expt = SettingsError(   msg = 'Mandatory settings not found',
76
-                                                key_id = section+'.'+keyname)
77
-                        self.__errors_list.append(expt)
78
-                    else:
79
-                        expt = SettingsValidationError(
80
-                                                        "For %s.%s : %s" % 
81
-                                                        (section, keyname,e)
82
-                        )
83
-                        expt2 = SettingsError(  msg = str(expt),
84
-                                                key_id = section+'.'+keyname,
85
-                                                filename = sec[keyname]['file'])
86
-                        self.__errors_list.append(expt2)
87
-                    return
88 66
 
89
-                try:
90
-                    del self.__conf_sv[section + ':' + keyname]
91
-                except KeyError: #allready fetched
92
-                    pass
93
-                return option
94
-            elif default_value is None and mandatory:
95
-                msg = "Default value mandatory for option %s" % keyname
96
-                expt = SettingsError(   msg = msg,
97
-                                        key_id = section+'.'+keyname,
98
-                                        filename = sec[keyname]['file'])
99
-                self.__errors_list.append(expt)
100
-                return
67
+        if section not in conf:
68
+            conf[section] = dict()
69
+
70
+        sec = conf[section]
71
+        if keyname in sec:
72
+            result = sec[keyname]['value']
73
+            try:
74
+                del self.__conf_sv[section + ':' + keyname]
75
+            except KeyError: #allready fetched
76
+                pass
77
+        elif default_value is None and mandatory:
78
+            msg = "Default value mandatory for option %s" % keyname
79
+            expt = SettingsError(   msg = msg,
80
+                                    key_id = section+'.'+keyname,
81
+                                    filename = sec[keyname]['file'])
82
+            self.__errors_list.append(expt)
83
+            return
84
+        else:
101 85
             sec[keyname]=dict()
102 86
             sec[keyname]['value'] = default_value
103 87
             sec[keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
104
-            return default_value
105
-        else:
106
-            conf[section]=dict()
107
-            conf[section][keyname]=dict()
108
-            conf[section][keyname]['value'] = default_value
109
-            conf[section][keyname]['file'] = SettingsLoader.DEFAULT_FILENAME
110
-            return default_value
88
+            result = default_value
89
+
90
+        try:
91
+            return validator(result)
92
+        except Exception as e:
93
+            # Generating nice exceptions
94
+            if False and sec[keyname]['file'] == SettingsLoader.DEFAULT_FILENAME:
95
+                expt = SettingsError(   msg = 'Mandatory settings not found',
96
+                                        key_id = section+'.'+keyname)
97
+                self.__errors_list.append(expt)
98
+            else:
99
+                expt = SettingsValidationError(
100
+                                                "For %s.%s : %s" % 
101
+                                                (section, keyname,e)
102
+                )
103
+                expt2 = SettingsError(  msg = str(expt),
104
+                                        key_id = section+'.'+keyname,
105
+                                        filename = sec[keyname]['file'])
106
+                self.__errors_list.append(expt2)
107
+            return
108
+
111 109
     ##@brief Sets option in a config section. Writes in the conf file
112 110
     # @param section str : name of the section
113 111
     # @param keyname str

+ 1
- 1
plugins/mongodb_datasource/confspec.py View File

@@ -6,7 +6,7 @@ CONFSPEC = {
6 6
     'lodel2.datasource.mongodb_datasource.*':{
7 7
         'read_only': (False, SettingValidator('bool')),
8 8
         'host': ('localhost', SettingValidator('host')),
9
-        'port': (None, SettingValidator('string')),
9
+        'port': (None, SettingValidator('string', none_is_valid = True)),
10 10
         'db_name':('lodel', SettingValidator('string')),
11 11
         'username': (None, SettingValidator('string')),
12 12
         'password': (None, SettingValidator('string'))

+ 1
- 1
plugins/mongodb_datasource/utils.py View File

@@ -38,7 +38,7 @@ def connection_string(host, port, username, password):
38 38
         raise RuntimeError("Password given but no username given...")
39 39
     host = 'localhost' if host is None else host
40 40
     ret += host
41
-    if port != None:
41
+    if port is not None:
42 42
         ret += ':'+str(port)
43 43
     return ret
44 44
 

+ 2
- 2
plugins/webui/confspec.py View File

@@ -8,8 +8,8 @@ CONFSPEC = {
8 8
                             SettingValidator('dummy')),
9 9
         'listen_port': (    '9090',
10 10
                             SettingValidator('int')),
11
-        'virtualenv': ('',
12
-                       SettingValidator('path')),
11
+        'virtualenv': (None,
12
+                       SettingValidator('path', none_is_valid=True)),
13 13
         'uwsgicmd': ('uwsgi_python3', SettingValidator('dummy')),
14 14
     },
15 15
     'lodel2.webui.sessions': {

+ 0
- 2
plugins/webui/main.py View File

@@ -15,8 +15,6 @@ def root_url():
15 15
 ##@brief uwsgi startup demo
16 16
 @LodelHook('lodel2_loader_main')
17 17
 def uwsgi_fork(hook_name, caller, payload):
18
-    from lodel.plugin.plugins import Plugin
19
-    Plugin.from_name('users')
20 18
 
21 19
     if Settings.webui.standalone:
22 20
         cmd='{uwsgi} --http-socket {addr}:{port} --module plugins.webui.run'

Loading…
Cancel
Save