Salome HOME
modif pour MT
[tools/eficas.git] / InterfaceQT4 / eficas_go.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2017   EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 # Modules Python
23 from __future__ import absolute_import
24 from __future__ import print_function
25 try :
26    from builtins import str
27 except : pass
28
29
30 import sys,os
31 repIni=os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),".."))
32 ihmQTDir=os.path.join(repIni,"UiQT5")
33 editeurDir=os.path.join(repIni,"Editeur")
34 ihmDir=os.path.join(repIni,"InterfaceQT4")
35
36 if ihmDir not in sys.path : sys.path.append(ihmDir)
37 if ihmQTDir not in sys.path : sys.path.append(ihmQTDir)
38 if editeurDir not in sys.path :sys.path.append(editeurDir)
39
40 def getEficasSsIhm(code=None,fichier=None,ssCode=None,multi=False,langue='en',versionCode=None):
41     #print (versionCode)
42     from InterfaceQT4.qtEficasSsIhm import AppliSsIhm
43     Eficas=AppliSsIhm(code=code,salome=0,ssCode=ssCode,multi=multi,langue=langue,versionCode=versionCode)
44     from Editeur  import session
45     options=session.parse(['ssIhm','-k',code,'-v',versionCode])
46     return Eficas
47
48
49 def lanceEficas(code=None,fichier=None,ssCode=None,multi=False,langue='en'):
50     """
51         Lance l'appli EFICAS
52     """
53     # Analyse des arguments de la ligne de commande
54     try :
55       from PyQt5.QtWidgets import QApplication
56     except :
57       print('Please, set qt environment')
58       return
59
60     from Editeur  import session
61     options=session.parse(sys.argv)
62     if options.code!= None : code=options.code
63     if options.ssCode!= None : ssCode=options.ssCode
64
65     from InterfaceQT4.qtEficas import Appli
66     app = QApplication(sys.argv)
67
68     #import cProfile, pstats, StringIO
69     #pr = cProfile.Profile()
70     #pr.enable()
71
72     Eficas=Appli(code=code,salome=0,ssCode=ssCode,multi=multi,langue=langue)
73     #pr.disable()
74     #s = StringIO.StringIO()
75     #sortby = 'cumulative'
76     #ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
77     #ps.print_stats()
78     #print (s.getValue())
79
80     Eficas.show()
81
82     res=app.exec_()
83     sys.exit(res)
84
85
86 def lanceEficas_ssIhm(code=None,fichier=None,ssCode=None,version=None,debug=False,langue='en'):
87     """
88         Lance l'appli EFICAS pour trouver les noms des groupes
89     """
90     # Analyse des arguments de la ligne de commande
91     from Editeur  import session
92     options=session.parse(sys.argv)
93     if version!=None and options.cata==None : options.cata=version
94     if fichier==None : fichier=options.comm[0]
95     if code == None : code=options.code
96
97     from .qtEficas import Appli
98     Eficas=Appli(code=code,salome=0,ssCode=ssCode,ssIhm=True,langue=langue)
99
100     from .ssIhm  import QWParentSSIhm
101     parent=QWParentSSIhm(code,Eficas,version)
102
103     from . import readercata
104     if not hasattr ( Eficas, 'readercata'):
105            monreadercata  = readercata.ReaderCata( parent, Eficas )
106            Eficas.readercata=monreadercata
107
108     from .editor import JDCEditor
109     monEditeur=JDCEditor(Eficas,fichier)
110     return monEditeur
111
112 def lanceEficas_ssIhm_chercheGroupes(code=None,fichier=None,ssCode=None,version=None):
113     monEditeur=lanceEficas_ssIhm(code,fichier,ssCode,version)
114     print((monEditeur.chercheGroupes()))
115
116 def lanceEficas_ssIhm_cherche_cr(code=None,fichier=None,ssCode=None,version=None):
117     monEditeur=lanceEficas_ssIhm(code,fichier,ssCode,version)
118     print((monEditeur.jdc.cr))
119
120 def lanceEficas_ssIhm_reecrit(code=None,fichier=None,ssCode=None,version=None,ou=None,cr=False,debug=False,leger=False,langue='ang'):
121     #print 'lanceEficas_ssIhm_reecrit', fichier
122     monEditeur=lanceEficas_ssIhm(code,fichier,ssCode,version,langue=langue)
123     if ou == None : 
124        fileName=fichier.split(".")[0]+"_reecrit.comm"
125        fn=fichier.split(".")[0]+"_cr.txt"
126     else :
127        f=fichier.split(".")[0]+"_reecrit.comm"
128        f1=os.path.basename(f)
129        fn=fichier.split(".")[0]+"_cr.txt"
130        f2=os.path.basename(fn)
131        fileName=os.path.join(ou,f1)
132        fileCr=os.path.join(ou,f2)
133     debut=False
134     if debug :
135          import cProfile, pstats, StringIO
136          pr = cProfile.Profile()
137          pr.enable()
138          monEditeur.saveFileAs(fileName=fileName)
139          pr.disable()
140          s = StringIO.StringIO()
141          sortby = 'cumulative'
142          ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
143          ps.print_stats()
144          print (s.getValue())
145
146     elif not leger : monEditeur.saveFileAs(fileName=fileName)
147     else : monEditeur.saveFileLegerAs(fileName=fileName)
148     if cr:
149        f = open(fileCr, 'w')
150        f.write(str(monEditeur.jdc.report()))
151        f.close()
152
153 def lanceEficas_param(code='Adao',fichier=None,version='V0',macro='ASSIMILATION_STUDY'):
154     """
155         Lance l'appli EFICAS pour trouver les noms des groupes
156     """
157     # Analyse des arguments de la ligne de commande
158     from Editeur  import session
159     options=session.parse(sys.argv)
160
161     from .qtEficas import Appli
162     #app = QApplication(sys.argv)
163     #Eficas=Appli(code=code,ssCode=None,salome=0)
164
165     from .ssIhm  import QWParentSSIhm
166     Eficas=QWParentSSIhm(code,version)
167
168     from . import readercata
169     if not hasattr ( Eficas, 'readercata'):
170            monreadercata  = readercata.ReaderCata( parent, Eficas )
171            Eficas.readercata=monreadercata
172
173     from .editor import JDCEditor
174     monEditeur=JDCEditor(Eficas,fichier)
175     texte=loadJDC(fichier)
176     parameters=getJdcParameters(texte,macro)
177     return parameters
178
179 #def getEficasSsIhm(code='Adao',versionCode='V0'):
180 #    from .qtEficasSsIhm import AppliSsIhm
181 #    Eficas=AppliSsIhm(code=code,ssCode=None,salome=0)
182 #    return Eficas
183
184 def getJdcParameters(jdc,macro):
185     """
186     This function converts the data from the specified macro of the
187     specified jdc text to a python dictionnary whose keys are the
188     names of the data of the macro.
189     """
190     context = {}
191     source = "def args_to_dict(**kwargs): return kwargs \n"
192     source+= "%s = _F = args_to_dict          \n"%macro
193     source+= "parameters="+jdc+"                        \n"
194     source+= "context['parameters'] = parameters         \n"
195     code = compile(source, 'file.py', 'exec')
196     eval(code)
197     parameters = context['parameters']
198     return parameters
199
200 def loadJDC(filename):
201     """
202     This function loads the text from the specified JdC file. A JdC
203     file is the persistence file of Eficas (*.comm).
204     """
205     fcomm=open(filename,'r')
206     jdc = ""
207     for line in fcomm.readlines():
208         if not (line[0]=='#'):
209            jdc+="%s"%line
210
211     # Warning, we have to make sure that the jdc comes as a simple
212     # string without any extra spaces/newlines
213     return jdc.strip()
214
215 if __name__ == "__main__":
216     import sys
217     sys.path.insert(0,os.path.abspath(os.path.join(os.getcwd(),'..')))
218     lanceEficas(code=None,fichier=None,ssCode=None,multi=True)
219     
220