Salome HOME
Fix to allow Cleaner and SurfOpt to be launched on files with special characters...
[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 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.connect( self.PB_Ok,SIGNAL("clicked()"), self, SLOT("close()") )
52         self.PB_Ok.clicked.connect( self.theClose )
53         self.PB_Save.clicked.connect( self.saveFile )
54         self.PB_Save.setToolTip("Save trace in log file")
55         self.PB_Ok.setToolTip("Close view")
56         self.monExe=QProcess(self)
57
58         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
59         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
60         self.monExe.finished.connect( self.finished )
61
62         """ for test set environment
63         env = QProcessEnvironment().systemEnvironment()
64         env.insert("HELLO", "bonjour") #Add an environment variable for debug
65         self.monExe.setProcessEnvironment(env)
66         if verbose: 
67           PP.pprint([str(i) for i in sorted(self.monExe.processEnvironment().toStringList()) if 'DISTENE' in i])
68         """
69         
70         if os.path.exists(self.parent().fichierOut):
71             os.remove(self.parent().fichierOut)
72         
73         self.monExe.start(txt)
74         self.monExe.closeWriteChannel()
75         self.enregistreResultatsDone=False
76         self.show()
77
78     def make_executable(self, path):
79         mode = os.stat(path).st_mode
80         mode |= (mode & 0o444) >> 2    # copy R bits to X
81         os.chmod(path, mode)
82
83     def saveFile(self):
84         #recuperation du nom du fichier
85         savedir=os.environ['HOME']
86         fn, mask = QFileDialog.getSaveFileName(None,"Save File",savedir)
87         if not fn: return
88         ulfile = os.path.abspath(str(fn))
89         try:
90             f = open(fn, 'wb')
91             f.write(self.TB_Exe.toPlainText().encode("utf-8"))
92             f.close()
93         except IOError as why:
94             QMessageBox.critical(self, 'Save File',
95                  'The file <b>%s</b> could not be saved.<br>Reason: %s'%(str(fn), str(why)))
96
97     def readFromStdErr(self):
98         a=self.monExe.readAllStandardError()
99         aa=a.data().decode(errors='ignore')
100         self.TB_Exe.append(aa)
101
102     def readFromStdOut(self) :
103         a=self.monExe.readAllStandardOutput()
104         aa=a.data().decode(errors='ignore')
105         self.TB_Exe.append(aa)
106     
107     def finished(self):
108         self.parent().enregistreResultat()
109         self.enregistreResultatsDone=True
110     
111     def theClose(self):
112       if not self.enregistreResultatsDone:
113         self.parent().enregistreResultat()
114         self.enregistreResultatsDone=True
115       self.close()