taste-progress-dialog.py 2.69 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python

import sys
import time
import signal
6
from collections import deque
7

8
import PySide
9 10 11 12 13 14 15
from PySide.QtCore import QThread, Signal, QObject

from PySide.QtGui import (QApplication,
                          QMessageBox,
                          QProgressDialog)

class MyThread(QThread, QObject):
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    ''' Thread waiting for data on stdin and sending signals to the prgress
    bar in case something came in.
    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

32 33 34 35
    def run(self):
        value = 10
        while True:
            # read from stdin without any buffering
36 37
            if self.force_quit:
                return
38 39
            line = sys.stdin.readline()
            if len(line) == 0:
40 41
                print("Bye")
                self.end.emit()
42 43
                return
            else:
44
                split = line.split()
Maxime Perrotin's avatar
Maxime Perrotin committed
45
                try:
46
                    # check for format "NUMBER text"
Maxime Perrotin's avatar
Maxime Perrotin committed
47 48
                    possible_val = split[0]
                    value = int(possible_val)
49 50
                    if 100 < value < 0:
                        raise ValueError
Maxime Perrotin's avatar
Maxime Perrotin committed
51 52
                    text = ' '.join(split[1:])
                    self.progress.emit(value)
53 54 55 56
                    self.text.emit(text)
                    if value == 100:
                        self.end.emit()
                        return
Maxime Perrotin's avatar
Maxime Perrotin committed
57 58
                except (ValueError, IndexError):
                    text = line
59 60 61 62 63 64
                self.log.append(text)
                try:
                    if split[0] == '@ERROR@':
                        self.error.emit()
                except IndexError:
                    pass
65 66 67 68



def run_gui():
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    app      = QApplication(sys.argv)
    thread   = MyThread()
    progress = QProgressDialog()
    progress.setValue(0)
    progress.setAutoClose(True)

    thread.text.connect     (progress.setLabelText)
    thread.end.connect      (progress.cancel)
    thread.progress.connect (progress.setValue)
    thread.setTerminationEnabled(True)

    thread.start()
    progress.exec_()
    if progress.wasCanceled:
        print 'Cancel was pressed'
        thread.force_quit = True
    thread.wait()
    print "end"
87 88 89 90 91 92 93 94

def main():
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    run_gui()

if __name__ == '__main__':
    main()