Salome HOME
Windows compatibility
[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 PyQt4 import *
27 from PyQt4.QtGui import *
28 from PyQt4.QtCore import *
29
30 # Import des panels
31
32 from MGCleanerViewText_ui import Ui_ViewExe
33
34 class MGCleanerMonViewText(Ui_ViewExe, QDialog):
35     """
36     Classe permettant la visualisation de texte
37     """
38     def __init__(self, parent, txt, ):
39         QDialog.__init__(self,parent)
40         self.setupUi(self)
41         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
42         #self.connect( self.PB_Ok,SIGNAL("clicked()"), self, SLOT("close()") )
43         self.connect( self.PB_Ok,SIGNAL("clicked()"), self.theClose )
44         self.connect( self.PB_Save,SIGNAL("clicked()"), self.saveFile )
45         self.PB_Save.setToolTip("Save trace in log file")
46         self.PB_Ok.setToolTip("Close view")
47         self.monExe=QProcess(self)
48
49         self.connect(self.monExe, SIGNAL("readyReadStandardOutput()"), self.readFromStdOut )
50         self.connect(self.monExe, SIGNAL("readyReadStandardError()"), self.readFromStdErr )
51       
52         # Je n arrive pas a utiliser le setEnvironment du QProcess
53         # fonctionne hors Salome mais pas dans Salome ???
54         cmds=''
55         '''
56         try :
57           LICENCE_FILE=os.environ["DISTENE_LICENCE_FILE_FOR_MGCLEANER"]
58         except:
59           LICENCE_FILE=''
60         try :
61           PATH=os.environ["DISTENE_PATH_FOR_MGCLEANER"]
62         except:
63           PATH=''
64         if LICENCE_FILE != '': 
65           cmds+='source '+LICENCE_FILE+'\n'
66         else:
67           cmds+="# $DISTENE_LICENCE_FILE_FOR_MGCLEANER NOT SET\n"
68         if PATH != '': 
69           cmds+='export PATH='+PATH+':$PATH\n'
70         else:
71           cmds+="# $DISTENE_PATH_FOR_MGCLEANER NOT SET\n"
72         #cmds+='env\n'
73         cmds+='rm -f '+self.parent().fichierOut+'\n'
74         '''
75         cmds+=txt+'\n'
76         cmds+='echo END_OF_MGCleaner\n'
77         ext=''
78         if sys.platform == "win32":
79             ext = '.bat'
80         else:
81             ext = '.sh'
82         nomFichier=tempfile.mktemp(suffix=ext,prefix="MGCleaner_")
83         f=open(nomFichier,'w')
84         f.write(cmds)
85         f.close()
86
87         maBidouille=nomFichier
88         self.monExe.start(maBidouille)
89         self.monExe.closeWriteChannel()
90         self.enregistreResultatsDone=False
91         self.show()
92
93     def saveFile(self):
94         #recuperation du nom du fichier
95         savedir=os.environ['HOME']
96         fn = QFileDialog.getSaveFileName(None, self.trUtf8("Save File"),savedir)
97         if fn.isNull() : return
98         ulfile = os.path.abspath(unicode(fn))
99         try:
100            f = open(fn, 'wb')
101            f.write(str(self.TB_Exe.toPlainText()))
102            f.close()
103         except IOError, why:
104            QMessageBox.critical(self, self.trUtf8('Save File'),
105                 self.trUtf8('The file <b>%1</b> could not be saved.<br>Reason: %2')
106                     .arg(unicode(fn)).arg(str(why)))
107
108     def readFromStdErr(self):
109         a=self.monExe.readAllStandardError()
110         self.TB_Exe.append(QString.fromUtf8(a.data(),len(a)))
111
112     def readFromStdOut(self) :
113         a=self.monExe.readAllStandardOutput()
114         aa=QString.fromUtf8(a.data(),len(a))
115         self.TB_Exe.append(aa)
116         if "END_OF_MGCleaner" in aa:
117           self.parent().enregistreResultat()
118           self.enregistreResultatsDone=True
119           #self.theClose()
120     
121     def theClose(self):
122       if not self.enregistreResultatsDone:
123         self.parent().enregistreResultat()
124         self.enregistreResultatsDone=True
125       self.close()