Salome HOME
Update copyrights
[modules/smesh.git] / src / Tools / MGCleanerPlug / MGCleanerMonViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2013-2019  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 os
23 import sys
24 import string
25 import types
26 import tempfile
27 import traceback
28 import pprint as PP #pretty print
29
30 from qtsalome import *
31
32 # Import des panels
33
34 from MGCleanerViewText_ui import Ui_ViewExe
35
36 verbose = True
37
38 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
39 if force != None:
40     os.environ["DISTENE_LICENSE_FILE"] = force
41     os.environ["DLIM8VAR"] = "NOTHING"
42
43 class MGCleanerMonViewText(Ui_ViewExe, QDialog):
44     """
45     Classe permettant la visualisation de texte
46     """
47     def __init__(self, parent, txt):
48         QDialog.__init__(self,parent)
49         self.setupUi(self)
50         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
51         self.PB_Ok.clicked.connect( self.theClose )
52         # Button OK is disabled until computation is finished
53         self.PB_Ok.setEnabled(False)
54         # Button cancel allows to kill the computation
55         # It is disabled when the computation is finished
56         self.PB_Cancel.clicked.connect( self.cancelComputation )
57         self.PB_Cancel.setToolTip("Cancel computation")
58         self.PB_Save.clicked.connect( self.saveFile )
59         self.PB_Save.setToolTip("Save trace in log file")
60         self.PB_Ok.setToolTip("Close view")
61         self.monExe=QProcess(self)
62
63         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
64         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
65         self.monExe.finished.connect( self.finished )
66         self.monExe.errorOccurred.connect( self.errorOccured )
67
68         if os.path.exists(self.parent().fichierOut):
69             os.remove(self.parent().fichierOut)
70         
71         self.monExe.start(txt)
72         self.monExe.closeWriteChannel()
73         self.hasBeenCanceled = False
74         self.anErrorOccured = False
75         self.show()
76
77     def make_executable(self, path):
78         mode = os.stat(path).st_mode
79         mode |= (mode & 0o444) >> 2    # copy R bits to X
80         os.chmod(path, mode)
81
82     def saveFile(self):
83         #recuperation du nom du fichier
84         savedir=os.environ['HOME']
85         fn, mask = QFileDialog.getSaveFileName(None,"Save File",savedir)
86         if not fn: return
87         ulfile = os.path.abspath(str(fn))
88         try:
89             f = open(fn, 'wb')
90             f.write(self.TB_Exe.toPlainText().encode("utf-8"))
91             f.close()
92         except IOError as why:
93             QMessageBox.critical(self, 'Save File',
94                  'The file <b>%s</b> could not be saved.<br>Reason: %s'%(str(fn), str(why)))
95
96     def readFromStdErr(self):
97         a=self.monExe.readAllStandardError()
98         aa=a.data().decode(errors='ignore')
99         self.TB_Exe.append(aa)
100
101     def readFromStdOut(self) :
102         a=self.monExe.readAllStandardOutput()
103         aa=a.data().decode(errors='ignore')
104         self.TB_Exe.append(aa)
105
106     def finished(self):
107         self.PB_Ok.setEnabled(True)
108         self.PB_Cancel.setEnabled(False)
109         exit_code = self.monExe.exitCode()
110         if exit_code == 0 and not self.anErrorOccured:
111             self.parent().enregistreResultat()
112         elif not self.hasBeenCanceled:
113             QMessageBox.critical(self, 'Computation failed',
114                  'The computation has failed.<br>Please, check the log message.')
115         pass
116
117     def errorOccured(self):
118         # for instance if the executable is not found
119         self.anErrorOccured = True
120         self.finished()
121
122     def cancelComputation(self):
123         self.hasBeenCanceled = True
124         self.monExe.kill()
125
126     def theClose(self):
127         self.close()