Khadafi995

Python use class as decorator for another class method

May 29th, 2021 (edited)
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. import time
  5. import random
  6. from selenium import webdriver
  7. import threading
  8. import sys
  9.  
  10. from PyQt5 import QtCore, QtWidgets
  11. from gui import Ui_MainWindow
  12.  
  13. class HTaskLib:
  14.     def __init__(self) -> None:
  15.         self._driver = ""
  16.         self._options = ""
  17.  
  18.     @HTask
  19.     def browser_start(self):
  20.         self._options = webdriver.ChromeOptions()
  21.         self._driver = webdriver.Chrome(
  22.             options=self._options)
  23.         self._driver.set_window_size(1000, 660)
  24.  
  25.     @HTask
  26.     def open(self):
  27.         self._driver.get("https://www.google.fr")
  28.  
  29.     @HTask
  30.     def print_timer(self):
  31.         for i in range(10):
  32.             self.wait_flag()
  33.             print(time.time())
  34.             # self.pause()
  35.             time.sleep(0.5)
  36.         self.done()
  37.  
  38.  
  39.  
  40. class HTask(threading.Thread):
  41.  
  42.     def __init__(self, func, *args, **kwargs):
  43.         super(HTask, self).__init__(*args, **kwargs)
  44.         self.__flag = threading.Event()  # The flag used to pause the thread
  45.         self.__flag.set()  # Set to True
  46.         self.__running = threading.Event()  # Used to stop the thread identification
  47.         self.__running.set()  # Set running to True
  48.         self._func = func
  49.         self.__done = threading.Event()
  50.  
  51.     def run(self):
  52.         while self.__running.isSet():
  53.             # return immediately when it is True, block until the internal flag is True when it is False
  54.             self.__flag.wait()
  55.             self._func(self)
  56.  
  57.     def pause(self):
  58.         print('[+] Paused')
  59.         self.clear_flag()  # Set to False to block the thread
  60.  
  61.     def resume(self):
  62.         print('[+] Resumed')
  63.         self.set_flag()  # Set to True, let the thread stop blocking
  64.  
  65.     def stop(self):
  66.         print('[+] Stoped')
  67.         self.set_flag()  # Resume the thread from the suspended state, if it is already suspended
  68.         self.clear_running()  # Set to False
  69.  
  70.     def done(self):
  71.         print('[+] Task done')
  72.         self.__flag.set()
  73.         self.__done.set()
  74.         self.__running.clear()
  75.  
  76.     def set_flag(self):
  77.         self.__flag.set()
  78.  
  79.     def clear_flag(self):
  80.         self.__flag.clear()
  81.  
  82.     def wait_flag(self):
  83.         self.__flag.wait()
  84.  
  85.     def is_flagged(self):
  86.         return self.__flag.is_set()
  87.  
  88.     def clear_running(self):
  89.         self.__running.clear()
  90.  
  91.     def wait_running(self):
  92.         self.__running.wait()
  93.  
  94.     def is_running(self):
  95.         return self.__running.is_set()
  96.  
  97.     def is_done(self):
  98.         return self.__done.is_set()
  99.  
  100.  
  101. class ApplicationWindow(QtWidgets.QMainWindow):
  102.     def __init__(self) -> None:
  103.         super(ApplicationWindow, self).__init__()
  104.  
  105.         self.ui = Ui_MainWindow()
  106.         self.ui.setupUi(self)
  107.  
  108.  
  109. def main():
  110.     _translate = QtCore.QCoreApplication.translate
  111.  
  112.     app = QtWidgets.QApplication(sys.argv)
  113.     application = ApplicationWindow()
  114.     application.show()
  115.  
  116.     def play_pause_task(task: HTask):
  117.         if task.is_flagged():
  118.             application.ui.play_pause_button.setText(
  119.                 _translate("MainWindow", "Play"))
  120.             task.pause()
  121.         else:
  122.             application.ui.play_pause_button.setText(
  123.                 _translate("MainWindow", "Pause"))
  124.             task.resume()
  125.  
  126.     def stop_task(task: HTask, app: QtWidgets.QApplication):
  127.         app.close()
  128.         task.stop()
  129.  
  130.     # TODO : If there is still time, implement shorcut keyboard for better UX
  131.  
  132.     htl = HTaskList()
  133.     task_todo = []
  134.     task_done = []
  135.     task_todo.append(htl.browser_start)
  136.     # task_todo.append(htl.print_hello)
  137.     task_todo.append(htl.open)
  138.  
  139.     for id, task in enumerate(task_todo):
  140.         print('Task ID' + f"{id+1:03}")
  141.  
  142.         application.ui.play_pause_button.clicked.connect(
  143.             lambda: play_pause_task(task))
  144.  
  145.         application.ui.stop_button.clicked.connect(
  146.             lambda: stop_task(task, application))
  147.  
  148.         task.start()
  149.         if task.is_done():
  150.             task_done.append(task_todo[task])
  151.  
  152.     sys.exit(app.exec_())
  153.  
  154.  
  155. if __name__ == '__main__':
  156.     main()
  157.  
Add Comment
Please, Sign In to add comment