Salome HOME
Fix to allow Cleaner and SurfOpt to be launched on files with special characters...
[modules/smesh.git] / src / Tools / YamsPlug / monViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-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 import os
22 import sys
23 import string
24 import types
25 import tempfile
26 import traceback
27 import pprint as PP #pretty print
28
29 from qtsalome import *
30
31 # Import des panels
32 from ViewText_ui import Ui_ViewExe
33
34 verbose = True
35
36 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
37 if force != None:
38     os.environ["DISTENE_LICENSE_FILE"] = force
39     os.environ["DLIM8VAR"] = "NOTHING"
40
41 class MonViewText(Ui_ViewExe, QDialog):
42     """
43     Classe permettant la visualisation de texte
44     """
45     def __init__(self, parent, txt):
46         QDialog.__init__(self,parent)
47         self.setupUi(self)
48         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
49         # self.PB_Ok.clicked.connect(self.close)
50         self.PB_Ok.clicked.connect( self.theClose )
51         self.PB_Save.clicked.connect( self.saveFile )
52         self.monExe=QProcess(self)
53
54         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
55         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
56         self.monExe.finished.connect( self.finished )
57
58         if os.path.exists(self.parent().fichierOut):
59             os.remove(self.parent().fichierOut)
60
61         self.monExe.start(txt)
62         self.monExe.closeWriteChannel()
63         self.enregistreResultatsDone=False
64         self.show()
65
66     def make_executable(self, path):
67         mode = os.stat(path).st_mode
68         mode |= (mode & 0o444) >> 2    # copy R bits to X
69         os.chmod(path, mode)
70
71     def saveFile(self):
72         #recuperation du nom du fichier
73         savedir=os.environ['HOME']
74         fn, mask = QFileDialog.getSaveFileName(None,"Save File",savedir)
75         if not fn: return
76         ulfile = os.path.abspath(str(fn))
77         try:
78             f = open(fn, 'wb')
79             f.write(self.TB_Exe.toPlainText().encode("utf-8"))
80             f.close()
81         except IOError as why:
82             QMessageBox.critical(self, 'Save File',
83                  'The file <b>%s</b> could not be saved.<br>Reason: %s'%(str(fn), str(why)))
84
85     def readFromStdErr(self):
86         a=self.monExe.readAllStandardError()
87         aa=a.data().decode(errors='ignore')
88         self.TB_Exe.append(aa)
89
90     def readFromStdOut(self) :
91         a=self.monExe.readAllStandardOutput()
92         aa=a.data().decode(errors='ignore')
93         self.TB_Exe.append(aa)
94
95     def finished(self):
96         self.parent().enregistreResultat()
97         self.enregistreResultatsDone=True
98
99     def theClose(self):
100         if not self.enregistreResultatsDone:
101             self.parent().enregistreResultat()
102             self.enregistreResultatsDone=True
103         self.close()