worker.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. import sys, traceback, time
  3. from PySide2 import QtCore
  4. class Worker(QtCore.QRunnable):
  5. """Worker thread for running background tasks."""
  6. def __init__(self, fn, *args, **kwargs):
  7. super(Worker, self).__init__()
  8. # Store constructor arguments (re-used for processing)
  9. self.fn = fn
  10. self.args = args
  11. self.kwargs = kwargs
  12. self.signals = WorkerSignals()
  13. self.kwargs['progress_callback'] = self.signals.progress
  14. @QtCore.Slot()
  15. def run(self):
  16. try:
  17. result = self.fn(
  18. *self.args, **self.kwargs,
  19. )
  20. except:
  21. traceback.print_exc()
  22. exctype, value = sys.exc_info()[:2]
  23. self.signals.error.emit((exctype, value, traceback.format_exc()))
  24. else:
  25. self.signals.result.emit(result)
  26. finally:
  27. self.signals.finished.emit()
  28. class WorkerSignals(QtCore.QObject):
  29. finished = QtCore.Signal()
  30. error = QtCore.Signal(tuple)
  31. result = QtCore.Signal(object)
  32. progress = QtCore.Signal(tuple)
  33. class PlugsWorker(QtCore.QObject):
  34. def __init__(self, parent):
  35. super().__init__()
  36. self.parent = parent
  37. self.pool = QtCore.QThreadPool()
  38. self.stopped = False
  39. self.plugs = self.parent.get_plugs()
  40. def stop(self):
  41. self.stopped = True
  42. self.pool.waitForDone()
  43. def progress(self, state):
  44. state, plug = state
  45. if state:
  46. self.parent.updateProgress()
  47. self.parent.updateText(
  48. f'Plug «{plug["name"]}» connected!',
  49. 'Continuing'
  50. )
  51. else:
  52. header = f'Missing plug «{plug["name"]}», please open the terminal and run:'
  53. if plug['provider'] is None:
  54. desc = f'$ snap connect ozwadmin:{plug["name"]}'
  55. else:
  56. desc = f'$ snap connect ozwadmin:{plug["name"]} {plug["provider"]}:{plug["name"]}'
  57. self.parent.updateText(header, desc)
  58. def error(self, err):
  59. self.stop()
  60. self.parent.updateText("Error", str(err))
  61. self.parent.updateExitCode(256)
  62. return
  63. def completed(self):
  64. self.parent.updateText('Plugs connected', "Continuing launch")
  65. self.parent.updateFinised()
  66. return
  67. def run(self):
  68. worker = Worker(fn=self.test)
  69. self.pool.start(worker)
  70. worker.signals.progress.connect(self.progress)
  71. worker.signals.finished.connect(self.completed)
  72. return
  73. def test(self, progress_callback):
  74. for plug in self.plugs:
  75. if self.stopped:
  76. break
  77. connected = False
  78. while not connected and not self.stopped:
  79. connected = self.parent.app.get_env().is_connected(plug['name'], plug['provider'])
  80. progress_callback.emit((connected, plug))
  81. if not connected:
  82. self.parent.updateExitCode(255)
  83. time.sleep(0.5)
  84. return