|
@@ -9,6 +9,11 @@ from lodel.settings.utils import *
|
9
|
9
|
|
10
|
10
|
##@brief Merges and loads configuration files
|
11
|
11
|
class SettingsLoader(object):
|
|
12
|
+
|
|
13
|
+ ## To avoid the DEFAULT section whose values are found in all sections, we
|
|
14
|
+ # have to give it an unsual name
|
|
15
|
+ DEFAULT_SECTION = 'lodel2_default_passaway_tip'
|
|
16
|
+
|
12
|
17
|
##@brief Constructor
|
13
|
18
|
# @param conf_path str : conf.d path
|
14
|
19
|
def __init__(self,conf_path):
|
|
@@ -17,39 +22,28 @@ class SettingsLoader(object):
|
17
|
22
|
self.__conf=self.__merge()
|
18
|
23
|
|
19
|
24
|
##@brief Lists and merges files in settings_loader.conf_path
|
20
|
|
- #
|
21
|
|
- #
|
22
|
25
|
# @return dict()
|
23
|
|
- #
|
24
|
26
|
def __merge(self):
|
25
|
27
|
conf = dict()
|
26
|
|
- dir_conf = os.open(self.__conf_path, os.O_RDONLY)
|
27
|
|
-
|
28
|
28
|
l_dir = glob.glob(self.__conf_path+'/*.ini')
|
29
|
29
|
|
30
|
30
|
for f_ini in l_dir:
|
31
|
|
- #To avoid the DEFAULT section whose values are found in all sections, we have to give it an unsual name
|
32
|
|
- config = configparser.ConfigParser(default_section = 'lodel2_default_passaway_tip',interpolation=None)
|
|
31
|
+ config = configparser.ConfigParser(default_section = self.DEFAULT_SECTION ,interpolation=None)
|
33
|
32
|
config.read(f_ini)
|
34
|
|
-
|
35
|
|
- for sect in config:
|
36
|
|
- if sect in conf:
|
37
|
|
- for param in config[sect]:
|
38
|
|
- if param not in conf[sect]:
|
39
|
|
- conf[sect][param] = config[sect][param]
|
40
|
|
- if sect != 'DEFAULT': self.__conf_sv[sect + ':' + param]=f_ini
|
41
|
|
- else:
|
42
|
|
- raise SettingsError("Key attribute already defined : %s " % sect + '.' + param + ' dans ' + f_ini + ' et ' + self.__conf_sv[sect + ':' + param])
|
43
|
|
- else:
|
44
|
|
- opts={}
|
45
|
|
- for key in config[sect]:
|
46
|
|
- opts[key] = config[sect].get(key)
|
47
|
|
- if sect != 'lodel2_default_passaway_tip': self.__conf_sv[sect + ':' + key]=f_ini
|
48
|
|
- conf.update({sect: opts})
|
49
|
|
- os.close(dir_conf)
|
|
33
|
+ for section in [ s for s in config if s != self.DEFAULT_SECTION ]:
|
|
34
|
+ if section not in conf:
|
|
35
|
+ conf[section] = dict()
|
|
36
|
+ for param in config[section]:
|
|
37
|
+ if param not in conf[section]:
|
|
38
|
+ conf[section][param] = config[section][param]
|
|
39
|
+ self.__conf_sv[section + ':' + param]=f_ini
|
|
40
|
+ else:
|
|
41
|
+ raise SettingsError("Error redeclaration of key %s in section %s. Found in %s and %s" % (
|
|
42
|
+ section,
|
|
43
|
+ param,
|
|
44
|
+ f_ini,
|
|
45
|
+ self.__conf_sv[section + ':' + param]))
|
50
|
46
|
return conf
|
51
|
|
-
|
52
|
|
-
|
53
|
47
|
|
54
|
48
|
##@brief Returns option if exists default_value else and validates
|
55
|
49
|
# @param section str : name of the section
|
|
@@ -70,7 +64,6 @@ class SettingsLoader(object):
|
70
|
64
|
raise SettingsError("Default value mandatory for option %s" % keyname)
|
71
|
65
|
else:
|
72
|
66
|
return default_value
|
73
|
|
-
|
74
|
67
|
|
75
|
68
|
##@brief Returns the section to be configured
|
76
|
69
|
# @param section_prefix str
|
|
@@ -94,8 +87,13 @@ class SettingsLoader(object):
|
94
|
87
|
|
95
|
88
|
return sections;
|
96
|
89
|
|
97
|
|
- ##@brief Returns the sections which have not been configured
|
98
|
|
- # @return list of missing options
|
|
90
|
+ ## @brief Returns invalid settings
|
|
91
|
+ #
|
|
92
|
+ # This method returns all the settings that was not fecthed by
|
|
93
|
+ # getsection() method. For the Settings object it allows to know
|
|
94
|
+ # the list of invalids settings keys
|
|
95
|
+ # @return a dict with SECTION_NAME+":"+KEY_NAME as key and the filename
|
|
96
|
+ # where the settings was found as value
|
99
|
97
|
def getremains(self):
|
100
|
|
- return list(self.__conf_sv)
|
101
|
|
-
|
|
98
|
+ return self.__conf_sv
|
|
99
|
+
|