-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
489 lines (397 loc) · 15.4 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
########################
# Auto Attendance #
# By Badgeminer2 #
# a open sorce project #
########################
#greifed: 1 time
###################
# ver = version #
# stud = students #
# upd = update #
# H = here #
# NH = not here #
###################
#imports
import tkinter as tk
import tkinter.ttk as ttk
import json, sys
import colorama
from termcolor import colored
import configparser
import logging
import coloredlogs
import vercheck as updr
from tkinter.simpledialog import askstring
#from ctypes import windll
import platform
import gc
import verboselogs
import time
#import gs
import argparse
import traceback
import threading
import atexit
import sync as sy
#inits
colorama.init(autoreset=True)
upd = False
argv = sys.argv[1:]
config = configparser.ConfigParser()
config.read('cfgs.ini')
verboselogs.install()
logging.basicConfig(format="%(asctime)s [%(levelname)s]: %(message)s", level=1,filename=config['logging']['file'])
coloredlogs.install(level=5,fmt="%(asctime)s [%(levelname)s]: %(message)s",filename=config['logging']['file'])
logging.debug("[main.py]starting system")
ver = config['DEFAULT']['version']
logging.log(15,"[main.py]using version v"+ver)
updr.chunks = int(config["DEFAULT"]['downloadChunkSize'])
updr.branch = config["UPDATE"]['verBranch']
#if config["sheets"].getboolean('enabled'):
# gs.init(config["sheets"]["id"])
#cmdline args
logging.log(5,"[main.py]loading command-line arguments")
clas = "usrs"
parser = argparse.ArgumentParser()
parser.add_argument("--useclass",
default="usrs",
help="the class to use")
args = parser.parse_args()
clas = args.useclass
logging.log(25,f"[main.py]using class {clas}")
clases = []
for i in json.load(open("usrs.json",encoding="utf-8")).keys():
clases.append(i)
#init students
try:
stud = json.load(open("usrs.json",encoding="utf-8"))[clas]
except BaseException as e:
logging.critical(f"[main.py]Invalid class:{clas}")
logging.critical(f"[main.py]{traceback.format_exc()}")
logging.error("[main.py]shuting down...")
logging.log(5,"[main.py]cleaned up "+str(gc.collect())+" objects")
logging.error("[main.py]crashed with exit code -5")
sys.exit(-5)
studLs = list(stud.values())
studLs.sort()
studs = dict.fromkeys(studLs, False)
logging.debug("[main.py]loaded class")
#cofigs
logging.log(5,"[main.py]reading configs")
dcs = int(config['DEFAULT']['downloadChunkSize'])
max = int(config['ATTENDANCE']['maxStud'])
sync = config['sheets'].getboolean('enabled')
safetoclose = not config['DEFAULT'].getboolean('closeprot')
cpsw = config['DEFAULT']['closepsw']
logging.debug("[main.py] configs read")
logging.log(15,"[main.py]checking for updates")
#update checking
if config['UPDATE'].getboolean('checkForUpd'):
upd =updr.check(config["UPDATE"]["verBranch"],ver)
#custom title bar
# title bar colors
TITLE_FOREGROUND = "white"
TITLE_BACKGROUND = "#2c2c2c"
TITLE_BACKGROUND_HOVER = "green"
BUTTON_FOREGROUND = "white"
BUTTON_BACKGROUND = TITLE_BACKGROUND
BUTTON_FOREGROUND_HOVER = BUTTON_FOREGROUND
BUTTON_min_FOREGROUND_HOVER = "white"
BUTTON_min_BACKGROUND_HOVER = 'blue'
BUTTON_BACKGROUND_HOVER = 'red'
# window colors
WINDOW_BACKGROUND = "white"
WINDOW_FOREGROUND = "black"
def close(*args,**kwargs):
if safetoclose:
root.destroy()
else:
psw = askstring("enter the pasword to close", "enter the pasword to close auto attendance")
if psw == cpsw:
root.destroy()
else:
tk.messagebox.showerror("invalid psw","the psw entered is invalid")
logging.warning("[main.py]invalid close pasword")
class MyButton(tk.Button):
def __init__(self, master, text='x', command=None,afg=BUTTON_FOREGROUND_HOVER,abg=BUTTON_BACKGROUND_HOVER, **kwargs):
super().__init__(master, bd=0, font="bold", padx=5, pady=2,
fg=BUTTON_FOREGROUND,
bg=BUTTON_BACKGROUND,
activebackground=abg,
activeforeground=afg,
highlightthickness=0,
text=text,
command=command)
self.bind('<Enter>', self.on_enter)
self.bind('<Leave>', self.on_leave)
def on_enter(self, event):
self['bg'] = BUTTON_BACKGROUND_HOVER
def on_leave(self, event):
self['bg'] = BUTTON_BACKGROUND
class MyTitleBar(tk.Frame):
def __init__(self, master, *args, **kwargs):
super().__init__(master, relief='raised', bd=1,
bg=TITLE_BACKGROUND,
highlightcolor=TITLE_BACKGROUND,
highlightthickness=0)
self.columnconfigure(4, weight=3)
self.title_label = tk.Label(self,
bg=TITLE_BACKGROUND,
fg=TITLE_FOREGROUND)
self.title_txt = "Auto Attendance V"+ver
if upd:
self.title_txt = self.title_txt +"⎇"
elif not config['UPDATE'].getboolean('checkForUpd'):
self.title_txt = self.title_txt +"⌁"
self.set_title(self.title_txt)
self.close_button = MyButton(self, text='x', command=close)
#self.minimize_button = MyButton(self, text='-', command=self.on_minimize,afg=BUTTON_min_FOREGROUND_HOVER,abg=BUTTON_min_BACKGROUND_HOVER)
self.other_button = MyButton(self, text='?', command=self.on_other)
self.edit_btn = MyButton(self,"🖉",self.on_other)
self.grid(column=0, row=0, sticky='ew',columnspan=4)
self.title_label.grid(column=0, row=0,columnspan=4)
self.close_button.grid(column=7, row=0,sticky='ew')
self.edit_btn.grid(column=6, row=0,sticky='w')
self.other_button.grid(column=5, row=0,sticky="w")
self.bind("<ButtonPress-1>", self.on_press)
self.bind("<ButtonRelease-1>", self.on_release)
self.bind("<B1-Motion>", self.on_move)
def set_title(self, title):
self.title = title
self.title_label['text'] = title
def on_press(self, event):
self.xwin = event.x
self.ywin = event.y
self.set_title(self.title_txt+" - ... I'm moving! ...")
self['bg'] = 'green'
self.title_label['bg'] = TITLE_BACKGROUND_HOVER
def on_release(self, event):
self.set_title(self.title_txt)
self['bg'] = TITLE_BACKGROUND
self.title_label['bg'] = TITLE_BACKGROUND
def on_move(self, event):
x = event.x_root - self.xwin
y = event.y_root - self.ywin
self.master.geometry(f'+{x}+{y}')
def on_minimize(self):
print('TODO: minimize')
def on_other(self):
print('TODO: other')
#define the main window
class window(tk.Frame):
#setup window
def __init__(self, master):
super().__init__(master)
self.style = ttk.Style().configure("TNotebook", foreground="white",background='#3d3d3d')
#configure grid rows and columns
self.rowconfigure(0, weight=2)
self.rowconfigure(1, weight=1)
self.rowconfigure(2, weight=2)
self.rowconfigure(3, weight=2)
self.rowconfigure(6, weight=5)
self.columnconfigure(1, weight=1)
#add tabs
self.tabs = ttk.Notebook()
self.tabs.grid(column=0,columnspan=6,row=1,sticky="ew")
self.tbz = {}
#self.tabs.add(self.tb1,text="test")
for c in clases:
if c == "usrs":
continue
self.tbz[c] = ttk.Frame(self.tabs)
self.tabs.add(self.tbz[c],text=c)
s = json.load(open("usrs.json",encoding="utf-8"))[c]
#if there is a update add the update btn
if upd:
self.upd = tk.Button(text="Update",
command=updr.upd,
activebackground="red",
background="red")
self.upd.grid(column=0, row=3, columnspan=4)
#setup entry box
self.entrythingy = tk.Entry(foreground="white",background='#3d3d3d')
# tk vars
self.contents = tk.StringVar()
#setup input box
self.contents.set("")
self.entrythingy["textvariable"] = self.contents
self.entrythingy.bind('<Key-Return>', self.print_contents)
#setup listboxes
self.notHere = tk.Listbox(foreground="white",background='#3d3d3d')
self.Here = tk.Listbox(foreground="white",background='#3d3d3d')
#self.notHere = tk.Treeview()
#self.Here = tk.Treeview()
#setup scroll bars
self.scroll = tk.Scrollbar()
self.scrollH = tk.Scrollbar()
#setup labels
self.lableNH = tk.Label(text="Not Here",foreground="white",background='#3d3d3d')
self.lableH = tk.Label(text="Here",foreground="white",background='#3d3d3d')
#setup btns
#self.resetB = tk.Button(text="reset", command=self.reset, activeforeground="red")
#self.syncb = tk.Button(text="sync", command=self.sync, activeforeground="blue")
self.resetB = tk.Button(text="reset", command=self.reset,activeforeground="red",foreground="white",background='#3d3d3d',activebackground='#3d3d3d')
self.syncb = tk.Button(text="sync", command=self.sync,foreground="white",activeforeground="blue",background='#3d3d3d',activebackground='#3d3d3d')
#add reset button to grid
self.resetB.grid(column=2, row=3)
#add sync
self.syncb.grid(column=0, row=3)
#add entry box to grid
self.entrythingy.grid(column=0, row=2, columnspan=4)
#add studs to lists
I = 0
for i in studLs:
self.notHere.insert(I, i)
I += 1
#add labels to grid
self.lableNH.grid(column=0, row=4)
self.lableH.grid(column=2, row=4)
#add scroll bars to grid
self.scroll.grid(column=1, row=5, sticky="ns")
self.scrollH.grid(column=3, row=5, sticky="ns")
#add lists to grid
self.notHere.grid(column=0, row=5)
self.Here.grid(column=2, row=5)
#other
if not sync:
self.syncb['state'] = tk.DISABLED
#event bindings
self.notHere.config(yscrollcommand=self.scroll.set)
self.scroll.config(command=self.notHere.yview)
self.Here.config(yscrollcommand=self.scrollH.set)
self.scrollH.config(command=self.Here.yview)
self.Here.bind('<Double-1>', self.move_st_nH)
self.notHere.bind('<Double-1>', self.move_st_H)
self.tabs.bind('<<NotebookTabChanged>>',self.tab)
#######bindings#######
#move ppl to not here
def move_st_nH(self, event):
try:
entrys = self.Here.get(0, max)
place = self.Here.get(self.Here.curselection())
logging.log(15,f"[main.py]<⎌>{place} Here -> NotHere")
self.Here.delete(entrys.index(place))
self.notHere.insert(studLs.index(place), place)
studs[place] = False
except tk.TclError as e:
logging.error(f"[main.py]<⎌>usrMoveErr: {str(e)}")
#move ppl to here
def move_st_H(self, event):
try:
entrys = self.notHere.get(0, max)
place = self.notHere.get(self.notHere.curselection())
logging.log(15,f"[main.py]<⎌>{place} NotHere -> Here")
self.notHere.delete(entrys.index(place))
self.Here.insert(studLs.index(place), place)
studs[place] = True
except tk.TclError as e:
logging.error(f"[main.py]<⎌>usrMoveErr: {str(e)}")
#scan and move
#runs on <enter> pressed in input box
def print_contents(self, event):
entrys = self.notHere.get(0, max)
#get if student is valid
if self.contents.get() in stud.keys():
try:
self.notHere.delete(entrys.index(stud[self.contents.get()]))
self.Here.insert(studLs.index(stud[self.contents.get()]),
stud[self.contents.get()])
studs[stud[self.contents.get()]] = True
logging.log(15,f"[main.py]<⎌>{self.contents.get()}:{stud[self.contents.get()]} NotHere -> Here")
except Exception as e:
logging.warning(f"[main.py]<⎌>problem with moving student: {str(e)}")
else:
logging.warning(f"[main.py]<⎌>Not found:{self.contents.get()}")
self.contents.set("")
#reset btn
#moves all students back to not here
def reset(self):
self.Here.delete(0, max)
self.notHere.delete(0, max)
logging.log(15,f"[main.py]<⎌> Here -> NotHere")
I = 0
for i in studLs:
logging.log(5,f"[main.py]<⎌>moving {str(i)} to id_{str(I)}")
self.notHere.insert(I, i)
I += 1
studs[i] = False
def sync(self):
clas = self.tabs.tab(self.tabs.select(), "text")
t = threading.Thread(None,sy.sync,args=(clas,studs))
t.run()
#sy.sync(clas,studs)
#if sync:
# gs.build_sheet(studs)
def tab(self,event):
global studs,stud,studLs
if safetoclose:
pass
else:
psw = askstring("enter the pasword", "enter the pasword")
if psw == cpsw:
pass
else:
tk.messagebox.showerror("invalid psw","the psw entered is invalid")
logging.warning("[main.py]invalid pasword")
return None
clas = self.tabs.tab(self.tabs.select(), "text")
self.syncb["state"] = sy.sb(clas)
try:
stud = json.load(open("usrs.json",encoding="utf-8"))[clas]
if "38302008" in stud.keys():
stud["38302008"] = stud["38302008"]+"⌁"
except BaseException as e:
tb = traceback.format_exc()
logging.critical(f"[main.py]<⎘>Invalid class:{clas}")
logging.critical(f"[main.py]{tb}")
logging.error("[main.py]shuting down...")
logging.log(5,"[main.py]cleaned up "+str(gc.collect())+" objects")
logging.error("[main.py]crashed with exit code -15")
import report
report.report(tb)
sys.exit(-15)
studLs = list(stud.values())
studLs.sort()
studs = dict.fromkeys(studLs, False)
self.reset()
logging.log(15,"[main.py]<⎘> swaped classes")
@atexit.register
def onquit():
try:
logging.info("[main.py]shuting down...")
logging.log(5,"[main.py]cleaned up "+str(gc.collect())+" objects")
logging.log(35,"[main.py]done")
except:
pass
logging.log(15,"[main.py] loading window")
#run the app
root = tk.Tk()
logging.log(5,"[main.py] root init")
logging.log(5,"[main.py] root configs")
root.overrideredirect(True)
root.attributes("-topmost", True)
logging.log(5,"[main.py] title bar init")
title_bar = MyTitleBar(root)
logging.log(5,"[main.py] init window size")
if platform.system() == "Windows":
root.geometry('285x290')
else:
root.geometry('355x290')
root.configure(background='#3d3d3d')
logging.log(5,"[main.py] contents init")
myapp = window(root)
myapp.master.title(f"Auto Attendance V{ver}")
try:
logging.log(5,"[main.py] run main loop")
myapp.mainloop()
except BaseException as e:
try:
time.sleep(0.5)
root.overrideredirect(False)
tb = traceback.format_exc()
logging.fatal(str(e))
logging.critical(f"[main.py]{tb}")
import report
report.report(tb)
except:
pass