Tomb/src/gui/qt/worker.py

65 lines
2.1 KiB
Python
Raw Normal View History

import sys,os
import time
import re
from tempfile import NamedTemporaryFile
from PyQt4 import QtCore
parentdir = sys.path[0].split(os.sep)[:-1]
sys.path.append(os.sep.join(parentdir))
from tomblib.tomb import Tomb
class TombCreateThread(QtCore.QThread):
line_received = QtCore.pyqtSignal(QtCore.QString)
error_received = QtCore.pyqtSignal(QtCore.QString)
def __init__(self, tombpath, size, keypath, **opts):
QtCore.QThread.__init__(self)
self.tombpath = tombpath
self.size = size
self.keypath = keypath
self.opts = opts
self.err_thread = TombOutputThread()
self.err_thread.line_received.connect(self.line_received)
self.err_thread.error_received.connect(self.error_received)
def run(self):
self.err_thread.start()
#Tomb.tombexec = '/home/davide/coding/projects/tomb/src/tomb'
self.status = Tomb.create(str(self.tombpath), str(self.size),
self.keypath, stderr=self.err_thread.buffer, **self.opts)
# self.err_thread.terminate()
def get_success(self):
return self.status
class TombOutputThread(QtCore.QThread):
line_received = QtCore.pyqtSignal(QtCore.QString)
error_received = QtCore.pyqtSignal(QtCore.QString)
progressed = QtCore.pyqtSignal(int) #value in percent
def __init__(self):
QtCore.QThread.__init__(self)
self.buffer = NamedTemporaryFile()
def run(self):
while True:
where = self.buffer.tell()
line = self.buffer.readline()
if not line:
time.sleep(1)
self.buffer.seek(where)
else:
#ansi color escapes messes this up, but it'ok anyway
self.line_received.emit(line)
self.parse_line(line)
def parse_line(self, line):
#This could be simplified, and s/search/match, if --no-color supported
#see #59
#TODO: this should be moved to tomblib.parse
err_regex = re.compile(r'\[!\][^ ]* +(.+)$')
match = err_regex.search(line)
if match:
self.error_received.emit(match.group(1))