Commit 259fa5a7 authored by Thanassis Tsiodras's avatar Thanassis Tsiodras
Browse files
parents f25c9f59 dd437829
#!/usr/bin/env python #!/usr/bin/env python
import sys import sys
import PySide
import time import time
import signal import signal
from collections import deque
import PySide
from PySide.QtCore import QThread, Signal, QObject from PySide.QtCore import QThread, Signal, QObject
from PySide.QtGui import (QApplication, from PySide.QtGui import (QApplication,
...@@ -12,46 +13,77 @@ from PySide.QtGui import (QApplication, ...@@ -12,46 +13,77 @@ from PySide.QtGui import (QApplication,
QProgressDialog) QProgressDialog)
class MyThread(QThread, QObject): class MyThread(QThread, QObject):
signal = Signal(str) ''' Thread waiting for data on stdin and sending signals to the prgress
progress = Signal(int) bar in case something came in.
quit = Signal() Text can be formatted:
* if the line starts with a number, it will use the value (range 0..100)
to update the progress bar, and it will display the text above the bar.
* if the line starts with @ERROR@ the process will stop and all the
history will be dispayed in a log dialog.
* in all other cases, the line is appended to the log
text = Signal(str)
progress = Signal(int)
end = Signal()
error = Signal()
log = deque()
force_quit = False
def run(self): def run(self):
value = 10 value = 10
while True: while True:
# read from stdin without any buffering # read from stdin without any buffering
if self.force_quit:
line = sys.stdin.readline() line = sys.stdin.readline()
if len(line) == 0: if len(line) == 0:
print ("Bye") print("Bye")
self.quit.emit() self.end.emit()
elif line[0] == 'q':
print ("Quit")
return return
else: else:
split = line.split()
try: try:
split = line.split() # check for format "NUMBER text"
possible_val = split[0] possible_val = split[0]
value = int(possible_val) value = int(possible_val)
if 100 < value < 0:
raise ValueError
text = ' '.join(split[1:]) text = ' '.join(split[1:])
self.progress.emit(value) self.progress.emit(value)
if value == 100:
except (ValueError, IndexError): except (ValueError, IndexError):
text = line text = line
self.signal.emit(text) self.log.append(text)
time.sleep(0.1) try:
if split[0] == '@ERROR@':
except IndexError:
def run_gui(): def run_gui():
app = QApplication(sys.argv) app = QApplication(sys.argv)
thread = MyThread() thread = MyThread()
progress = QProgressDialog() progress = QProgressDialog()
progress.setValue(0) progress.setValue(0)
thread.signal.connect(progress.setLabelText) progress.setAutoClose(True)
thread.progress.connect(progress.setValue) thread.text.connect (progress.setLabelText)
thread.start() thread.end.connect (progress.cancel)
progress.exec_() thread.progress.connect (progress.setValue)
if progress.wasCanceled:
print 'Cancel was pressed'
thread.force_quit = True
print "end"
def main(): def main():
signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment