Salome HOME
Merge branch 'V7_dev'
[modules/smesh.git] / src / Tools / MGCleanerPlug / MGCleanerMonViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2013-2016  EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # Modules Python
22 import string,types,os,sys
23 import tempfile
24 import traceback
25
26 from qtsalome import *
27
28 # Import des panels
29
30 from MGCleanerViewText_ui import Ui_ViewExe
31
32 class MGCleanerMonViewText(Ui_ViewExe, QDialog):
33     """
34     Classe permettant la visualisation de texte
35     """
36     def __init__(self, parent, txt, ):
37         QDialog.__init__(self,parent)
38         self.setupUi(self)
39         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
40         #self.connect( self.PB_Ok,SIGNAL("clicked()"), self, SLOT("close()") )
41         self.PB_Ok.clicked.connect( self.theClose )
42         self.PB_Save.clicked.connect( self.saveFile )
43         self.PB_Save.setToolTip("Save trace in log file")
44         self.PB_Ok.setToolTip("Close view")
45         self.monExe=QProcess(self)
46
47         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
48         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
49       
50         # Je n arrive pas a utiliser le setEnvironment du QProcess
51         # fonctionne hors Salome mais pas dans Salome ???
52         cmds=''
53         '''
54         try :
55           LICENCE_FILE=os.environ["DISTENE_LICENCE_FILE_FOR_MGCLEANER"]
56         except:
57           LICENCE_FILE=''
58         try :
59           PATH=os.environ["DISTENE_PATH_FOR_MGCLEANER"]
60         except:
61           PATH=''
62         if LICENCE_FILE != '': 
63           cmds+='source '+LICENCE_FILE+'\n'
64         else:
65           cmds+="# $DISTENE_LICENCE_FILE_FOR_MGCLEANER NOT SET\n"
66         if PATH != '': 
67           cmds+='export PATH='+PATH+':$PATH\n'
68         else:
69           cmds+="# $DISTENE_PATH_FOR_MGCLEANER NOT SET\n"
70         #cmds+='env\n'
71         cmds+='rm -f '+self.parent().fichierOut+'\n'
72         '''
73         cmds+=txt+'\n'
74         cmds+='echo END_OF_MGCleaner\n'
75         ext=''
76         if sys.platform == "win32":
77             ext = '.bat'
78         else:
79             ext = '.sh'
80         nomFichier=tempfile.mktemp(suffix=ext,prefix="MGCleaner_")
81         f=open(nomFichier,'w')
82         f.write(cmds)
83         f.close()
84
85         maBidouille=nomFichier
86         self.monExe.start(maBidouille)
87         self.monExe.closeWriteChannel()
88         self.enregistreResultatsDone=False
89         self.show()
90
91     def saveFile(self):
92         #recuperation du nom du fichier
93         savedir=os.environ['HOME']
94         fn = QFileDialog.getSaveFileName(None, self.trUtf8("Save File"),savedir)
95         if fn.isNull() : return
96         ulfile = os.path.abspath(unicode(fn))
97         try:
98            f = open(fn, 'wb')
99            f.write(str(self.TB_Exe.toPlainText()))
100            f.close()
101         except IOError, why:
102            QMessageBox.critical(self, self.trUtf8('Save File'),
103                 self.trUtf8('The file <b>%1</b> could not be saved.<br>Reason: %2')
104                     .arg(unicode(fn)).arg(str(why)))
105
106     def readFromStdErr(self):
107         a=self.monExe.readAllStandardError()
108         self.TB_Exe.append(unicode(a.data()))
109
110     def readFromStdOut(self) :
111         a=self.monExe.readAllStandardOutput()
112         aa=unicode(a.data())
113         self.TB_Exe.append(aa)
114         if "END_OF_MGCleaner" in aa:
115           self.parent().enregistreResultat()
116           self.enregistreResultatsDone=True
117           #self.theClose()
118     
119     def theClose(self):
120       if not self.enregistreResultatsDone:
121         self.parent().enregistreResultat()
122         self.enregistreResultatsDone=True
123       self.close()