Refactored QThread classes for more clarity and less code duplication

This commit is contained in:
Marta Marczykowska-Górecka 2019-04-03 20:34:01 +02:00
parent fe810c5ad0
commit 6e10daa309
No known key found for this signature in database
GPG Key ID: 9A752C30B26FD04B
2 changed files with 20 additions and 40 deletions

View File

@ -25,34 +25,33 @@ from qubesadmin import exc
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class RemoveVMThread(QtCore.QThread): class QubesThread(QtCore.QThread):
def __init__(self, vm): def __init__(self, vm):
QtCore.QThread.__init__(self) QtCore.QThread.__init__(self)
self.vm = vm self.vm = vm
self.msg = None self.msg = None
self.is_error = False self.msg_is_success = False
# pylint: disable=too-few-public-methods
class RemoveVMThread(QubesThread):
def run(self): def run(self):
try: try:
del self.vm.app.domains[self.vm.name] del self.vm.app.domains[self.vm.name]
except (exc.QubesException, KeyError) as ex: except (exc.QubesException, KeyError) as ex:
self.msg = ("Error removing qube!", str(ex)) self.msg = ("Error removing qube!", str(ex))
self.is_error = True
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class CloneVMThread(QtCore.QThread): class CloneVMThread(QubesThread):
def __init__(self, src_vm, dst_name): def __init__(self, vm, dst_name):
QtCore.QThread.__init__(self) super(CloneVMThread, self).__init__(vm)
self.src_vm = src_vm
self.dst_name = dst_name self.dst_name = dst_name
self.msg = None
self.is_error = False
def run(self): def run(self):
try: try:
self.src_vm.app.clone_vm(self.src_vm, self.dst_name) self.vm.app.clone_vm(self.vm, self.dst_name)
self.msg = ("Sucess", "The qube was cloned sucessfully.") self.msg = ("Sucess", "The qube was cloned sucessfully.")
self.msg_is_success = True
except exc.QubesException as ex: except exc.QubesException as ex:
self.msg = ("Error while cloning qube!", str(ex)) self.msg = ("Error while cloning qube!", str(ex))
self.is_error = True

View File

@ -249,29 +249,16 @@ class VmShutdownMonitor(QtCore.QObject):
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class StartVMThread(QtCore.QThread): class StartVMThread(common_threads.QubesThread):
def __init__(self, vm):
QtCore.QThread.__init__(self)
self.vm = vm
self.msg = None
self.is_error = False
def run(self): def run(self):
try: try:
self.vm.start() self.vm.start()
except exc.QubesException as ex: except exc.QubesException as ex:
self.msg = ("Error starting Qube!", str(ex)) self.msg = ("Error starting Qube!", str(ex))
self.is_error = True
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class UpdateVMThread(QtCore.QThread): class UpdateVMThread(common_threads.QubesThread):
def __init__(self, vm):
QtCore.QThread.__init__(self)
self.vm = vm
self.msg = None
self.is_error = False
def run(self): def run(self):
try: try:
if self.vm.qid == 0: if self.vm.qid == 0:
@ -301,24 +288,19 @@ class UpdateVMThread(QtCore.QThread):
user="root", wait=False) user="root", wait=False)
except (ChildProcessError, exc.QubesException) as ex: except (ChildProcessError, exc.QubesException) as ex:
self.msg = ("Error on qube update!", str(ex)) self.msg = ("Error on qube update!", str(ex))
self.is_error = True
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class RunCommandThread(QtCore.QThread): class RunCommandThread(common_threads.QubesThread):
def __init__(self, vm, command_to_run): def __init__(self, vm, command_to_run):
QtCore.QThread.__init__(self) super(RunCommandThread, self).__init__(vm)
self.vm = vm
self.command_to_run = command_to_run self.command_to_run = command_to_run
self.msg = None
self.is_error = False
def run(self): def run(self):
try: try:
self.vm.run(self.command_to_run) self.vm.run(self.command_to_run)
except (ChildProcessError, exc.QubesException) as ex: except (ChildProcessError, exc.QubesException) as ex:
self.msg = ("Error while running command!", str(ex)) self.msg = ("Error while running command!", str(ex))
self.is_error = True
class VmManagerWindow(ui_qubemanager.Ui_VmManagerWindow, QtGui.QMainWindow): class VmManagerWindow(ui_qubemanager.Ui_VmManagerWindow, QtGui.QMainWindow):
@ -521,17 +503,16 @@ class VmManagerWindow(ui_qubemanager.Ui_VmManagerWindow, QtGui.QMainWindow):
if thread.msg: if thread.msg:
(title, msg) = thread.msg (title, msg) = thread.msg
if thread.is_error: if thread.msg_is_success:
QtGui.QMessageBox.warning(
None,
self.tr(title),
self.tr(msg))
else:
QtGui.QMessageBox.information( QtGui.QMessageBox.information(
None, None,
self.tr(title), self.tr(title),
self.tr(msg)) self.tr(msg))
else:
QtGui.QMessageBox.warning(
None,
self.tr(title),
self.tr(msg))
self.threads_list.remove(thread) self.threads_list.remove(thread)
return return