-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtasks.py
92 lines (78 loc) · 2.42 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import threading
import subprocess
import socket
import queue
from qrcode import QRCode
from abc import ABC, abstractmethod
class Task(ABC):
@abstractmethod
def run(self):
"""
This method is called when the task is started.
"""
pass
class TaskQueue(threading.Thread):
def __init__(self, task_queue: queue.Queue[Task]):
"""
Initializes the task queue.
"""
threading.Thread.__init__(self)
self.task_queue = task_queue
def run(self):
"""
Runs the task queue.
"""
while True:
next_task = self.task_queue.get()
next_task.run()
self.task_queue.task_done()
class SpawnPythonServerTask(Task):
def __init__(self, port, dir_, lock, current_ports):
"""
Initializes the task of spawning a python server..
"""
self.port = port
self.dir_ = dir_
self.lock = lock
self.current_ports = current_ports
def run(self):
"""
Runs the task of spawning a python server.
"""
self.lock.acquire()
self.current_ports.append({"port": self.port, "dir": self.dir_})
self.lock.release()
subprocess.Popen(
"python3 -m http.server {} --directory {}".format(self.port, self.dir_).split(" "))
class CreateQRCodeTask(Task):
def __init__(self, text, file_name):
"""
Initializes the task of creating a qr code.
"""
self.text = text
self.file_name = file_name
def run(self):
"""
Runs the task of creating a qr code.
"""
qrcode = QRCode()
qrcode.add_data(self.text)
qrcode.print_tty()
class CompleteTask(Task):
def __init__(self, filepath: str, port: int, lock: threading.Lock, task_queue: queue.Queue[Task], current_ports: list):
"""
Initializes the complete task.
"""
self.task_queue = task_queue
self.filepath = filepath
self.port = port
self.lock = lock
self.current_ports = current_ports
def run(self):
"""
Runs the complete task.
"""
self.task_queue.put(SpawnPythonServerTask(self.port, self.filepath, self.lock, self.current_ports))
ip = socket.gethostbyname(socket.gethostname())
self.task_queue.put(CreateQRCodeTask(
"http://{}:{}".format(ip, self.port), f"{self.port}-qrcode.png"))