worker.py (2094B)
1 import sys,os 2 import time 3 from tempfile import NamedTemporaryFile 4 5 from PyQt4 import QtCore 6 7 parentdir = sys.path[0].split(os.sep)[:-1] 8 sys.path.append(os.sep.join(parentdir)) 9 from tomblib.tomb import Tomb 10 from tomblib.parser import parse_line 11 12 class TombCreateThread(QtCore.QThread): 13 line_received = QtCore.pyqtSignal(QtCore.QString) 14 error_received = QtCore.pyqtSignal(QtCore.QString) 15 def __init__(self, tombpath, size, keypath, **opts): 16 QtCore.QThread.__init__(self) 17 self.tombpath = tombpath 18 self.size = size 19 self.keypath = keypath 20 self.opts = opts 21 22 self.err_thread = TombOutputThread() 23 self.err_thread.line_received.connect(self.line_received) 24 self.err_thread.error_received.connect(self.error_received) 25 26 def run(self): 27 self.err_thread.start() 28 self.status = Tomb.create(str(self.tombpath), str(self.size), 29 self.keypath, stderr=self.err_thread.buffer, **self.opts) 30 # self.err_thread.terminate() 31 32 def get_success(self): 33 return self.status 34 35 class TombOutputThread(QtCore.QThread): 36 line_received = QtCore.pyqtSignal(QtCore.QString) 37 error_received = QtCore.pyqtSignal(QtCore.QString) 38 progressed = QtCore.pyqtSignal(int) #value in percent 39 40 def __init__(self): 41 QtCore.QThread.__init__(self) 42 self.buffer = NamedTemporaryFile() 43 44 def run(self): 45 while True: 46 where = self.buffer.tell() 47 line = self.buffer.readline() 48 if not line: 49 time.sleep(1) 50 self.buffer.seek(where) 51 else: 52 #ansi color escapes messes this up, but it'ok anyway 53 self.line_received.emit(line) 54 self.parse_line(line) 55 56 def parse_line(self, line): 57 #This could be simplified, and s/search/match, if --no-color supported 58 #see #59 59 #TODO: this should be moved to tomblib.parse 60 parsed = parse_line(line) 61 if parsed and parsed['type'] == 'error': 62 self.error_received.emit(parsed.content)