1 # Copyright (C) 2007-2021 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
19 from __future__ import absolute_import
23 from Extensions.i18n import tr
26 #traceback.print_stack()
28 from convert.convert_python import Pythonparser
34 pattern_comment_slash = re.compile(r"^\s*/")
35 pattern_comment_slash_vide = re.compile(r"^\s*/\s*$")
36 pattern_comment_tiret = re.compile(r"^\s*/-*/*$")
37 pattern_eta = re.compile(r".*&ETA.*")
38 pattern_fin = re.compile(r".*&FIN.*")
39 pattern_oui = re.compile(r"^\s*(oui|OUI|YES|yes|TRUE|true|vrai|VRAI)\s*$")
40 pattern_non = re.compile(r"^\s*(non|NON|NO|no|FALSE|false|faux|FAUX)\s*$")
41 pattern_blanc = re.compile(r"^\s*$")
42 pattern_listeVide = re.compile(r"^\s*'\s*'\s*$")
43 pattern_commence_par_quote=re.compile(r'^\s*[\'"].*')
44 pattern_finit_par_virgule_ou_affect=re.compile(r'^.*(,|;|=|:)\s*$')
46 pattern_ligne=re.compile(r'^\s*(?P<ident>[^=:]*)\s*[:=]\s*(?P<reste>.*)$')
48 pattern_variables=re.compile(r"^\s*(?P<ident>C_VSM_PRINTOUT_SELECTION|COUPLAGE AVEC|COUPLING WITH|VARIABLES FOR GRAPHIC PRINTOUTS|VARIABLES POUR LES SORTIES GRAPHIQUES|VARIABLES TO BE PRINTED|VARIABLES A IMPRIMER|VARIABLES FOR 3D GRAPHIC PRINTOUTS|VARIABLES POUR LES SORTIES GRAPHIQUES 3D|VARIABLES POUR LES SORTIES GRAPHIQUES 2D|VARIABLES FOR 2D GRAPHIC PRINTOUTS)\s*[:=]\s*(?P<quote>[']?)(?P<valeur>[A-Za-z]+(\d*|\*)([,;][A-Za-z]+(\d*|\*))*)\s*(?P=quote)(?P<reste>.*)$")
50 # Attention aux listes de flottants
51 pattern_liste=re.compile(r'^\s*(?P<valeur>[+-.\w]+(\s*;\s*[+-.\w]+)+)\s*(?P<reste>.*)$')
52 pattern_liste_texte=re.compile(r"^\s*(?P<valeur>('.*(';\s*))+('.*'\s*)?)(?P<reste>.*)$")
53 pattern_flottant=re.compile(r'^\s*(?P<valeur>[+-]?((\d+(\.\d*)?)|(\.\d+))([dDeE][+-]?\d+)?)\s*(?P<reste>.*)$')
54 pattern_texteQuote = re.compile(r"^\s*(?P<valeur>'[^']+(''[^']+)*')\s*(?P<reste>.*)$")
55 pattern_texteSimple = re.compile(r"(?P<valeur>(^|\s)\s*[\w\.-]+)\s*(?P<reste>.*)$")
56 pattern_texteVide = re.compile(r"^\s*(?P<valeur>'')\s*(?P<reste>.*)$")
58 pattern_ContientDouble=re.compile(r"^.*''.*$")
60 # le pattern texte reconnait
61 #nom1 nom 2 : ou = chaine entre '
62 # avec eventuellement des quotes au milieu par exemple
63 # TITRE = 'TELEMAC 2D : GOUTTE D''EAU DANS UN BASSIN$'
64 # m.group("texte") va rendre 'TELEMAC 2D : GOUTTE D''EAU DANS UN BASSIN$'
67 #Si le code n est pas Telemac
69 # from enum_Telemac2d_auto import self.TelemacdicoEn
73 from Extensions import localisation
79 Return a dictionary containing the description needed to load the plugin
83 'factory' : TELEMACparser
86 class TELEMACparser(Pythonparser):
88 This converter works like Pythonparser, except that it also initializes all
89 model variables to None in order to avoid Python syntax errors when loading
90 a file with a different or inexistent definition of variables.
95 def convert(self, outformat, appliEficas=None):
98 from Accas import A_BLOC, A_FACT, A_SIMP
100 self.dicoCasToCata = appliEficas.readercata.dicoCasToCata
102 self.dicoCasToCata = {}
103 print ('pas de dicoCasToCata')
104 self.dicoInverse = appliEficas.readercata.dicoInverse
105 self.dicoMC = appliEficas.readercata.dicoMC
106 self.Ordre_Des_Commandes = appliEficas.readercata.Ordre_Des_Commandes
108 self.TelemacdicoEn = appliEficas.readercata.TelemacdicoEn
110 self.TelemacdicoEn = {}
111 print('pas de TelemacdicoEn')
113 self.DicoEnumCasFrToEnumCasEn = appliEficas.readercata.DicoEnumCasFrToEnumCasEn
115 self.DicoEnumCasFrToEnumCasEn = {}
116 print ('pas de DicoEnumCasFrToEnumCasEn')
119 if appliEficas.langue=='fr' :
120 #from enum_Telemac2d_auto import DicoEnumCasFrToEnumCasEn
121 for k in self.DicoEnumCasFrToEnumCasEn :
122 self.TelemacdicoEn[k]=self.DicoEnumCasFrToEnumCasEn[k]
127 l_lignes_texte_all = self.text.split('\n')
136 for l in l_lignes_texte_all :
137 if pattern_eta.match(l) : continue
138 if pattern_fin.match(l) : continue
139 if pattern_blanc.match(l) : continue
141 if not(pattern_comment_slash.match(l)):
142 l_lignes_texte.append(l)
144 if debut: dicoComment['debut']=texteComment
145 else : dicoComment[l]=texteComment
148 if debut : debut = False
150 if pattern_comment_slash.match(l):
151 #if pattern_comment_slash_vide.match(l) : continue
152 if pattern_comment_tiret.match(l) : continue
153 texteComment+=l.replace ('/','',1)
157 if texteComment != "" : dicoComment['fin']= texteComment
162 while (i < len(l_lignes_texte)) :
163 ligne=l_lignes_texte[i]
165 if not(pattern_finit_par_virgule_ou_affect.match(ligne)):
166 l_lignes.append(ligne)
169 while (i < len(l_lignes_texte)):
170 ligne_traitee=l_lignes_texte[i]
172 nouvelle_ligne += ligne_traitee
173 if not(pattern_finit_par_virgule_ou_affect.match(ligne_traitee)):
174 l_lignes.append(nouvelle_ligne)
178 for ligne in l_lignes :
179 if pattern_comment_slash.match(ligne) : continue
181 #if pattern_eta.match(ligne) : continue
182 #if pattern_fin.match(ligne) : continue
183 #if pattern_blanc.match(ligne) : continue
187 while finLigne != "" :
188 if pattern_comment_slash.match(finLigne) : finLigne=""; continue
190 if pattern_variables.match(finLigne) :
191 m=pattern_variables.match(finLigne)
192 simpCas=self.traiteIdent(m.group('ident'))
193 if simpCas in ["COUPLING WITH", "COUPLAGE AVEC"]:
194 if "," in m.group('valeur'):
195 valeur=m.group('valeur').split(",")
197 valeur=m.group('valeur').split(";")
199 valeur=m.group('valeur')
200 finLigne=m.group('reste')
201 self.dictSimp[simpCas]=valeur
205 m=pattern_ligne.match(finLigne)
207 #print( "________________________________________________")
208 print ('pb avec ****', finLigne , '**** dans ', ligne)
209 #print( "________________________________________________")
212 simpCas=self.traiteIdent(m.group('ident'))
214 finLigne=m.group('reste')
217 finLigne=m.group('reste')
218 # attention, l ordre des if est important
219 if pattern_liste.match(finLigne) :
220 m=pattern_liste.match(finLigne)
221 elif pattern_liste_texte.match(finLigne) :
222 m=pattern_liste_texte.match(finLigne)
223 elif pattern_texteQuote.match(finLigne) :
224 m=pattern_texteQuote.match(finLigne)
225 elif pattern_flottant.match(finLigne) :
226 m=pattern_flottant.match(finLigne)
227 elif pattern_texteVide.match(finLigne):
228 m=pattern_texteVide.match(finLigne)
229 elif pattern_texteSimple.match(finLigne):
230 m=pattern_texteSimple.match(finLigne)
232 #print ("________________________________________________")
233 print ('pb avec ****', finLigne , '**** dans ', ligne)
235 #print ("________________________________________________")
239 valeur=m.group('valeur')
240 if pattern_blanc.match(valeur) : valeur=None
242 if pattern_flottant.match(finLigne) :
243 valeur=re.sub("d","e",valeur)
244 valeur=re.sub("D","E",valeur)
246 if pattern_liste.match(finLigne) or \
247 pattern_liste_texte.match(finLigne):
248 valeur=valeur.split(";")
251 finLigne=m.group('reste')
252 self.dictSimp[simpCas]=valeur
254 if ligne in dicoComment.keys():
255 dicoCommentSimp[simpCas]=dicoComment[ligne]
257 if 'TITLE' not in self.dictSimp :
259 #self.dictSimp['TITLE']=os.path.basename(self.filename)
263 for simp in self.dictSimp:
264 if simp in TELEMACparser.__dict__ : TELEMACparser.__dict__[simp](self,)
266 for simp in self.dictSimp:
267 if simp not in self.dicoInverse :
268 #print ( "************")
269 print ("pb avec dans dicoInverse", simp,'------')
270 print("dicoInverse",sorted(self.dicoInverse.keys()))
271 #print ("************")
273 listeGenea=self.dicoInverse[simp]
275 for (u,v) in listeGenea :
276 if isinstance(v,A_BLOC.BLOC): continue
277 listeGeneaReverse.append(u)
278 listeGeneaReverse.reverse()
279 dicoTravail=dicoParMC
281 if simp in dicoCommentSimp :
282 MC=listeGeneaReverse[0]
283 if MC in dicoCommentMC : dicoCommentMC[MC]+dicoCommentSimp[simp]
284 else : dicoCommentMC[MC]=dicoCommentSimp[simp]
285 while i < len(listeGeneaReverse[0:-1]) :
286 mot=listeGeneaReverse[i]
288 if mot not in dicoTravail: dicoTravail[mot]={}
289 dicoTravail=dicoTravail[mot]
290 dicoTravail[simp]=self.dictSimp[simp]
293 listeMC=self.tri(list(dicoParMC.keys()))
295 if k in dicoCommentMC :
296 commentaire="COMMENTAIRE("+repr(dicoCommentMC[k])+")\n"
297 self.textePy+=commentaire
298 self.textePy += str(k )+ "("
299 self.traiteMC(dicoParMC[k])
300 self.textePy += ");\n"
304 #appliEficas.listeTelemac=self.dictSimp
305 appliEficas.listeTelemac={}
306 if 'debut' in dicoComment :
307 commentaire="COMMENTAIRE("+repr(dicoComment['debut'])+")\n"
308 self.textePy=commentaire+self.textePy
309 if 'fin' in dicoComment :
310 commentaire="COMMENTAIRE("+repr(dicoComment['fin'])+")\n"
311 self.textePy=self.textePy+commentaire
313 #print (self.textePy)
317 #----------------------------------------
318 def traiteIdent(self,ident):
319 # enleve les espaces de part et autre
320 # traduit du langage Telemac vers le langage Catalogue
321 #----------------------------------------
322 while ident[-1] == " " or ident[-1] == '\t' : ident=ident[0:-1]
323 while ident[0] == " " or ident[0] == '\t' : ident=ident[1:]
324 try : identCata=self.dicoCasToCata[ident]
326 print ( "---> ", "pb mot clef pour", ident)
331 def traiteMC(self,dico) :
332 from Accas import A_BLOC, A_FACT, A_SIMP
335 if k not in self.dicoMC : kA=self.dicoFrancaisAnglais[k]
338 if isinstance(obj,A_FACT.FACT): self.convertFACT(obj,kA,valeur)
339 elif isinstance(obj,A_BLOC.BLOC): self.convertBLOC(obj,kA,valeur)
340 elif isinstance(obj,A_SIMP.SIMP): self.convertSIMP(obj,kA,valeur)
341 else : print ("%%%%%%%%%%%\n", "pb conversion type pour", k, obj, "\n%%%%%%%%%%%")
344 def convertFACT(self,obj,nom,valeur):
345 # traitement LIQUID_BOUNDARIES
346 if nom in TELEMACparser.__dict__ :
347 TELEMACparser.__dict__[nom](self,)
349 self.textePy += nom + "=_F( "
350 self.traiteMC(valeur)
351 self.textePy += '),\n'
354 def convertBLOC(self,obj,nom,valeur):
355 print ("ANOMALIE _________ BLOC ")
358 def convertSIMP(self,obj,nom,valeur):
359 #print ('in convertSIMP', nom,valeur)
360 #if nom in ("PRESCRIBED_FLOWRATES", "PRESCRIBED_VELOCITIES", "PRESCRIBED_ELEVATIONS" ): return
362 if hasattr(obj.type[0],'ntuple') :
368 self.textePy += nom + "=" + str(lval) +","
370 if 'TXM' in obj.type :
372 if pattern_ContientDouble.match(str(valeur)):
373 valeur=re.sub("''","\'\'",str(valeur))
374 self.textePy += nom + "=" + str(valeur) +","
378 # ceinture et bretelle si les re sont correctes -)
379 while valeur[-1] == " " or valeur[-1] == '\t' : valeur=valeur[0:-1]
380 while valeur[0] == " " or valeur[0] == '\t' : valeur=valeur[1:]
385 try : valeur=eval(valeur,{})
388 if nom in self.TelemacdicoEn:
390 valeur=self.TelemacdicoEn[nom][valeur]
391 self.textePy += nom + "= '" + str(valeur) +"',"
396 if obj.into != [] and obj.into != None and not('R' in obj.type) and not('I' in obj.type):
397 for possible in obj.into :
399 if possible.upper() == valeur.upper():
402 v=valeur[0].upper()+valeur[1:].lower()
404 if possible.upper() == v2.upper():
409 print ("pb avec le type de ", obj.nom, obj.type, 'et la valeur ', valeur)
411 if 'Fichier' in obj.type or 'TXM' in obj.type or 'Repertoire' in obj.type or 'FichierOuRepertoire' in obj.type :
413 if valeur == "" or valeur == " " :
414 self.textePy += nom + "= '" + str(valeur) +"' ,"
416 while valeur[-1] == " " : valeur=valeur[0:-1]
417 while valeur[0] == " " : valeur=valeur[1:]
418 self.textePy += nom + "= '" + str(valeur) +"' ,"
421 if bool in obj.type :
422 if valeur == True : self.textePy += nom + "= True,"
423 elif valeur == False : self.textePy += nom + "= False,"
424 elif pattern_oui.match(valeur) : self.textePy += nom + "= True,"
425 elif pattern_non.match(valeur) : self.textePy += nom + "= False,"
426 else : self.textePy += nom + "= None,"
428 self.textePy += nom + "=" + str(valeur) +","
431 if valeur == () or valeur ==[] or pattern_listeVide.match(str(valeur)) :
432 self.textePy += nom + "= None,"
435 # les 4 lignes suivantes sont probablement inutiles
436 while valeur[-1] == " " or valeur[-1]=="'" : valeur=valeur[0:-1]
437 while valeur[0] == " " or valeur[-0]=="'" : valeur=valeur[1:]
439 if isinstance(valeur, basestring) :
440 if ";" in valeur : valeur=valeur.split(';')
441 else : valeur=valeur.split(',')
443 if len(valeur)< 2 and pattern_flottant.match(oldValeur):
444 # Attention : on attend une liste mais on a une seule valeur!
445 try : oldValeur=eval(oldValeur,{})
447 if nom in self.TelemacdicoEn :
448 v=self.TelemacdicoEn[nom][oldValeur]
449 self.textePy += nom + "= ('" + str(v) +"',),"
451 self.textePy += nom + "= (" + str(oldValeur) +",),"
454 # Cas des liste de booléen
458 if val == True or pattern_oui.match(val) or val == 'True':
459 values.append('True')
460 elif val == False or pattern_non.match(val) or val == 'False':
461 values.append('False')
463 values.append('None')
464 self.textePy += nom + "= [" + ','.join(values) + '],'
468 if valeur == None : return
473 if nom in self.TelemacdicoEn:
474 try : v=self.TelemacdicoEn[nom][v]
477 self.textePy += nom + "=" + str(newVal) +","
481 def tri(self, listeIn):
482 if len(listeIn) == 1 : return listeIn
483 if self.Ordre_Des_Commandes == None : return listeIn
484 listeOut=[listeIn[0],]
485 for k in listeIn[1:]:
486 #k=str(self.dicoFrancaisAnglais[kF])
487 ordreK=self.Ordre_Des_Commandes.index(k)
489 while i < len(listeOut):
490 #ordreI=self.Ordre_Des_Commandes.index(self.dicoFrancaisAnglais[listeOut[i]])
491 ordreI=self.Ordre_Des_Commandes.index(listeOut[i])
492 if ordreK < ordreI : break
494 #listeOut.insert(i,kF)