1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import os
16
17 import dico, base64
18 from copy import copy
19 from creole.cfgparser import EoleDict
20 from creole.error import TypeEoleError
21 from zephir.config import charset
22 from creole import eosfunc
23 from glob import glob
24
26 """sert de wrapper par dessus les dictionnaires creole1 et creole2
27 """
28
29 - def __init__(self, dicos=[], confdir='', mode='config', version='creole2'):
30 """initialise un objet dictionnaire avec la bonne api
31 """
32 self.version = version
33 config_file = ""
34 self.separators = {}
35 if confdir != '':
36 if mode == 'modif_config' and os.path.isfile(confdir+os.sep+'zephir.eol'):
37 config_file = confdir+os.sep+'zephir.eol'
38 elif mode in ['config','modif_config','modif_dico']:
39 if os.path.isfile(confdir+os.sep+'dico.eol'):
40 config_file = confdir+os.sep+'dico.eol'
41 elif os.path.isfile(confdir+os.sep+'module.eol'):
42 config_file = confdir+os.sep+'module.eol'
43 elif mode in ['migration', 'modif_migration'] and os.path.isfile(confdir+os.sep+'migration.eol'):
44 config_file = confdir+os.sep+'migration.eol'
45 self.multivars = []
46 self.autovars = []
47 if self.version == 'creole1':
48
49 self.dico = dico.DicoEole(dico_zephir=dicos)
50
51 dico_actuel = None
52 if config_file != "":
53 fic = open(config_file)
54 data = fic.readlines()
55 lines = [ unicode(line,'ISO-8859-1').encode(charset) for line in data ]
56 fic.close()
57
58 dico_actuel=dico.DicoEole(dico_zephir=[lines]).ParseDico()
59
60 if dico_actuel is not None:
61
62 dico_final = {}
63
64 for variable, data in self.dico.dictionnaire.items():
65
66 if dico_actuel.has_key(data[0]):
67 data[1] = dico_actuel[data[0]][0]
68 dico_final[variable] = data
69
70 self.dico.dictionnaire = dico_final
71 self.liste_vars = self.dico.liste_vars
72
73 elif self.version == 'creole2':
74
75 self.dico = EoleDict(logfile='/dev/null')
76 self.dicos = []
77
78 for creoledir in dicos:
79 if os.path.isdir(creoledir):
80 list_dic = glob(creoledir + '/*.xml')
81 list_dic.sort()
82 self.dicos.extend(list_dic)
83 self.dico.read_dir(creoledir)
84
85 if config_file != "" and mode != 'migration':
86 self.dico.load_values(config_file)
87 else:
88 if mode in ['migration', 'modif_migration']:
89
90 for path_conf in dicos:
91 for conf_file in ('module.eol','dico.eol'):
92 if os.path.isfile(os.path.join(os.path.dirname(path_conf),conf_file)):
93 self.dico.load_values(os.path.join(os.path.dirname(path_conf),conf_file))
94
95 if os.path.isfile(confdir+os.sep+'zephir.eol'):
96 res = self.dico.import_values(confdir+os.sep+'zephir.eol')
97 elif os.path.isfile(confdir+os.sep+'module.eol'):
98
99 self.dico.load_values(confdir+os.sep+'module.eol')
100
101 self.index = 0
102 self.vars = []
103 for family in self.dico.family_order:
104 for var in self.dico.families[family]['vars']:
105 self.vars.append(var)
106 if self.dico.variables[var].multi == True:
107
108 self.multivars.append(var)
109 self.multivars.extend(self.dico.groups.get(var,[]))
110 if self.dico.variables[var].auto != []:
111 self.autovars.append(var)
112
113 self.separators = self.dico.separators
114
115 ind = 0
116 self.liste_vars = {}
117 for var in self.vars:
118 self.liste_vars[var] = ind
119 ind += 1
120
122 """charge les dictionnaire depuis une liste de chaines xml
123 @param dicos: contenu des dictionnaires à implémenter + valeurs actuelles en fin de liste
124 """
125 if self.version == 'creole1':
126 dicos = base64.decodestring(dicos)
127 dicos = [dicos.splitlines(True)]
128 self.dico = dico.DicoEole(dico_zephir=dicos)
129 self.liste_vars = self.dico.liste_vars
130 elif self.version == 'creole2':
131
132 self.dico = EoleDict(logfile='/dev/null')
133 self.dicos = [base64.decodestring(dico_zeph) for dico_zeph in dicos[:-1]]
134
135 for xml_dict in self.dicos:
136 self.dico.read_string(xml_dict)
137
138 self.dico.store._sections = eval(dicos[-1])
139 self.dico._load_values()
140
141 self.index = 0
142 self.vars = []
143 for family in self.dico.family_order:
144 for var in self.dico.families[family]['vars']:
145 self.vars.append(var)
146 if self.dico.variables[var].multi == True:
147 self.multivars.append(var)
148 self.multivars.extend(self.dico.groups.get(var,[]))
149 if self.dico.variables[var].auto != []:
150 self.autovars.append(var)
151
152 self.separators = self.dico.separators
153
154 ind = 0
155 self.liste_vars = {}
156 for var in self.vars:
157 self.liste_vars[var] = ind
158 ind += 1
159
162
164 vals = []
165 allow_other = False
166 if self.version == 'creole1':
167 conditions = self.dico.dictionnaire[self.dico.liste_vars[var]][3]
168
169 for cond in conditions:
170 if cond.startswith('enumeration'):
171 try:
172 vals = eval(cond.split('??')[1])
173 except:
174
175 vals = ['oui','non']
176 break
177 elif self.version == 'creole2':
178 eolvar = self.dico.get_variable(var)
179 checks = eolvar.checks
180 for check in checks:
181 if check[0] == u'valid_enum':
182
183 for param in check[1]:
184 if param['name'] == 'checkval':
185 if param['value'] == "False":
186 allow_other = True
187 else:
188 vals.extend(eval(param['value']))
189 return vals, allow_other
190
192 """retourne les paramètres de la variable nom,valeur,libelle,variable cachée
193 """
194 if self.version == 'creole1':
195 self.dico.get_first()
196 elif self.version == 'creole2':
197 self.index = 0
198 return self._get_var_details()
199
201 if self.version == 'creole1':
202 self.dico.get_next()
203 elif self.version == 'creole2':
204 self.index = self.index + 1
205 try:
206 return self._get_var_details()
207 except:
208 raise dico.PositionError, 'fin de la liste des variables'
209
211 """retourne les paramètres d'une variable creole1/2
212 """
213 return self._get_var_details(varname)
214
216 """adapte la représentation des variables creole1/2 pour zephir
217 """
218
219 if self.version == 'creole2':
220 if varname != None:
221 var = self.dico.get_variable(varname)
222 self.index = self.vars.index(varname)
223 else:
224 var = self.dico.get_variable(self.vars[self.index])
225 obligatoire = False
226 if 'obligatoire' in [ check[0] for check in var.checks ]:
227 obligatoire = True
228 mime = var.mime
229 val = var.get_value()
230 if type(val) != list:
231 val = [val]
232 if val ==[]:
233 val = ['']
234 data = var.name, val, var.description, var.hidden, obligatoire, mime
235 elif self.version == 'creole1':
236 if varname != None:
237 name, val, description, hidden = self.dico.get_var(varname)
238 else:
239 name, val, description, hidden = self.dico.get_current()
240 checks = self.dico.dictionnaire[self.dico.liste_vars[name]][3]
241 if 'obligatoire' in checks:
242 obligatoire = True
243 liste_funcs = copy(checks)
244 liste_funcs.remove('obligatoire')
245
246 else:
247 obligatoire = False
248 liste_funcs = checks
249 data = name, [val], description, hidden, obligatoire, liste_funcs
250
251 return data
252
254 """ renvoit la valeur précédente """
255 if self.version == 'creole2':
256 try:
257 value = self.dico.get_prec_value(varname)[0]
258 except:
259 value = ''
260
261
262 elif self.version == 'creole1':
263 try:
264 value = self.dico.get_var(varname)[1]
265 except:
266 value = ''
267 return value
268
270 """ renvoit la valeur par défaut """
271 if self.version == 'creole2':
272 try:
273 value = self.dico.get_value(varname, default=True)[0]
274 except:
275 value = ''
276
277
278 elif self.version == 'creole1':
279 try:
280 value = self.dico.get_var(varname)[1]
281 except:
282 value = ''
283 return value
284
285 - def set_value(self, value, invisible=False, force=False):
286 if self.version == 'creole1':
287 if type(value) == list:
288 value = value[0]
289 self.dico.set_value(value, invisible, force=force)
290 elif self.version == 'creole2':
291 try:
292 self.dico.set_value(self.vars[self.index], value, force=force)
293 except Exception, e:
294 raise dico.InvalidDataError, e.args[0]
295
296 - def save(self, file='/dev/null', force=False, encode=False):
297 if self.version == 'creole1':
298 data = self.dico.save(file, encode=encode)
299 elif self.version == 'creole2':
300
301 self.dico.save_values(file,force)
302
303 data = [str(self.dico.store._sections)]
304 return data
305
307
308 data = []
309 if self.version == 'creole2':
310 for dic in self.dicos:
311 f = open(dic)
312 content = f.read()
313 f.close()
314 data.append(base64.encodestring(content))
315
316
317 for varname in self.dico.variables.keys():
318 var = self.dico.variables[varname]
319 if var.val == [] or var.val == ['']:
320
321
322 var.val = var.get_value(default=True)
323
324 for list_val in [var.val, var.valprec, var.valdefault]:
325 for ind in range(len(list_val)):
326 if type(list_val[ind]) == unicode:
327 list_val[ind] = list_val[ind].encode(config.charset)
328
329 if not self.dico.store.has_section(varname):
330 self.dico.store.add_section(varname)
331 self.dico.store.set(varname, 'val', var.val)
332 self.dico.store.set(varname, 'valprec', var.valprec)
333 self.dico.store.set(varname, 'valdefault', var.valdefault)
334 data.extend([str(self.dico.store._sections)])
335 return data
336 elif self.version == 'creole1':
337 return [base64.encodestring(self.save())]
338
340 """retourne les familles et les variables dans l'ordre de saisie
341 @param expert: mode de saisie (non expert par défaut)
342 @return: renvoie une liste ordonnée de familles [famille, liste_vars]
343 """
344 menu = []
345 if self.version == 'creole1':
346
347 middle = len(self.dico.dictionnaire)/2
348 end = len(self.dico.dictionnaire)-middle
349 vars1 = [self.dico.dictionnaire[numvar][0] for numvar in range(middle)]
350 vars2 = [self.dico.dictionnaire[middle + numvar][0] for numvar in range(end)]
351 menu.append(['general', False, vars1])
352 menu.append(['general_1', False, vars2])
353
354
355
356 else:
357
358 for family in self.dico.family_order:
359 data = self.dico.families[family]
360 if not data['mode'] == 'expert':
361
362 vars = data['vars']
363 menu.append([family.encode(charset), data['hidden'], vars])
364
365 if expert == True:
366 for family in self.dico.family_order:
367 data = self.dico.families[family]
368 if data['mode'] == 'expert':
369 vars = data['vars']
370 menu.append([family.encode(charset), data['hidden'], vars])
371 return menu
372
374
375 data = {}
376 if self.version == 'creole1':
377 for varname, values in self.dico.ParseDico().items():
378 data[varname] = values[0]
379 else:
380 for varname in self.dico.variables.keys():
381 data[varname] = separator.join(self.dico.get_value(varname))
382 return data
383