Browse Source

Fix PEP 8 errors

master
Piotr Orzechowski 2 years ago
parent
commit
099fc1e9cc
1 changed files with 35 additions and 57 deletions
  1. 35
    57
      src/qsummer.py

+ 35
- 57
src/qsummer.py View File

@@ -64,32 +64,32 @@ from PySide.QtUiTools import QUiLoader

ROOT_DIR = abspath(dirname(__file__))


class ChecksumWorker(QObject):
fractionCalculated = QtCore.Signal(int)
calculationFinished = QtCore.Signal(str)
calculationCancelled = QtCore.Signal()
calculationException = QtCore.Signal(object)
def __init__(self, file_name, method):
QObject.__init__(self)
self._fileName = file_name
self._method = method
self._mutex = QMutex()
self._stopped = False


@QtCore.Slot()
def run(self):
try:
with open(self._fileName, 'rb') as file:
fileSize = path.getsize(self._fileName)
chunkSize = fileSize // 99
bytesRead = file.read(chunkSize)
totalBytesRead = len(bytesRead)
while bytesRead:
self._mutex.lock()
if self._stopped:
@@ -97,19 +97,18 @@ class ChecksumWorker(QObject):
self.calculationCancelled.emit()
return
self._mutex.unlock()
self._method.update(bytesRead)
bytesRead = file.read(chunkSize)
totalBytesRead += len(bytesRead)
self.fractionCalculated.emit(totalBytesRead / fileSize * 100)
return self.calculationFinished.emit(self._method.hexdigest())
except Exception as e:
self.calculationException.emit(e)


def stop(self):
self._mutex.lock()
self._stopped = True
@@ -120,52 +119,48 @@ class MainController(QObject):

def __init__(self, fileName=''):
QObject.__init__(self)
self._thread = QThread()
self._worker = None
self._fileName = fileName
loader = QUiLoader()
self.loadMainWindow(loader)
self.loadAboutDialog(loader)
self.loadCreditsDialog(loader)
self.setupUi()
self.setupConnections()


def loadMainWindow(self, loader):
self._window = loader.load(join(ROOT_DIR, 'ui/main_window.ui'))
self._window.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint)
self._fileChooser = self._window.findChild(QPushButton, 'fileChooser')
self._comboBox = self._window.findChild(QComboBox, 'comboBox')
self._lineEdit = self._window.findChild(QLineEdit, 'lineEdit')
self._progressBar = self._window.findChild(QProgressBar, 'progressBar')
self._verifyCancelButton = self._window.findChild(QPushButton, 'verifyCancelButton')
self._statusBar = QLabel()
self._window.findChild(QStatusBar, 'statusBar').addPermanentWidget(self._statusBar, 1)
self._statusBar.setAlignment(Qt.AlignHCenter)
self._newAction = self._window.findChild(QAction, 'actionNew')
self._quitAction = self._window.findChild(QAction, 'actionQuit')
self._aboutAction = self._window.findChild(QAction, 'actionAbout')
if len(self._fileName):
self._fileChooser.setText(basename(self._fileName))
self._verifyCancelButton.setEnabled(True)


def loadAboutDialog(self, loader):
self._aboutDialog = loader.load(join(ROOT_DIR, 'ui/about_dialog.ui'))


def loadCreditsDialog(self, loader):
self._creditsDialog = loader.load(join(ROOT_DIR, 'ui/credits_dialog.ui'))


def setupUi(self):
self._comboBox.addItem('MD5')
self._comboBox.addItem('SHA-1')
@@ -175,7 +170,6 @@ class MainController(QObject):
self._comboBox.addItem('SHA-512')
self._comboBox.setCurrentIndex(3)


def setupConnections(self):
self._newAction.activated.connect(self.reset)
self._fileChooser.clicked.connect(self.showFileChooserDialog)
@@ -184,11 +178,9 @@ class MainController(QObject):
button = self._aboutDialog.findChild(QPushButton, 'creditsButton')
button.clicked.connect(self.showCreditsDialog)


def show(self):
self._window.show()


def reset(self):
self._verifyCancelButton.setEnabled(False)
self._fileChooser.setText(self.tr('(None)'))
@@ -196,61 +188,54 @@ class MainController(QObject):
self._progressBar.setValue(0)
self._statusBar.setText('')


def showFileChooserDialog(self):
self._fileName, _ = QtGui.QFileDialog.getOpenFileName()
if len(self._fileName):
self._fileChooser.setText(basename(self._fileName))
self._verifyCancelButton.setEnabled(True)


def showAboutDialog(self):
self._aboutDialog.show()


def showCreditsDialog(self):
self._creditsDialog.show()


def startVerification(self):
self.setCancelAction()
self._progressBar.setValue(0)
self._fileChooser.setEnabled(False)
self._comboBox.setEnabled(False)
self._lineEdit.setEnabled(False)
self._newAction.setEnabled(False)
self._statusBar.setText('')
self._startThread()

self._startThread()

def _startThread(self):
self._worker = ChecksumWorker(self._fileName, self.getAlgorithm())
self._worker.moveToThread(self._thread)
self._thread.started.connect(self._worker.run)
self._worker.calculationFinished.connect(self.onCalculationFinished)
self._worker.fractionCalculated.connect(self.onFractionCalculated)
self._worker.calculationCancelled.connect(self.onCalculationCancelled)
self._worker.calculationException.connect(self.onCalculationException)
self._thread.start()

self._thread.start()

def cancelVerification(self):
self.setVerifyAction()
self._worker.stop()
self._thread.quit()
self._worker = None
self._fileChooser.setEnabled(True)
self._comboBox.setEnabled(True)
self._lineEdit.setEnabled(True)
self._newAction.setEnabled(True)


def getAlgorithm(self):
options = {
0: md5(),
@@ -262,71 +247,64 @@ class MainController(QObject):
}
return options[self._comboBox.currentIndex()]


@QtCore.Slot(int)
def onFractionCalculated(self, fraction):
self._progressBar.setValue(fraction)


@QtCore.Slot(str)
def onCalculationFinished(self, checksum):
self._thread.quit()
self._worker = None
self.setVerifyAction()
if checksum == self._lineEdit.text():
self._statusBar.setStyleSheet('QLabel { color: darkgreen; }')
self._statusBar.setText(self.tr('Checksums are equal'))
else:
self._statusBar.setStyleSheet('QLabel { color: red; }')
self._statusBar.setText(self.tr('Checksums ARE NOT equal'))
self._progressBar.setValue(100)
self._fileChooser.setEnabled(True)
self._comboBox.setEnabled(True)
self._lineEdit.setEnabled(True)
self._newAction.setEnabled(True)


@QtCore.Slot()
def onCalculationCancelled(self):
self._progressBar.setValue(0)


@QtCore.Slot(object)
def onCalculationException(self, exception):
self._thread.quit()
self._worker = None
self._statusBar.setStyleSheet('QLabel { color: red; }')
self._statusBar.setText(str(exception))
self.setVerifyAction()
self._progressBar.setValue(100)
self._fileChooser.setEnabled(True)
self._comboBox.setEnabled(True)
self._lineEdit.setEnabled(True)
self._newAction.setEnabled(True)


def setVerifyAction(self):
self._verifyCancelButton.clicked.disconnect(self.cancelVerification)
self._verifyCancelButton.clicked.connect(self.startVerification)
self._verifyCancelButton.setText(self.tr('&Verify'))
self._verifyCancelButton.setIcon(QIcon.fromTheme('media-playback-start'))


def setCancelAction(self):
self._verifyCancelButton.clicked.disconnect(self.startVerification)
self._verifyCancelButton.clicked.connect(self.cancelVerification)
self._verifyCancelButton.setText(self.tr('&Cancel'))
self._verifyCancelButton.setIcon(QIcon.fromTheme('process-stop'))

self._verifyCancelButton.setIcon(QIcon.fromTheme('process-stop'))

def quit(self, *args):
if self._worker != None:
if self._worker is not None:
self._worker.stop()
self._thread.quit()
self._thread.wait()
@@ -341,12 +319,12 @@ if __name__ == '__main__':

translator = QtCore.QTranslator()
translator.load(join(ROOT_DIR, 'l10n/' + locale.getlocale()[0] + '.qm'))
app = QApplication(sys.argv)
app.installTranslator(translator)
controller = MainController(args.file)
app.aboutToQuit.connect(controller.quit)
controller.show()
sys.exit(app.exec_())

Loading…
Cancel
Save