|
@@ -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
|