1 # Copyright (C) 2007-2013 EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from Extensions.i18n import tr
21 from Accas.A_BLOC import BLOC
25 from convert_python import PythonParser
27 pattern_comment_slash = re.compile(r"^\s*/")
28 pattern_eta = re.compile(r".*&ETA.*")
29 pattern_fin = re.compile(r".*&FIN.*")
30 pattern_oui = re.compile(r"^\s*(oui|OUI|YES|yes|TRUE|VRAI)\s*$")
31 pattern_non = re.compile(r"^\s*(non|NON|NO|no|FALSE|FAUX)\*s$")
32 pattern_blanc = re.compile(r"^\s*$")
33 pattern_listeVide = re.compile(r"^\s*'\s*'\s*$")
35 pattern_ligne=re.compile(r'^\s*(?P<ident>[^=:]*)\s*[:=]\s*(?P<reste>.*)$')
37 # Attention aux listes de flottants
38 pattern_liste=re.compile(r'^\s*(?P<valeur>[+-.\w]+(\s*;\s*[+-.\w]+)+)\s*(?P<reste>.*)$')
39 pattern_flottant=re.compile(r'^\s*(?P<valeur>[+-]?((\d+(\.\d*)?)|(\.\d+))([dDeE][+-]?\d+)?)\s*(?P<reste>.*)$')
40 pattern_texteQuote = re.compile (r"^\s*(?P<valeur>'[^']+(''[^']+)*')\s*(?P<reste>.*)$")
41 pattern_texteSimple = re.compile (r"(?P<valeur>(^|\s)\s*[\w\.-]+)\s*(?P<reste>.*)$")
42 pattern_texteVide = re.compile (r"^\s*(?P<valeur>'')\s*(?P<reste>.*)$")
44 pattern_ContientDouble=re.compile (r"^.*''.*$")
47 # le pattern texte reconnait
48 #nom1 nom 2 : ou = chaine entre '
49 # avec eventuellement des quotes au milieu par exemple
50 # TITRE = 'TELEMAC 2D : GOUTTE D''EAU DANS UN BASSIN$'
51 # m.group("texte") va rendre 'TELEMAC 2D : GOUTTE D''EAU DANS UN BASSIN$'
54 #Si le code n est pas Telemac
56 from aideAuxConvertisseurs import DicoEficasToCas, ListeSupprimeCasToEficas
57 from aideAuxConvertisseurs import ListeCalculCasToEficas, DicoAvecMajuscules
58 from enumDicoTelemac2 import DicoEnumCasEn
62 from Extensions import localisation
64 from determine import monEnvQT5
70 Return a dictionary containing the description needed to load the plugin
74 'factory' : TELEMACParser
77 class TELEMACParser(PythonParser):
79 This converter works like PythonParser, except that it also initializes all
80 model variables to None in order to avoid Python syntax errors when loading
81 a file with a different or inexistent definition of variables.
84 def convert(self, outformat, appli=None):
85 self.dicoInverseFrancais=appli.readercata.dicoInverseFrancais
86 self.dicoAnglaisFrancais=appli.readercata.dicoAnglaisFrancais
87 self.dicoFrancaisAnglais=appli.readercata.dicoFrancaisAnglais
88 self.dicoMC=appli.readercata.dicoMC
89 self.Ordre_Des_Commandes=appli.readercata.Ordre_Des_Commandes
92 #print self.dicoInverseFrancais
93 #text = PythonParser.convert(self, outformat, appli)
96 l_lignes = string.split(self.text,'\n')
98 for ligne in l_lignes :
100 if pattern_comment_slash.match(ligne) : continue
101 if pattern_eta.match(ligne) : continue
102 if pattern_fin.match(ligne) : continue
103 if pattern_blanc.match(ligne) : continue
107 while finLigne != "" :
110 m=pattern_ligne.match(finLigne)
112 print "________________________________________________"
113 print 'pb avec ****', finLigne , '**** dans ', ligne
114 print "________________________________________________"
117 simp=self.traiteIdent(m.group('ident'))
118 finLigne=m.group('reste')
120 # attention, l ordre des if est important
121 if pattern_liste.match(finLigne) :
122 m=pattern_liste.match(finLigne)
123 elif pattern_texteQuote.match(finLigne) :
124 m=pattern_texteQuote.match(finLigne)
125 elif pattern_flottant.match(finLigne) :
126 m=pattern_flottant.match(finLigne)
127 elif pattern_texteVide.match(finLigne):
128 m=pattern_texteVide.match(finLigne)
129 elif pattern_texteSimple.match(finLigne):
130 m=pattern_texteSimple.match(finLigne)
132 print "________________________________________________"
133 print 'pb avec ****', finLigne , '**** dans ', ligne
135 print "________________________________________________"
138 valeur=m.group('valeur')
139 if pattern_blanc.match(valeur) : valeur=None
141 if pattern_flottant.match(finLigne) :
142 valeur=re.sub("d","e",valeur)
143 valeur=re.sub("D","E",valeur)
145 if pattern_liste.match(finLigne):
146 valeur=valeur.split(";")
148 finLigne=m.group('reste')
152 self.dictSimp[simp]=valeur
154 if 'Title' not in self.dictSimp.keys() and 'Titre' not in self.dictSimp.keys():
156 self.dictSimp['Titre']=os.path.basename(self.filename)
159 for simp in self.dictSimp.keys():
160 #print simp, " : ", self.dictSimp[simp]
162 if simp in TELEMACParser.__dict__.keys() : apply(TELEMACParser.__dict__[simp],(self,))
164 for simp in self.dictSimp.keys():
165 if simp in ListeSupprimeCasToEficas: continue
166 if simp not in self.dicoInverseFrancais.keys() :
168 print "pb avec dans dicoInverseFrancais", simp,'------'
172 listeGenea=self.dicoInverseFrancais[simp]
174 for (u,v) in listeGenea :
175 if isinstance(v,BLOC): continue
176 listeGeneaReverse.append(u)
177 listeGeneaReverse.reverse()
178 dicoTravail=dicoParMC
180 #print (listeGeneaReverse[0:-1])
181 while i < len(listeGeneaReverse[0:-1]) :
182 mot=listeGeneaReverse[i]
184 if mot not in dicoTravail.keys(): dicoTravail[mot]={}
185 dicoTravail=dicoTravail[mot]
186 dicoTravail[simp]=self.dictSimp[simp]
189 #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
191 #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
192 listeMC=self.tri(dicoParMC.keys())
194 #print "----------- traitement de " , k
195 self.textePy += self.dicoFrancaisAnglais[k] + "("
196 self.traiteMC(dicoParMC[k])
197 self.textePy += ");\n"
198 #print "----------- "
204 #----------------------------------------
205 def redecoupeSimp(self,simp):
206 # traite les - dans les identifants python
207 # les remplace par des _
208 #----------------------------------------
209 # replace('-','_') uniquement dans les identifiants
210 while simp.find('-') > 0 :
212 if ind==len(simp)-1 : break
213 simp=simp[0:ind]+'_'+simp[ind+1].upper()+simp[ind+2:]
215 #----------------------------------------
216 def traiteIdent(self,listeIdent):
217 # Recree l identifiant Python
218 #----------------------------------------
219 while listeIdent[-1] == " " or listeIdent[-1] == '\t' : listeIdent=listeIdent[0:-1]
220 while listeIdent[0] == " " or listeIdent[0] == '\t' : listeIdent=listeIdent[1:]
222 listeIdent=re.sub("'"," ",listeIdent)
223 motsInIdent=string.split(listeIdent,' ')
225 for mot in motsInIdent:
226 if re.compile(r"^\s*$").match(mot) : continue
227 mot=mot.replace('_','__')
228 simp=simp+mot[0].upper() +mot[1:].lower()+'_'
231 while simp[-1] == " " : simp=simp[0:-1]
232 if simp.find('-') > 0 : simp=self.redecoupeSimp(simp)
236 def traiteMC(self,dico) :
237 for k in dico.keys() :
239 if k not in self.dicoMC.keys() : kA=self.dicoFrancaisAnglais[k]
242 if isinstance(obj,FACT): self.generFACT(obj,kA,valeur)
243 elif isinstance(obj,BLOC): self.generBLOC(obj,kA,valeur)
244 elif isinstance(obj,SIMP): self.generSIMP(obj,kA,valeur)
245 else : print "%%%%%%%%%%%\n", "pb generation pour", k, obj, "\n%%%%%%%%%%%"
247 #print "_____________"
249 def generFACT(self,obj,nom,valeur):
250 if nom in TELEMACParser.__dict__.keys() :
251 apply(TELEMACParser.__dict__[nom],(self,))
253 self.textePy += nom + "=_F( "
254 self.traiteMC(valeur)
255 self.textePy += '),\n'
258 def generBLOC(self,obj,nom,valeur):
262 def generSIMP(self,obj,nom,valeur):
263 if nom in ("Prescribed_Flowrates", "Prescribed_Velocities", "Prescribed_Elevations" ): return
264 #print "___________________________"
267 if valeur == None : print nom
269 if 'TXM' in obj.type :
271 if pattern_ContientDouble.match(valeur):
272 valeur=re.sub("''","\'\'",valeur)
279 # ceinture et bretelle si les re sont correctes -)
280 while valeur[-1] == " " or valeur[-1] == '\t' : valeur=valeur[0:-1]
281 while valeur[0] == " " or valeur[0] == '\t' : valeur=valeur[1:]
283 if pattern_ContientDouble.match(valeur):
284 valeur=re.sub("''","\'\'",valeur)
291 try : valeur=eval(valeur,{})
293 if nom in DicoEnumCasEn.keys():
294 #print "est dans le dico des enum, valeur simple"
295 try : valeur=DicoEnumCasEn[nom][valeur]
299 if obj.into != [] and obj.into != None :
300 for possible in obj.into :
301 if possible.upper() == valeur.upper():
304 v=valeur[0].upper()+valeur[1:].lower()
306 if possible.upper() == v2.upper():
310 if 'Fichier' in obj.type or 'TXM' in obj.type or 'Repertoire' in obj.type :
312 while valeur[-1] == " " : valeur=valeur[0:-1]
313 while valeur[0] == " " : valeur=valeur[1:]
314 self.textePy += nom + "= '" + str(valeur) +"' ,"
317 if bool in obj.type :
318 if valeur == True : self.textePy += nom + "= True,"
319 elif valeur == False : self.textePy += nom + "= False,"
320 elif pattern_oui.match(valeur) : self.textePy += nom + "= True,"
321 elif pattern_non.match(valeur) : self.textePy += nom + "= False,"
322 else : self.textePy += nom + "= None,"
324 self.textePy += nom + "=" + str(valeur) +","
327 if pattern_listeVide.match(valeur) :
328 self.textePy += nom + "= None,"
330 while valeur[-1] == " " or valeur[-1]=="'" : valeur=valeur[0:-1]
331 while valeur[0] == " " or valeur[-0]=="'" : valeur=valeur[1:]
333 if ";" in valeur : valeur=valeur.split(';')
334 elif "," in valeur : valeur=valeur.split(',')
336 if valeur == None : return
341 if nom in DicoEnumCasEn.keys():
342 #print "est dans le dico des enum, valeurs multiples"
343 try : v=DicoEnumCasEn[nom][v]
346 self.textePy += nom + "=" + str(newVal) +","
350 def tri(self, listeIn):
351 if len(listeIn) == 1 : return listeIn
352 if self.Ordre_Des_Commandes == None : return listeIn
353 #print self.Ordre_Des_Commandes
354 listeOut=[listeIn[0],]
355 for kF in listeIn[1:]:
356 k=str(self.dicoFrancaisAnglais[kF])
357 ordreK=self.Ordre_Des_Commandes.index(k)
359 while i < len(listeOut):
360 ordreI=self.Ordre_Des_Commandes.index(self.dicoFrancaisAnglais[listeOut[i]])
361 if ordreK < ordreI : break
363 listeOut.insert(i,kF)
366 def Processeurs_Paralleles(self):
368 if self.dictSimp["Processeurs_Paralleles"] == 0 : del self.dictSimp["Processeurs_Paralleles"]
369 else : self.dictSimp["Parallel_Computation"]="Parallel"
371 def decoupeListe(self,valeurs,label):
375 for prefixe in ('_U_And_V','_H'):
376 labelComplet=label+prefixe
378 try : valeur=eval(valeur,{})
380 if tr(label) in DicoEnumCasEn.keys():
381 try : valeur=DicoEnumCasEn[tr(label)][valeur]
383 self.dictSimp[labelComplet]=valeur
385 if len(valeurs)==2 : return
386 for prefixe in ('_K_And_Epsilon','_Tracers'):
387 labelComplet=label+prefixe
389 try : valeur=eval(valeur,{})
391 if tr(label) in DicoEnumCasEn.keys():
392 try : valeur=DicoEnumCasEn[tr(label)][valeur]
394 self.dictSimp[labelComplet]=valeur
397 def Option_De_Supg(self):
398 #print "ds Option_De_Supg"
399 self.decoupeListe( self.dictSimp["Option_De_Supg"],"Option_De_Supg")
400 del self.dictSimp["Option_De_Supg"]
402 def Forme_De_La_Convection(self):
403 self.decoupeListe( self.dictSimp["Forme_De_La_Convection"],"Forme_De_La_Convection")
404 valeurs=self.dictSimp["Forme_De_La_Convection"]
405 del self.dictSimp["Forme_De_La_Convection"]
406 self.dictSimp['Convection_De_U_Et_V']=True
407 self.dictSimp['Convection_De_H']=True
408 if len(valeurs)==2 : return
409 self.dictSimp['Advection_Of_K_And_Epsilon']=True
410 self.dictSimp['Advection_Of_Tracers']=True
412 def Discretisations_En_Espace(self):
413 self.decoupeListe( self.dictSimp["Discretisations_En_Espace"],"Discretisations_En_Espace")
414 del self.dictSimp["Discretisations_En_Espace"]
417 def Liquid_Boundaries(self):
418 #print 'Liquid Boundaries'
419 texte_Boundaries="Liquid_Boundaries=( "
421 if 'Prescribed_Elevations' in self.dictSimp.keys():
422 valeurs=self.dictSimp["Prescribed_Elevations"]
423 elif 'Cotes_Imposees' in self.dictSimp.keys():
424 valeurs=self.dictSimp["Cotes_Imposees"]
427 for e in range(len(valeurs)):
428 if valeurs[e] == "" or valeurs[e] == "\n" : continue
429 if eval(valeurs[e],{})==0 : continue
430 if not premier : premier=1
431 texte_Boundaries += "_F(Type_Condition = 'Prescribed Elevations',\n"
432 texte_Boundaries += "Prescribed_Elevations = " + str(valeurs[e]) + "),\n"
434 if 'Prescribed_Flowrates' in self.dictSimp.keys():
435 valeurs=self.dictSimp["Prescribed_Flowrates"]
436 elif 'Debits_Imposes' in self.dictSimp.keys():
437 valeurs=self.dictSimp["Debits_Imposes"]
440 for e in range(len(valeurs)):
441 if valeurs[e] == "" or valeurs[e] == "\n" : continue
442 if eval(valeurs[e],{})==0 : continue
443 if not premier : premier=1
444 texte_Boundaries += "_F(Type_Condition = 'Prescribed Flowrates',\n"
445 texte_Boundaries += "Prescribed_Flowrates = " + str(valeurs[e]) + "),\n"
447 if 'Prescribed_Velocity' in self.dictSimp.keys():
448 valeurs=self.dictSimp["Prescribed_Velocity"]
449 elif 'Vitesses_Imposees' in self.dictSimp.keys():
450 valeurs=self.dictSimp["Vitesses_Imposees"]
453 for e in range(len(valeurs)):
454 if valeurs[e] == "" or valeurs[e] == "\n" : continue
455 if eval(valeurs[e],{})==0 : continue
456 if not premier : premier=1
457 texte_Boundaries += "_F(Type_Condition = 'Prescribed Velocity',\n"
458 texte_Boundaries += "Prescribed_Velocity = " + str(valeurs[e]) + "),\n"
459 if premier : texte_Boundaries +="),\n"
460 else : texte_Boundaries="" ; print "pb texte_Boundaries "
461 self.textePy += texte_Boundaries