Salome HOME
23288: [CEA 1626] Meshgems v2.3
[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       
57         cmds = ''
58         ext = ''
59         if sys.platform == "win32":
60             cmds += 'delete %s\n' % self.parent().fichierOut
61         else:
62             cmds += '#!/bin/bash\n'
63             cmds += 'pwd\n'
64             #cmds += 'which mg-surfopt.exe\n'
65             cmds += 'echo "DISTENE_LICENSE_FILE="$DISTENE_LICENSE_FILE\n'
66             cmds += 'echo "DLIM8VAR="$DLIM8VAR\n'
67             cmds += 'rm -f %s\n' % self.parent().fichierOut
68             ext = '.bash'
69
70         cmds += 'echo %s\n' % txt #to see what is compute command
71         cmds += txt+'\n'
72         cmds += 'echo "END_OF_MGSurfOpt"\n'
73         
74         nomFichier = os.path.splitext(self.parent().fichierOut)[0] + ext
75         with open(nomFichier, 'w') as f:
76           f.write(cmds)
77         self.make_executable(nomFichier)
78         
79         if verbose: print("INFO: MGSurfOpt launch script file: %s" % nomFichier)
80         
81         self.monExe.start(nomFichier)
82         self.monExe.closeWriteChannel()
83         self.enregistreResultatsDone=False
84         self.show()
85
86     def make_executable(self, path):
87         mode = os.stat(path).st_mode
88         mode |= (mode & 0o444) >> 2    # copy R bits to X
89         os.chmod(path, mode)
90
91     def saveFile(self):
92         #recuperation du nom du fichier
93         savedir=os.environ['HOME']
94         fn = QFileDialog.getSaveFileName(None,"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, 'Save File',
103                 'The file <b>%1</b> could not be saved.<br>Reason: %2'%(unicode(fn), str(why)))
104
105     def readFromStdErr(self):
106         a=self.monExe.readAllStandardError()
107         self.TB_Exe.append(unicode(a.data().encode()))
108
109     def readFromStdOut(self) :
110         a=self.monExe.readAllStandardOutput()
111         aa=unicode(a.data(),len(a))
112         self.TB_Exe.append(aa)
113         if "END_OF_MGSurfOpt" in aa:
114           self.parent().enregistreResultat()
115           self.enregistreResultatsDone=True
116           #self.theClose()
117     
118     def theClose(self):
119       if not self.enregistreResultatsDone:
120         self.parent().enregistreResultat()
121         self.enregistreResultatsDone=True
122       self.close()