Commit 7d6e18fbf406f73ae10f06cdb221725c5f243853
1 parent
18f847bf
Exists in
dev
Classe ConfigPyros pour lire les fichiers XML de configuration
Showing
1 changed file
with
202 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,202 @@ |
1 | +# python c:\srv\develop\pyros\config\configpyros.py | |
2 | +# ---------------------------------------------------------------------------------------------------- | |
3 | +# Parameter attributes = section, key, value, type, unit, access, comment, label, reference, timestamp | |
4 | +# ---------------------------------------------------------------------------------------------------- | |
5 | +from lxml import etree | |
6 | +import os | |
7 | + | |
8 | +class ConfigPyros: | |
9 | + | |
10 | + # === Constants | |
11 | + NO_ERROR = 0 | |
12 | + ERR_FILE_NOT_EXISTS = 100 | |
13 | + ERR_LOAD_ERROR = 200 | |
14 | + ERR_TAG_ROOT_IS_NOT_UNIT = 201 | |
15 | + ERR_TAG_ROOT_HAS_NO_ATTRIBUTE_ALIAS = 202 | |
16 | + | |
17 | + # === Private variables | |
18 | + _last_errno = NO_ERROR | |
19 | + _config_filename = "" | |
20 | + _config_filename_mtime = 0 | |
21 | + _config_filename_need_to_be_read = True | |
22 | + _unit_component_params = [] | |
23 | + _unit_component_params_valid = False | |
24 | + | |
25 | + def get_last_errno(self): | |
26 | + """ Get the last error code | |
27 | + """ | |
28 | + return self._last_errno | |
29 | + | |
30 | + def set_configfile(self, config_filename:str ): | |
31 | + """ To set the config file name | |
32 | + | |
33 | + :param config_filename: A string that contains the full name of the configuration file. | |
34 | + :type config_filename: string | |
35 | + """ | |
36 | + if config_filename == "" or config_filename != self._config_filename: | |
37 | + self._config_filename = config_filename | |
38 | + self._config_filename_mtime = 0 | |
39 | + self._unit_component_params_valid = False | |
40 | + self._unit_component_params = [] | |
41 | + self._configfile_verify() | |
42 | + return | |
43 | + | |
44 | + def get_configfile(self): | |
45 | + """ Get the current config file name | |
46 | + """ | |
47 | + return self._config_filename | |
48 | + | |
49 | + def _configfile_verify(self): | |
50 | + """ Update parameters of the configuration file (existing, modification date) | |
51 | + """ | |
52 | + self._config_filename_need_to_be_read = False | |
53 | + self._last_errno = self.NO_ERROR | |
54 | + if os.path.isfile(self._config_filename): | |
55 | + # --- verify the date of last modification of the file | |
56 | + config_filename_mtime = os.path.getmtime(self._config_filename) | |
57 | + if config_filename_mtime > self._config_filename_mtime: | |
58 | + self._config_filename_need_to_be_read = True | |
59 | + #### self._config_filename_mtime = config_filename_mtime | |
60 | + else: | |
61 | + self._last_errno = self.ERR_FILE_NOT_EXISTS | |
62 | + | |
63 | + | |
64 | + def load(self, verbose:bool = False): | |
65 | + """ To load the config file if necessary | |
66 | + | |
67 | + :Example: | |
68 | + | |
69 | + ConfigPyros().load() | |
70 | + """ | |
71 | + | |
72 | + self._configfile_verify() | |
73 | + errno = config.get_last_errno() | |
74 | + if errno != config.NO_ERROR: | |
75 | + return | |
76 | + if self._config_filename_need_to_be_read == False: | |
77 | + return | |
78 | + | |
79 | + # --- Read the configuration file and parse it | |
80 | + self._last_errno = self.NO_ERROR | |
81 | + unit_component_params = [] | |
82 | + try: | |
83 | + tree = etree.parse(self._config_filename) | |
84 | + # --- Get root from the tree | |
85 | + root = tree.getroot() | |
86 | + | |
87 | + # === Verify the root tag is 'unit' | |
88 | + verify_unit = root.tag | |
89 | + if (verify_unit != "unit"): | |
90 | + self._last_errno = self.ERR_TAG_ROOT_IS_NOT_UNIT | |
91 | + return | |
92 | + | |
93 | + # === Get the alias value of the root tag 'unit' | |
94 | + alias_unit = root.attrib['alias'] | |
95 | + if (alias_unit == ""): | |
96 | + self._last_errno = self.ERR_TAG_ROOT_HAS_NO_ATTRIBUTE_ALIAS | |
97 | + return | |
98 | + | |
99 | + # === Get the mount aliases | |
100 | + alias_mounts = [] | |
101 | + for elem in root.xpath("/unit/mount"): | |
102 | + # print(elem.tag, elem.attrib) | |
103 | + alias_mounts.append(elem.attrib['alias']) | |
104 | + if (verbose==True): | |
105 | + print("alias_mounts = {}".format(alias_mounts)) | |
106 | + | |
107 | + # === Get the channel aliases | |
108 | + alias_channels = [] | |
109 | + for elem in root.xpath("/unit/channel"): | |
110 | + # print(elem.tag, elem.attrib) | |
111 | + alias_channels.append(elem.attrib['alias']) | |
112 | + if (verbose==True): | |
113 | + print("alias_channels = {}".format(alias_channels)) | |
114 | + | |
115 | + # === Get the association of channels for this mount | |
116 | + unit_components = [] | |
117 | + for elem in root: | |
118 | + if elem.tag == "param": | |
119 | + attributes = elem.attrib | |
120 | + if attributes['section'] != "instanciation": | |
121 | + continue | |
122 | + alias_param_unit = attributes['value'] | |
123 | + for alias_mount in alias_mounts: | |
124 | + if alias_param_unit == alias_mount: | |
125 | + key = attributes['key'] | |
126 | + dico = {'tag':'mount','instanciation':key,'alias':alias_param_unit} | |
127 | + unit_components.append(dico) | |
128 | + for alias_channel in alias_channels: | |
129 | + if alias_param_unit == alias_channel: | |
130 | + key = attributes['key'] | |
131 | + dico = {'tag':'channel','instanciation':key,'alias':alias_param_unit} | |
132 | + unit_components.append(dico) | |
133 | + if (verbose==True): | |
134 | + print("unit_components = {}".format(unit_components)) | |
135 | + | |
136 | + # === Get detailed informations about the unit components | |
137 | + for unit_component in unit_components: | |
138 | + tag = unit_component['tag'] | |
139 | + instanciation = unit_component['instanciation'] | |
140 | + alias = unit_component['alias'] | |
141 | + params = [] | |
142 | + for elem in root: | |
143 | + if (elem.tag == tag) and (elem.attrib['alias'] == alias): | |
144 | + for el in elem: | |
145 | + if (el.tag == "param"): | |
146 | + params.append(el.attrib) | |
147 | + unit_component_params.append({'component':tag, 'instanciation':instanciation, 'alias':alias, 'params':params}) | |
148 | + if (verbose==True): | |
149 | + print("unit_component_params = {}".format(unit_component_params)) | |
150 | + self._unit_component_params = unit_component_params | |
151 | + self._unit_component_params_valid = True | |
152 | + self._config_filename_mtime = os.path.getmtime(self._config_filename) | |
153 | + except: | |
154 | + self._last_errno = self.ERR_LOAD_ERROR | |
155 | + return | |
156 | + | |
157 | + def get_paramvalue(self, instanciation:str, section:str, key:str): | |
158 | + if self._unit_component_params_valid == False: | |
159 | + return "" | |
160 | + unit_component_params = self._unit_component_params | |
161 | + paramvalue = "" | |
162 | + for unit_component_param in unit_component_params: | |
163 | + component_instanciation = unit_component_param['instanciation'] | |
164 | + if (component_instanciation == instanciation): | |
165 | + for param in unit_component_param['params']: | |
166 | + param_section = param['section'] | |
167 | + param_key = param['key'] | |
168 | + if (param_section == section) and (param_key == key): | |
169 | + paramvalue = param['value'] | |
170 | + break | |
171 | + if (paramvalue != ""): | |
172 | + break | |
173 | + return paramvalue | |
174 | + | |
175 | + def __init__(self, config_filename:str = ""): | |
176 | + self._last_errno = self.NO_ERROR | |
177 | + self._unit_component_params = [] | |
178 | + self.set_configfile(config_filename) | |
179 | + | |
180 | +if __name__ == "__main__": | |
181 | + # --- Set the filename of the configuration | |
182 | + config_filename = 'c:/srv/develop/pyros/config/config_unit_simulunit1.xml' | |
183 | + #config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml' | |
184 | + # --- Instanciate an object for configuration | |
185 | + config = ConfigPyros(config_filename) | |
186 | + #config.set_configfile(config_filename) | |
187 | + if config.get_last_errno() != config.NO_ERROR: | |
188 | + print("Error code = {}".format(config.get_last_errno())) | |
189 | + else: | |
190 | + # --- Load the configuration file if needed only (i.e. modified) | |
191 | + config.load() | |
192 | + # --- get the parameter value of <mount1/localization/home> | |
193 | + instanciation = 'mount1' | |
194 | + section = 'localization' | |
195 | + key = 'home' | |
196 | + paramvalue = config.get_paramvalue(instanciation, section, key) | |
197 | + print("paramvalue <{}/{}/{}> = {}".format(instanciation, section, key,paramvalue)) | |
198 | + # | |
199 | + #print("unit_component_params = {}".format(unit_component_params)) | |
200 | + #component = unit_component_params[0] | |
201 | + #params = component['params'] | |
202 | + #print("params = {}".format(params)) | ... | ... |