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
23 from Telemac_Cata import Tuple
26 from convert_python import PythonParser
28 pattern_comment_slash = re.compile(r"^\s*/")
29 pattern_eta = re.compile(r".*&ETA.*")
30 pattern_fin = re.compile(r".*&FIN.*")
31 pattern_oui = re.compile(r"^\s*(oui|OUI|YES|yes|TRUE|VRAI)\s*$")
32 pattern_non = re.compile(r"^\s*(non|NON|NO|no|FALSE|FAUX)\*s$")
33 pattern_blanc = re.compile(r"^\s*$")
34 pattern_listeVide = re.compile(r"^\s*'\s*'\s*$")
36 pattern_ligne=re.compile(r'^\s*(?P<ident>[^=:]*)\s*[:=]\s*(?P<reste>.*)$')
38 # Attention aux listes de flottants
39 pattern_liste=re.compile(r'^\s*(?P<valeur>[+-.\w]+(\s*;\s*[+-.\w]+)+)\s*(?P<reste>.*)$')
40 pattern_flottant=re.compile(r'^\s*(?P<valeur>[+-]?((\d+(\.\d*)?)|(\.\d+))([dDeE][+-]?\d+)?)\s*(?P<reste>.*)$')
41 pattern_texteQuote = re.compile (r"^\s*(?P<valeur>'[^']+(''[^']+)*')\s*(?P<reste>.*)$")
42 pattern_texteSimple = re.compile (r"(?P<valeur>(^|\s)\s*[\w\.-]+)\s*(?P<reste>.*)$")
43 pattern_texteVide = re.compile (r"^\s*(?P<valeur>'')\s*(?P<reste>.*)$")
45 pattern_ContientDouble=re.compile (r"^.*''.*$")
48 # le pattern texte reconnait
49 #nom1 nom 2 : ou = chaine entre '
50 # avec eventuellement des quotes au milieu par exemple
51 # TITRE = 'TELEMAC 2D : GOUTTE D''EAU DANS UN BASSIN$'
52 # m.group("texte") va rendre 'TELEMAC 2D : GOUTTE D''EAU DANS UN BASSIN$'
55 #Si le code n est pas Telemac
57 from aideAuxConvertisseurs import DicoEficasToCas, ListeSupprimeCasToEficas
58 from aideAuxConvertisseurs import ListeCalculCasToEficas, DicoAvecMajuscules
59 from enumDicoTelemac import DicoEnumCasEn
63 from Extensions import localisation
65 from determine import monEnvQT5
71 Return a dictionary containing the description needed to load the plugin
75 'factory' : TELEMACParser
78 class TELEMACParser(PythonParser):
80 This converter works like PythonParser, except that it also initializes all
81 model variables to None in order to avoid Python syntax errors when loading
82 a file with a different or inexistent definition of variables.
85 def convert(self, outformat, appli=None):
86 self.dicoInverseFrancais=appli.readercata.dicoInverseFrancais
87 self.dicoAnglaisFrancais=appli.readercata.dicoAnglaisFrancais
88 self.dicoFrancaisAnglais=appli.readercata.dicoFrancaisAnglais
89 self.dicoMC=appli.readercata.dicoMC
90 self.Ordre_Des_Commandes=appli.readercata.Ordre_Des_Commandes
93 #print self.dicoInverseFrancais
94 #text = PythonParser.convert(self, outformat, appli)
97 l_lignes = string.split(self.text,'\n')
99 for ligne in l_lignes :
101 if pattern_comment_slash.match(ligne) : continue
102 if pattern_eta.match(ligne) : continue
103 if pattern_fin.match(ligne) : continue
104 if pattern_blanc.match(ligne) : continue
108 while finLigne != "" :
110 if pattern_comment_slash.match(finLigne) : finLigne=""; continue
112 m=pattern_ligne.match(finLigne)
114 print "________________________________________________"
115 print 'pb avec ****', finLigne , '**** dans ', ligne
116 print "________________________________________________"
119 simp=self.traiteIdent(m.group('ident'))
120 finLigne=m.group('reste')
122 # attention, l ordre des if est important
123 if pattern_liste.match(finLigne) :
124 m=pattern_liste.match(finLigne)
125 elif pattern_texteQuote.match(finLigne) :
126 m=pattern_texteQuote.match(finLigne)
127 elif pattern_flottant.match(finLigne) :
128 m=pattern_flottant.match(finLigne)
129 elif pattern_texteVide.match(finLigne):
130 m=pattern_texteVide.match(finLigne)
131 elif pattern_texteSimple.match(finLigne):
132 m=pattern_texteSimple.match(finLigne)
134 print "________________________________________________"
135 print 'pb avec ****', finLigne , '**** dans ', ligne
137 print "________________________________________________"
140 valeur=m.group('valeur')
141 if pattern_blanc.match(valeur) : valeur=None
143 if pattern_flottant.match(finLigne) :
144 valeur=re.sub("d","e",valeur)
145 valeur=re.sub("D","E",valeur)
147 if pattern_liste.match(finLigne):
148 valeur=valeur.split(";")
150 finLigne=m.group('reste')
154 self.dictSimp[simp]=valeur
156 if 'Title' not in self.dictSimp.keys() and 'Titre' not in self.dictSimp.keys():
158 self.dictSimp['Titre']=os.path.basename(self.filename)
161 for simp in self.dictSimp.keys():
162 #print simp, " : ", self.dictSimp[simp]
164 if simp in TELEMACParser.__dict__.keys() : apply(TELEMACParser.__dict__[simp],(self,))
166 for simp in self.dictSimp.keys():
167 if simp in ListeSupprimeCasToEficas: continue
168 if simp not in self.dicoInverseFrancais.keys() :
170 print "pb avec dans dicoInverseFrancais", simp,'------'
174 listeGenea=self.dicoInverseFrancais[simp]
176 for (u,v) in listeGenea :
177 if isinstance(v,BLOC): continue
178 listeGeneaReverse.append(u)
179 listeGeneaReverse.reverse()
180 dicoTravail=dicoParMC
182 #print (listeGeneaReverse[0:-1])
183 while i < len(listeGeneaReverse[0:-1]) :
184 mot=listeGeneaReverse[i]
186 if mot not in dicoTravail.keys(): dicoTravail[mot]={}
187 dicoTravail=dicoTravail[mot]
188 dicoTravail[simp]=self.dictSimp[simp]
191 #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
193 #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
194 listeMC=self.tri(dicoParMC.keys())
196 #print "----------- traitement de " , k
197 self.textePy += self.dicoFrancaisAnglais[k] + "("
198 self.traiteMC(dicoParMC[k])
199 self.textePy += ");\n"
200 #print "----------- "
206 #----------------------------------------
207 def redecoupeSimp(self,simp):
208 # traite les - dans les identifants python
209 # les remplace par des _
210 #----------------------------------------
211 # replace('-','_') uniquement dans les identifiants
212 while simp.find('-') > 0 :
214 if ind==len(simp)-1 : break
215 simp=simp[0:ind]+'_'+simp[ind+1].upper()+simp[ind+2:]
218 #----------------------------------------
219 def traiteIdent(self,listeIdent):
220 # Recree l identifiant Python
221 #----------------------------------------
222 while listeIdent[-1] == " " or listeIdent[-1] == '\t' : listeIdent=listeIdent[0:-1]
223 while listeIdent[0] == " " or listeIdent[0] == '\t' : listeIdent=listeIdent[1:]
225 listeIdent=re.sub("'"," ",listeIdent)
226 motsInIdent=string.split(listeIdent,' ')
228 for mot in motsInIdent:
229 if re.compile(r"^\s*$").match(mot) : continue
230 mot=mot.replace('_','__')
231 simp=simp+mot[0].upper() +mot[1:].lower()+'_'
234 while simp[-1] == " " : simp=simp[0:-1]
235 if simp.find('-') > 0 : simp=self.redecoupeSimp(simp)
240 def traiteMC(self,dico) :
241 for k in dico.keys() :
243 if k not in self.dicoMC.keys() : kA=self.dicoFrancaisAnglais[k]
246 if isinstance(obj,FACT): self.generFACT(obj,kA,valeur)
247 elif isinstance(obj,BLOC): self.generBLOC(obj,kA,valeur)
248 elif isinstance(obj,SIMP): self.generSIMP(obj,kA,valeur)
249 else : print "%%%%%%%%%%%\n", "pb generation pour", k, obj, "\n%%%%%%%%%%%"
251 #print "_____________"
253 def generFACT(self,obj,nom,valeur):
254 if nom in TELEMACParser.__dict__.keys() :
255 apply(TELEMACParser.__dict__[nom],(self,))
257 self.textePy += nom + "=_F( "
258 self.traiteMC(valeur)
259 self.textePy += '),\n'
262 def generBLOC(self,obj,nom,valeur):
266 def generSIMP(self,obj,nom,valeur):
267 if nom in ("Prescribed_Flowrates", "Prescribed_Velocities", "Prescribed_Elevations" ): return
268 print "___________________________"
272 if hasattr(obj.type[0],'ntuple') :
278 self.textePy += nom + "=" + str(lval) +","
280 if 'TXM' in obj.type :
282 if pattern_ContientDouble.match(str(valeur)):
283 valeur=re.sub("''","\'\'",str(valeur))
284 self.textePy += nom + "=" + str(valeur) +","
288 # ceinture et bretelle si les re sont correctes -)
289 while valeur[-1] == " " or valeur[-1] == '\t' : valeur=valeur[0:-1]
290 while valeur[0] == " " or valeur[0] == '\t' : valeur=valeur[1:]
295 try : valeur=eval(valeur,{})
297 if nom in DicoEnumCasEn.keys():
298 #print "est dans le dico des enum, valeur simple"
300 valeur=DicoEnumCasEn[nom][valeur]
301 self.textePy += nom + "= '" + str(valeur) +"',"
306 if obj.into != [] and obj.into != None and not('R' in obj.type) and not('I' in obj.type):
307 for possible in obj.into :
308 if possible.upper() == valeur.upper():
311 v=valeur[0].upper()+valeur[1:].lower()
313 if possible.upper() == v2.upper():
317 if 'Fichier' in obj.type or 'TXM' in obj.type or 'Repertoire' in obj.type :
319 while valeur[-1] == " " : valeur=valeur[0:-1]
320 while valeur[0] == " " : valeur=valeur[1:]
321 self.textePy += nom + "= '" + str(valeur) +"' ,"
324 if bool in obj.type :
325 if valeur == True : self.textePy += nom + "= True,"
326 elif valeur == False : self.textePy += nom + "= False,"
327 elif pattern_oui.match(valeur) : self.textePy += nom + "= True,"
328 elif pattern_non.match(valeur) : self.textePy += nom + "= False,"
329 else : self.textePy += nom + "= None,"
331 self.textePy += nom + "=" + str(valeur) +","
334 if pattern_listeVide.match(valeur) :
335 self.textePy += nom + "= None,"
337 while valeur[-1] == " " or valeur[-1]=="'" : valeur=valeur[0:-1]
338 while valeur[0] == " " or valeur[-0]=="'" : valeur=valeur[1:]
340 if ";" in valeur : valeur=valeur.split(';')
341 elif "," in valeur : valeur=valeur.split(',')
343 if valeur == None : return
348 if nom in DicoEnumCasEn.keys():
349 #print "est dans le dico des enum, valeurs multiples"
350 try : v=DicoEnumCasEn[nom][v]
353 self.textePy += nom + "=" + str(newVal) +","
357 def tri(self, listeIn):
358 if len(listeIn) == 1 : return listeIn
359 if self.Ordre_Des_Commandes == None : return listeIn
360 #print self.Ordre_Des_Commandes
361 listeOut=[listeIn[0],]
362 for kF in listeIn[1:]:
363 k=str(self.dicoFrancaisAnglais[kF])
364 ordreK=self.Ordre_Des_Commandes.index(k)
366 while i < len(listeOut):
367 ordreI=self.Ordre_Des_Commandes.index(self.dicoFrancaisAnglais[listeOut[i]])
368 if ordreK < ordreI : break
370 listeOut.insert(i,kF)
373 def Processeurs_Paralleles(self):
375 if self.dictSimp["Processeurs_Paralleles"] == 0 : del self.dictSimp["Processeurs_Paralleles"]
376 else : self.dictSimp["Parallel_Computation"]="Parallel"
378 def decoupeListe(self,valeurs,label):
379 #print "decoupeListe"
382 for prefixe in ('_U_And_V','_H'):
383 labelComplet=label+prefixe
385 try : valeur=eval(valeur,{})
387 if tr(label) in DicoEnumCasEn.keys():
388 try : valeur=DicoEnumCasEn[tr(label)][valeur]
390 self.dictSimp[labelComplet]=valeur
392 if len(valeurs)==2 : return
393 for prefixe in ('_K_And_Epsilon','_Tracers'):
394 labelComplet=label+prefixe
396 try : valeur=eval(valeur,{})
398 if tr(label) in DicoEnumCasEn.keys():
399 try : valeur=DicoEnumCasEn[tr(label)][valeur]
401 self.dictSimp[labelComplet]=valeur
404 def Option_De_Supg(self):
405 #print "ds Option_De_Supg"
406 self.decoupeListe( self.dictSimp["Option_De_Supg"],"Option_De_Supg")
407 del self.dictSimp["Option_De_Supg"]
409 def Forme_De_La_Convection(self):
410 self.decoupeListe( self.dictSimp["Forme_De_La_Convection"],"Forme_De_La_Convection")
411 valeurs=self.dictSimp["Forme_De_La_Convection"]
412 del self.dictSimp["Forme_De_La_Convection"]
413 self.dictSimp['Convection_De_U_Et_V']=True
414 self.dictSimp['Convection_De_H']=True
415 if len(valeurs)==2 : return
416 self.dictSimp['Convection_De_K_Et_Epsilon']=True
417 self.dictSimp['Convection_Des_Traceurs']=True
419 def Discretisations_En_Espace(self):
420 self.decoupeListe( self.dictSimp["Discretisations_En_Espace"],"Discretisations_En_Espace")
421 del self.dictSimp["Discretisations_En_Espace"]
423 def Date_De_L_Origine_Des_Temps (self):
424 valeurs=self.dictSimp["Date_De_L_Origine_Des_Temps"]
425 self.dictSimp['Annee']=valeurs[0]
426 self.dictSimp['Mois']=valeurs[1]
427 self.dictSimp['Jour']=valeurs[2]
428 del self.dictSimp["Date_De_L_Origine_Des_Temps"]
431 def Heure_De_L_Origine_Des_Temps (self):
432 valeurs=self.dictSimp["Heure_De_L_Origine_Des_Temps"]
433 self.dictSimp['Heure']=valeurs[0]
434 self.dictSimp['Minute']=valeurs[1]
435 self.dictSimp['Seconde']=valeurs[2]
436 del self.dictSimp["Heure_De_L_Origine_Des_Temps"]
438 def Liquid_Boundaries(self):
439 #print 'Liquid Boundaries'
440 texte_Boundaries="Liquid_Boundaries=( "
442 if 'Prescribed_Elevations' in self.dictSimp.keys():
443 valeurs=self.dictSimp["Prescribed_Elevations"]
444 elif 'Cotes_Imposees' in self.dictSimp.keys():
445 valeurs=self.dictSimp["Cotes_Imposees"]
448 for e in range(len(valeurs)):
449 if valeurs[e] == "" or valeurs[e] == "\n" : continue
450 if eval(valeurs[e],{})==0 : continue
451 if not premier : premier=1
452 texte_Boundaries += "_F(Type_Condition = 'Prescribed Elevations',\n"
453 texte_Boundaries += "Prescribed_Elevations = " + str(valeurs[e]) + "),\n"
455 if 'Prescribed_Flowrates' in self.dictSimp.keys():
456 valeurs=self.dictSimp["Prescribed_Flowrates"]
457 elif 'Debits_Imposes' in self.dictSimp.keys():
458 valeurs=self.dictSimp["Debits_Imposes"]
461 for e in range(len(valeurs)):
462 if valeurs[e] == "" or valeurs[e] == "\n" : continue
463 if eval(valeurs[e],{})==0 : continue
464 if not premier : premier=1
465 texte_Boundaries += "_F(Type_Condition = 'Prescribed Flowrates',\n"
466 texte_Boundaries += "Prescribed_Flowrates = " + str(valeurs[e]) + "),\n"
468 if 'Prescribed_Velocity' in self.dictSimp.keys():
469 valeurs=self.dictSimp["Prescribed_Velocity"]
470 elif 'Vitesses_Imposees' in self.dictSimp.keys():
471 valeurs=self.dictSimp["Vitesses_Imposees"]
474 for e in range(len(valeurs)):
475 if valeurs[e] == "" or valeurs[e] == "\n" : continue
476 if eval(valeurs[e],{})==0 : continue
477 if not premier : premier=1
478 texte_Boundaries += "_F(Type_Condition = 'Prescribed Velocity',\n"
479 texte_Boundaries += "Prescribed_Velocity = " + str(valeurs[e]) + "),\n"
480 if premier : texte_Boundaries +="),\n"
481 else : texte_Boundaries="" ; print "pb texte_Boundaries "
482 self.textePy += texte_Boundaries