Salome HOME
sauvegarde du 13/04
[tools/eficas.git] / convert / convert_TELEMAC.py
1 # Copyright (C) 2007-2013   EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 import re, string
20 from Extensions.i18n import tr
21 from Accas.A_BLOC import BLOC
22 from Accas import *
23
24                                                                                         
25 from convert_python import PythonParser
26
27 pattern_comment_slash   = re.compile(r"^\s*/")
28 pattern_eta   = re.compile(r".*&ETA.*")
29 pattern_fin   = re.compile(r".*&FIN.*")
30 pattern_blanc = re.compile(r"^ *$")
31 pattern_OUI   = re.compile(r"^ *OUI *")
32 pattern_oui   = re.compile(r"^ *oui *")
33 pattern_NON   = re.compile(r"^ *NON *")
34 pattern_non   = re.compile(r"^ *non *")
35 pattern_vide  = re.compile(r"^ *$")
36
37 from aideAuxConvertisseurs import DicoEficasToCas, ListeSupprimeCasToEficas
38 from aideAuxConvertisseurs import ListeCalculCasToEficas, DicoAvecMajuscules
39 from enumDicoTelemac2      import DicoEnumCasEn
40
41 from Extensions import localisation
42
43 from determine import monEnvQT5
44
45
46
47 def entryPoint():
48    """
49    Return a dictionary containing the description needed to load the plugin
50    """
51    return {
52           'name' : 'TELEMAC',
53           'factory' : TELEMACParser
54           }
55
56 class TELEMACParser(PythonParser):
57    """
58    This converter works like PythonParser, except that it also initializes all
59    model variables to None in order to avoid Python syntax errors when loading
60    a file with a different or inexistent definition of variables.
61    """
62
63    def convert(self, outformat, appli=None):
64       self.dicoInverseFrancais=appli.readercata.dicoInverseFrancais
65       self.dicoAnglaisFrancais=appli.readercata.dicoAnglaisFrancais
66       self.dicoFrancaisAnglais=appli.readercata.dicoFrancaisAnglais
67       self.dicoMC=appli.readercata.dicoMC
68       self.Ordre_Des_Commandes=appli.readercata.Ordre_Des_Commandes
69    
70
71       #print self.dicoInverseFrancais
72       #text = PythonParser.convert(self, outformat, appli)
73       
74       text=""
75       l_lignes = string.split(self.text,'\n')
76       self.dictSimp={}
77       for ligne in l_lignes :
78           if pattern_comment_slash.match(ligne) : continue
79           if pattern_eta.match(ligne) : continue
80           if pattern_fin.match(ligne) : continue
81           if pattern_blanc.match(ligne) : continue
82           ligne=re.sub('\t',' ',ligne)
83           ligne=re.sub("'",' ',ligne)
84           ligne=re.sub(":",'=',ligne)
85           if ligne.count('=') != 1 :
86               print "pb avec la ligne " , ligne
87               continue 
88
89           motsInLigne=string.split(ligne,' ')
90           listeMotsSimp=()
91           simp=""
92           for mot in motsInLigne:
93               if mot == ""   : continue
94               if mot == "="  :
95                  simp=simp[0:-1]
96                  while simp[-1] == " " : simp=simp[0:-1]
97                  if simp.find('-') > 0 : simp=self.redecoupeSimp(simp)
98                  break
99
100               mot=mot.replace('_','__')
101               simp=simp+mot[0].upper() +mot[1:].lower()+'_'
102           valeur=ligne.split('=')[1]
103           self.dictSimp[simp]=valeur
104
105       
106       
107       #print dictSimp
108       #print self.dicoInverseFrancais
109
110       dicoParMC={}
111       #print ListeCalculCasToEficas
112
113       if 'Titre' not in self.dictSimp.keys():
114           import os
115           self.dictSimp['Titre']=os.path.basename(self.filename)
116       
117       for simp in self.dictSimp.keys():
118           if simp in ListeSupprimeCasToEficas: continue
119           if simp in TELEMACParser.__dict__.keys() : apply(TELEMACParser.__dict__[simp],(self,))
120
121       for simp in self.dictSimp.keys():
122           if simp not in self.dicoInverseFrancais.keys() : 
123              print "************"
124              print "pb avec ", simp,'------'
125              print "************"
126              continue
127           listeGenea=self.dicoInverseFrancais[simp]
128           listeGeneaReverse=[]
129           for (u,v) in listeGenea : 
130               if isinstance(v,BLOC): continue
131               listeGeneaReverse.append(u)
132           listeGeneaReverse.reverse()
133           dicoTravail=dicoParMC
134           i=0
135           #print (listeGeneaReverse[0:-1])
136           while i < len(listeGeneaReverse[0:-1]) : 
137             mot=listeGeneaReverse[i]
138             i=i+1
139             if mot not in dicoTravail.keys(): dicoTravail[mot]={}
140             dicoTravail=dicoTravail[mot]
141           dicoTravail[simp]=self.dictSimp[simp]
142         
143       self.textePy=""
144       #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
145       #print dicoParMC
146       #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
147       listeMC=self.tri(dicoParMC.keys())
148       for k in listeMC :
149           print "----------- traitement de " , k
150           self.textePy += self.dicoFrancaisAnglais[k] + "("
151           self.traiteMC(dicoParMC[k])
152           self.textePy += ");\n"
153           print "----------- " 
154            
155               
156       print self.textePy
157       return self.textePy 
158
159    def traiteMC(self,dico) :
160        for k in dico.keys() :
161            valeur= dico[k]
162            if k not in self.dicoMC.keys() : kA=self.dicoFrancaisAnglais[k] 
163            else : kA=k
164            obj=self.dicoMC[kA]
165            if isinstance(obj,FACT):   self.generFACT(obj,kA,valeur)
166            elif isinstance(obj,BLOC): self.generBLOC(obj,kA,valeur)
167            elif isinstance(obj,SIMP): self.generSIMP(obj,kA,valeur)
168            else : print "%%%%%%%%%%%\n", "pb generation pour", k, obj, "\n%%%%%%%%%%%"
169
170            #print "_____________"
171
172    def generFACT(self,obj,nom,valeur):
173        if nom in TELEMACParser.__dict__.keys() : 
174           apply(TELEMACParser.__dict__[nom],(self,))
175           return
176        self.textePy +=  nom + "=_F( "
177        self.traiteMC(valeur)
178        self.textePy += '),\n'
179
180
181    def generBLOC(self,obj,nom,valeur):
182        print "BLOC "
183        print nom
184
185    def generSIMP(self,obj,nom,valeur):
186        if nom in ("Prescribed_Flowrates", "Prescribed_Velocities", "Prescribed_Elevations" ): return
187        if obj.max==1 : 
188           if 'TXM' in obj.type :
189               valeur=str(valeur)
190               while valeur[-1] == " " : valeur=valeur[0:-1]
191               while valeur[0]  == " " : valeur=valeur[1:]
192               valeur=valeur[0].upper()+valeur[1:].lower()
193               valeur=tr(valeur)
194           try    : valeur=eval(valeur,{})
195           except : pass
196           if nom in DicoEnumCasEn.keys(): 
197              try    : valeur=DicoEnumCasEn[nom][valeur]
198              except : pass
199           if 'Fichier' in obj.type or 'TXM' in obj.type or 'Repertoire' in obj.type :
200               valeur=str(valeur)
201               while valeur[-1] == " " : valeur=valeur[0:-1]
202               while valeur[0]  == " " : valeur=valeur[1:]
203               self.textePy += nom + "= '" + str(valeur) +"' ,"
204               return
205           if bool in obj.type :
206             if pattern_OUI.match(valeur) or  pattern_oui.match(valeur) : self.textePy += nom + "= True,"
207             if pattern_NON.match(valeur) or  pattern_non.match(valeur) : self.textePy += nom + "= False,"
208             return
209           self.textePy += nom + "=" + str(valeur) +","
210        else :
211           if pattern_vide.match(valeur) : return
212           while valeur[-1] == " " : valeur=valeur[0:-1]
213           while valeur[0]  == " " : valeur=valeur[1:]
214
215           if   ";" in valeur : valeur=valeur.split(';')
216           elif "," in valeur : valeur=valeur.split(',')
217
218           if valeur == None : return
219           newVal=[]
220           for v in valeur :
221             try :    v==eval(v,{})
222             except : pass
223             if nom in DicoEnumCasEn.keys():
224                try    : v=DicoEnumCasEn[nom][v]
225                except : pass
226             newVal.append(v)
227           self.textePy += nom + "=" + str(newVal) +","
228           
229
230
231
232    def tri(self, listeIn):
233       if len(listeIn) == 1 : return listeIn
234       if self.Ordre_Des_Commandes == None : return listeIn
235       #print self.Ordre_Des_Commandes
236       listeOut=[listeIn[0],]
237       for kF in listeIn[1:]:
238           k=str(self.dicoFrancaisAnglais[kF])
239           ordreK=self.Ordre_Des_Commandes.index(k)
240           i=0
241           while i < len(listeOut):
242              ordreI=self.Ordre_Des_Commandes.index(self.dicoFrancaisAnglais[listeOut[i]])
243              if ordreK < ordreI : break
244              i=i+1
245           listeOut.insert(i,kF)
246       return listeOut
247
248    def Processeurs_Paralleles(self):
249       #YOANN
250       if self.dictSimp["Processeurs_Paralleles"] == 0 : del  self.dictSimp["Processeurs_Paralleles"]
251       else : self.dictSimp["Parallel_Computation"]="Parallel"
252  
253       
254    def Option_De_Supg(self):
255        print "ds Option_De_Supg"
256        return
257
258    def Forme_De_La_Convection(self):
259        print "ds Forme_De_La_Convection"
260        return
261
262    def redecoupeSimp(self,simp): 
263       # replace('-','_')  uniquement dans les identifiants
264       while simp.find('-') > 0 : 
265         ind=simp.find('-')
266         if ind==len(simp)-1 : break
267         simp=simp[0:ind]+'_'+simp[ind+1].upper()+simp[ind+2:]
268       return simp
269
270
271
272    def Liquid_Boundaries(self):
273        texte_Boundaries="Liquid_Boundaries=( "
274        premier=0
275        if 'Prescribed_Elevations' in self.dictSimp.keys(): 
276            valeurs=self.dictSimp["Prescribed_Elevations"].split(";")
277        elif 'Cotes_Imposees' in self.dictSimp.keys(): 
278            valeurs=self.dictSimp["Cotes_Imposees"].split(";")
279        else : valeurs=()
280        for e in range(len(valeurs)):
281           if valeurs[e] == "" or valeurs[e] == "\n" : continue
282           if eval(valeurs[e],{})==0 : continue
283           if not premier : premier=1
284           texte_Boundaries += "_F(Type_Condition = 'Prescribed Elevations',\n"
285           texte_Boundaries += "Prescribed_Elevations = " + str(valeurs[e]) + "),\n"
286                
287        if 'Prescribed_Flowrates' in self.dictSimp.keys(): 
288           valeurs=self.dictSimp["Prescribed_Flowrates"].split(";")
289        elif 'Debits_Imposes' in self.dictSimp.keys(): 
290           valeurs=self.dictSimp["Debits_Imposes"].split(";")
291        else : valeurs=()
292        for e in range(len(valeurs)):
293           if valeurs[e] == "" or valeurs[e] == "\n" : continue
294           if eval(valeurs[e],{})==0 : continue
295           if not premier : premier=1
296           texte_Boundaries += "_F(Type_Condition = 'Prescribed Flowrates',\n"
297           texte_Boundaries += "Prescribed_Flowrates = " + str(valeurs[e]) + "),\n"
298                
299        if 'Prescribed_Velocity' in self.dictSimp.keys(): 
300            valeurs=self.dictSimp["Prescribed_Velocity"].split(";")
301        elif 'Vitesses_Imposees' in self.dictSimp.keys(): 
302            valeurs=self.dictSimp["Vitesses_Imposees"].split(";")
303        else : valeurs=()
304        for e in range(len(valeurs)):
305           if valeurs[e] == "" or valeurs[e] == "\n" : continue
306           if eval(valeurs[e],{})==0 : continue
307           if not premier : premier=1
308           texte_Boundaries += "_F(Type_Condition = 'Prescribed Velocity',\n"
309           texte_Boundaries += "Prescribed_Velocity = " + str(valeurs[e]) + "),\n"
310        if premier :  texte_Boundaries +="),\n"
311        else : texte_Boundaries="" ; print "pb texte_Boundaries "
312        self.textePy += texte_Boundaries
313