1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#/*##########################################################################
# Copyright (C) 2004-2013 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# This toolkit is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# PyMca is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# PyMca; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# PyMca follows the dual licensing model of Riverbank's PyQt and cannot be
# used as a free plugin for a non-free program.
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license
# is a problem for you.
#############################################################################*/
import sys
import os
import subprocess
import time
from PyMca import PyMcaQt as qt
class SubprocessLogWidget(qt.QWidget):
def __init__(self, parent=None, args=None):
qt.QWidget.__init__(self, parent)
self.setWindowTitle("Subprocess Log Widget")
self.mainLayout = qt.QVBoxLayout(self)
self._p = None
self.__timer = qt.QTimer()
self._args = args
self.connect(self.__timer, qt.SIGNAL("timeout()"), self._timerSlot)
self.logWidget = qt.QTextEdit(self)
self.logWidget.setReadOnly(True)
self.mainLayout.addWidget(self.logWidget)
def setSubprocessArgs(self, args):
self._args = args
def start(self, args=None, timing=0.1):
if args is None:
if self._args is None:
raise ValueError("Subprocess command not defined")
else:
self._args = args
else:
self._args = args
self._startTimer(timing=timing)
def stop(self):
if self.isSubprocessRunning():
#print("MESSAGE TO KILL PROCESS")
#print("HOW TO KILL IT IN A GOOD WAY?")
self._p.kill()
def isSubprocessRunning(self):
running = False
if self._p is not None:
if self._p.poll() is None:
running = True
return running
def _startTimer(self, timing=0.1):
if self._args is None:
raise ValueError("Subprocess command not defined")
self._p = subprocess.Popen(self._args,
bufsize=0,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
ddict = {}
ddict["subprocess"] = self._p
ddict["event"] = "ProcessStarted"
self.emit(qt.SIGNAL("SubprocessLogWidgetSignal"), ddict)
self.__timer.start(timing)
def _timerSlot(self):
ddict = {}
ddict["subprocess"] = self._p
if self._p.poll() is None:
# process did not finish yet
line = self._p.stdout.readline()
if len(line) > 1:
self.logWidget.append(line[:-1])
qt.qApp.processEvents()
ddict["event"] = "ProcessRunning"
else:
self.__timer.stop()
returnCode = self._p.returncode
ddict['event'] = "ProcessFinished"
ddict['code'] = returnCode
ddict["message"] = []
if returnCode == 0:
line = self._p.stdout.readline()
while len(line) > 1:
self.logWidget.append(line[:-1])
line = self._p.stdout.readline()
else:
line = self._p.stderr.readline()
while len(line) > 1:
ddict["message"].append(line)
self.logWidget.append(line[:-1])
line = self._p.stderr.readline()
self._p = None
self.emit(qt.SIGNAL("SubprocessLogWidgetSignal"), ddict)
def clear(self):
self.logWidget.clear()
def append(self, text):
self.logWidget.append(text)
def closeEvent(self, event):
if self._p is not None:
try:
self.stop()
except:
# this may happen if the process finished in the mean time
pass
qt.QWidget.closeEvent(self, event)
|