-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
87 lines (64 loc) · 1.93 KB
/
handler.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
import json
import sys
from PyQt5.QtWidgets import QApplication
from gui.whichvalue import WhichValueDialog
BASE = {
"measurements": []
}
class Handler:
FILE = "data.json"
def __init__(self):
pass
def handle(self, color, size):
pass
class DetectHandler(Handler):
def __init__(self, color, size):
super().__init__()
self.size = size
self.color = color
self.dialog = WhichValueDialog(self.color, self.size)
self.dialog.event = self.got_amount
self.dialog.exec_()
def create_empty_file(self):
with open(self.FILE, "w") as file:
file.write(json.dumps(BASE))
file.close()
return True
def get_data(self):
try:
with open(self.FILE) as file:
data = json.load(file)
file.close()
print(data)
return data
except FileNotFoundError:
return None
def save_data(self, data):
try:
with open(self.FILE, "w") as file:
file.write(json.dumps(data))
file.close()
print(data)
return True
except FileNotFoundError:
return None
def got_amount(self, amount):
self.dialog.close()
if amount != 0:
data = self.get_data()
if data is None:
self.create_empty_file()
data = self.get_data()
new_record = {
"rgb": self.color,
"size": self.size,
"amount": amount
}
data["measurements"].append(new_record)
if self.save_data(data):
print("[DETECT HANDLER] New record saved: ", new_record)
else:
print("[DETECT HANDLER] No coin.")
if __name__ == '__main__':
app = QApplication(sys.argv)
handler = DetectHandler((139, 0, 0), 100)