-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pyw
421 lines (382 loc) · 23 KB
/
main.pyw
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
#!/usr/bin/python3
import os
import tkinter as tk
import traceback
import re
from tkinter import filedialog
from collections import OrderedDict as oDict
from rassadka_modules.rassadka_exceptions import ControllerException
from rassadka_modules.check_system import Checker
from rassadka_modules.controller import Controller
from rassadka_modules.tktools import TkTools
from tkinter.messagebox import showerror
class Settings(tk.Toplevel):
def __init__(self, master, items, *args, **kwargs):
super(Settings, self).__init__(master=master, *args, **kwargs)
self.wm_title("Настройки")
self.geometry("+500+300")
self.items = oDict([(item.inner_name, item) for item in items])
self.list_of_names = list(self.items.keys())
self.i = 0
self.buttons_lay = dict()
self.current = tk.StringVar(self, self.list_of_names[0])
self.select_menu = tk.OptionMenu(self, self.current, *[item.inner_name for item in items],
command=self._make_layout)
self.select_menu.grid(row=0, column=0, columnspan=2, sticky="we")
self._make_layout(self.current.get())
self.bind("<Left>", self._left)
self.bind("<Right>", self._right)
self.bind("<Up>", self._left)
self.bind("<Down>", self._right)
def _right(self, event):
if self.i < len(self.list_of_names) - 1:
self.i += 1
self.current.set(self.list_of_names[self.i])
self._make_layout(self.list_of_names[self.i])
def _left(self, event):
if self.i > 0:
self.i -= 1
self.current.set(self.list_of_names[self.i])
self._make_layout(self.list_of_names[self.i])
def _make_layout(self, name):
for widget in self.buttons_lay.values():
widget.grid_forget()
item = self.items[name]
self.buttons_lay = dict()
self.vars = dict()
self.row = 1
self._check_buttons(self.row, item)
self._radio_buttons(self.row, item)
self._scale_buttons(self.row, item)
self._make_button(self.row, item)
def _make_button(self, start_row, item):
def _commit_action():
new_settings = {setting: var.get() for setting, var in self.vars.items()}
item.refresh(new_settings)
self.buttons_lay["__label__"] = tk.Label(self, text="OK",
justify="left",
font="Courier 7")
self.buttons_lay["__label__"].grid(column=3, row=start_row)
self.buttons_lay["__commit__"] = tk.Button(self, text="Сохранить", command=_commit_action)
self.buttons_lay["__commit__"].grid(column=1, row=start_row, columnspan=2, sticky="we")
def _check_buttons(self, start_row, item):
if "CHECK" not in dir(item):
return
pos = 0
row = 0
for setting in item.CHECK:
self.vars[setting] = tk.BooleanVar(self, value=item.settings[setting])
self.buttons_lay[setting] = tk.Checkbutton(master=self, text=setting, variable=self.vars[setting])
self.buttons_lay[setting].grid(row=pos // 4 + start_row, column=pos % 4,
sticky="w")
pos += 1
row = pos // 4 + (pos % 4)
self.row = start_row + row
def _radio_buttons(self, start_row, item):
if "RADIO" not in dir(item):
return
row = 0
for setting in item.RADIO:
pos = 0
self.vars[setting["name"]] = tk.IntVar(self, value=item.settings[setting["name"]])
for state in setting["states"]:
self.buttons_lay[setting["name"]] = tk.Radiobutton(self, text=setting["name"] + " = " + str(state),
value=state,
variable=self.vars[setting["name"]])
self.buttons_lay[setting["name"]].grid(row=row + start_row, column=pos % 4,
sticky="w")
pos += 1
row = pos // 4 + (pos % 4)
self.row = start_row + row + 1
def _scale_buttons(self, start_row, item):
if "SCALE" not in dir(item):
return
row = 0
for setting in item.SCALE:
self.vars[setting["name"]] = tk.DoubleVar(self, value=item.settings[setting["name"]])
self.buttons_lay[setting["name"]] = tk.Scale(self, label=setting["name"],
variable=self.vars[setting["name"]],
from_=setting["var"][0],
to=setting["var"][1],
orient="horizontal",
resolution=0.01)
self.buttons_lay[setting["name"]].grid(row=row + start_row, column=0, sticky="w")
row += 1
self.row = start_row + row
class Compare(tk.Toplevel):
def __init__(self, master, data, *args, **kwargs):
super(Compare, self).__init__(master=master, *args, **kwargs)
self.wm_title("Сравнение")
self.geometry("+500+300")
self.info = tk.Label(self, text="Найдено несовпадений {}".format(len(data["here"])),
justify="left",
font="Courier 10")
self.info.grid(row=0, column=0, columnspan=2)
def save(where):
path = filedialog.SaveAs(self, filetypes=(('Excel files', '.xlsx'),)).show()
if not path:
return
if not path.endswith(".xlsx"):
path += ".xlsx"
data[where].to_excel(path)
self.d_there = tk.Button(self, text="Выгрузить загруженных\n(несовпадения) [%s]" % len(data["there"]),
command=lambda: save("there"))
self.d_here = tk.Button(self, text="Выгрузить сидящих\n(несовпадения) [%s]" % len(data["here"]),
command=lambda: save("here"))
self.d_nor = tk.Button(self, text="Выгрузить не сидящих `people`[%s]" % len(data["not_seated"]),
command=lambda: save("not_seated"))
self.e_nor = tk.Button(self, text="Выгрузить не сидящих `emails`[%s]" % len(data["emails_not_seated"]),
command=lambda: save("emails_not_seated"))
self.d_there.grid(row=1, column=0)
self.d_here.grid(row=1, column=1)
self.d_nor.grid(row=2, column=0, columnspan=2, sticky="we")
self.e_nor.grid(row=3, column=0, columnspan=2, sticky="we")
class DropdownFunc(tk.Toplevel):
def __init__(self, master, from_range, to_func, *args, **kwargs):
super(DropdownFunc, self).__init__(master, *args, **kwargs)
self.geometry("+500+300")
def activate(x):
to_func(x)
self.current = tk.StringVar(self, from_range[0])
self.select_menu = tk.OptionMenu(self, self.current, *from_range, command=activate)
self.select_menu.grid(row=0, column=0)
class RassadkaGUI(tk.Tk, TkTools):
__SIZE = (700, 400, 500, 250)
__GUI_GEOM = "%dx%d+%d+%d" % __SIZE
__POP_POS = "+" + str(int(__SIZE[2] + 0.5 * __SIZE[0])) + "+" + str(int(__SIZE[3] + 0.5 * __SIZE[1]))
__DEFAULT_APP_PATH = os.path.join(os.path.expanduser("~"), ".rassadka")
__CONTROLLER_FILE = os.path.join(__DEFAULT_APP_PATH, "controller.pkl")
__DEBUG_FILE = os.path.join(__DEFAULT_APP_PATH, "debug.txt")
if not os.path.exists(__DEFAULT_APP_PATH):
os.mkdir(__DEFAULT_APP_PATH)
def report_callback_exception(self, exc=None, val=None, tb=None):
if Checker.settings.get("debug_mode", False):
traceback.print_exception(exc, val, tb, file=open(self.__DEBUG_FILE, "a"))
showerror("Ошибка", message="См ошибку в файле {}".format(self.__DEBUG_FILE))
else:
showerror("Ошибка", message=str(exc) + "\n" + str(val))
def _load_controller(self):
try:
file = open(self.__CONTROLLER_FILE, "rb")
self.controller = Controller(file, from_pickle=True)
except FileNotFoundError:
filename = filedialog.Open(self,
filetypes=[("Excel file", ".xlsx")]).show()
if not filename:
self.destroy()
file = open(filename, "rb")
self.controller = Controller(file)
self.infovar.set(str(self.controller))
def task(self, ev):
self.infovar.set(str(self.controller))
self.update_idletasks()
def upd(self, event=tk.Event):
self.after(200, self.task, event)
def __init__(self):
super(RassadkaGUI, self).__init__()
self.wm_title("Рассадка v1.0")
self.__SAVE_ON_EXIT = tk.BooleanVar(self, value=True)
self.geometry(self.__GUI_GEOM)
self.label = tk.Label(self, text="Tap anywhere to refresh info",
justify="left",
font="Courier 7")
self.label.grid(row=1, column=0, sticky="w")
self.pack_propagate(1)
self.infovar = tk.StringVar(self)
try:
self._load_controller()
except ControllerException as exc:
showerror("Ошибка", message=str(type(exc)) + "\n" + str(exc))
raise
self.protocol("WM_DELETE_WINDOW", self.on_exit)
self.info = tk.Label(self, textvariable=self.infovar,
justify="left",
font="Courier 10")
self.info.grid(row=0, column=0)
menu = tk.Menu(self)
commands = oDict()
commands["Загрузки"] = oDict()
commands["Выгрузки"] = oDict()
commands["Волшебство"] = oDict()
commands["Загрузки"]["Загрузить участников"] = {"command":
self.load(parent=self, item=self.controller.load_people)}
commands["Загрузки"]["Загрузить Emails"] = {"command":
self.load(parent=self, item=self.controller.load_emails)}
commands["Загрузки"]["Добавить аудиторию"] = {"command": self.load(self, self.controller.load_auditory)}
commands["Загрузки"]["Очистить загруженных"] = {"command": self.controller.clear_buffer}
commands["Выгрузки"]["Сравнение"] = {"command": lambda: Compare(self, self.controller.comparison())}
commands["Выгрузки"]["На стенд"] = {"command": self.save(parent=self, item=self.controller.save_seated_to_excel,
for_item=dict(full=False),
initialfile="Список на стенд")}
commands["Выгрузки"]["Полная выгрузка"] = {"command":
self.save(parent=self, item=self.controller.save_seated_to_excel,
for_item=dict(full=True),
initialfile="Полная выгрузка")}
commands["Выгрузки"]["Раздатка"] = {"command": self.save(parent=self, item=self.controller.save_razdatka_to_excel,
initialfile="Раздатка")}
commands["Выгрузки"]["Карты..."] = oDict()
commands["Выгрузки"]["Карты..."]["Карта с классами"] = {"command": self.save(self,
item=self.controller.save_maps_with_data_to_excel,
for_item=dict(data="klass"),
filetypes=(('Excel files', '.xlsx'), ),
initialfile="Карты мест с klass")}
commands["Выгрузки"]["Карты..."]["Карта с местами"] = {"command": self.save(self,
item=self.controller.save_maps_with_status_to_excel,
filetypes=(('Excel files', '.xlsx'), ),
initialfile="Карты мест с seat")}
commands["Выгрузки"]["Карты..."]["Карта с произвольной информацией"] = {
"command": lambda: DropdownFunc(self, list(self.controller.required_data_cols.keys()),
lambda item: self.save(None,
item=self.controller.save_maps_with_data_to_excel,
for_item=dict(data=item),
filetypes=(('Excel files', '.xlsx'), ),
initialfile="Карты мест с %s" % item)())}
commands["Выгрузки"]["Аудитории..."] = oDict()
commands["Выгрузки"]["Аудитории..."]["в txt"] = {"command": self.save(parent=self,
item=self.controller.save_summary_to_txt,
filetypes=(("txt files", ".txt"), ),
initialfile="Статистика по аудиториям")}
commands["Выгрузки"]["Аудитории..."]["в Excel"] = {"command": self.save(parent=self,
item=self.controller.save_summary_to_excel,
filetypes=(("Excel files", ".xlsx"), ),
initialfile="Статистика по аудиториям")}
commands["Выгрузки"]["Выгрузить все в..."] = {"command": self.save_all_to_directory}
commands["Волшебство"]["Рассадить"] = {"command": self.controller.place_loaded_people}
commands["Волшебство"]["Закрепить на ключ"] = oDict()
commands["Волшебство"]["Закрепить на ключ"]["всех"] = {"command": self.key_usage(func=
self.controller.lock_seated_on_key)}
commands["Волшебство"]["Закрепить на ключ"]["по email"] = {"command": self.key_usage(func=
self.controller.lock_seated_on_key_by_email)}
commands["Волшебство"]["Открепить от ключа"] = oDict()
commands["Волшебство"]["Открепить от ключа"]["по ключу"] = {"command": self.key_usage(func=
self.controller.unlock_seated_by_key)}
commands["Волшебство"]["Открепить от ключа"]["по email"] = {"command": self.controller.unlock_seated_by_email}
commands["Волшебство"]["Отметка о прибытии"] = {"command": self.controller.mark_arrival_by_email}
commands["Волшебство"]["Удалить..."] = oDict()
commands["Волшебство"]["Удалить..."]["по местам"] = {"command": self.controller.remove_seated_by_coords}
commands["Волшебство"]["Удалить..."]["по email"] = {"command": self.controller.remove_seated_by_email}
commands["Волшебство"]["Удалить..."]["всех"] = {"command": self.controller.clean_seated}
commands["Волшебство"]["Опасно"] = oDict()
commands["Волшебство"]["Опасно"]["Обновить"] = oDict()
commands["Волшебство"]["Опасно"]["Обновить"]["по email"] = {"command":
self.yes_no(lambda event: self.controller.update_seated_by_email(True),
lambda event: self.controller.update_seated_by_email(False),
label="Игнорировать ли блокировку?")}
commands["Волшебство"]["Опасно"]["Обновить"]["по местам"] = {"command":
self.yes_no(lambda event: self.controller.update_seated_by_coords(True),
lambda event: self.controller.update_seated_by_coords(False),
label="Игнорировать ли блокировку?")}
commands["Волшебство"]["Опасно"]["Очень опасно"] = oDict()
commands["Волшебство"]["Опасно"]["Очень опасно"]["Удалить загрузочный файл"] = {
"command": lambda: self.__SAVE_ON_EXIT.set(False),
"background": "red"
}
commands["Волшебство"]["Опасно"]["Очень опасно"]["Удалить аудиторию"] = {
"command": self.key_usage(self.controller.delete_auditory, label="Название аудитории"),
"background": "red"
}
self.test_button = tk.Button(text="Настройки",
command=lambda: Settings(self, [self.controller] +
sorted(list(self.controller.auds.values()))))
self.test_button.grid(column=0, columnspan=2, sticky="we")
self._create_menu(menu, commands, menuopts=dict(tearoff=0))
self.bind_all("<Button-1>", self.upd, add="+")
self.config(menu=menu)
def save_all_to_directory(self):
prefix = filedialog.askdirectory(parent=self, mustexist=False)
if not os.path.exists(prefix):
os.mkdir(prefix)
savers = [name for name in dir(self.controller) if name.startswith("save_")]
re_filename = re.compile("<(.*)>") # Имя файла без расширения будет в docstring
re_args = re.compile("\[(.*)\]") # Для доп аргументов, если они будут
for save_func in savers:
ext = ".xlsx" if save_func.endswith("excel") else ".txt"
mode = "wb" if save_func.endswith("excel") else "w"
func = getattr(self.controller, save_func)
base_filename = re_filename.search(func.__doc__).group(1)
args = re_args.search(func.__doc__)
if not base_filename:
continue
if args:
for arg in args.group(1).split(","):
arg = arg.strip()
filename = base_filename + "_" + str(arg) + ext
if re.match("^[0-9]*$|^True$|^False$", arg):
arg = eval(arg)
with open(os.path.join(prefix, filename), mode) as file:
func(file, arg)
else:
filename = base_filename + ext
with open(os.path.join(prefix, filename), mode) as file:
func(file)
def load(self, parent, item, for_item=None, **kwargs):
if not for_item:
for_item = dict()
def wrapper():
dialog = filedialog.Open(parent, filetypes=(('Excel files', '.xlsx'),), **kwargs)
filename = dialog.show()
if not filename:
return
file = open(filename, "rb")
item(file=file, **for_item)
self.upd()
return wrapper
def save(self, parent, item, filetypes=(('Excel files', '.xlsx'),), for_item=None, initialfile="output", **kwargs):
if not for_item:
for_item = dict()
def wrapper():
path = tk.filedialog.SaveAs(parent, filetypes=filetypes,
initialfile=initialfile, **kwargs).show()
if not path:
return
if not path.endswith(filetypes[0][-1]):
path = path + filetypes[0][-1]
file = open(path, "w" if path.endswith(".txt") else "wb")
item(file=file, **for_item)
self.upd()
return wrapper
def on_exit(self):
if self.__SAVE_ON_EXIT.get():
self.controller.to_pickle(open(self.__CONTROLLER_FILE, "wb"))
else:
try:
os.remove(self.__CONTROLLER_FILE)
except (NotImplementedError, FileNotFoundError):
pass
self.destroy()
def key_usage(self, func, label="Key"):
def wrapper():
pop_up = tk.Toplevel(self)
pop_up.geometry(self.__POP_POS)
tk.Label(pop_up, text=label).grid(column=0, row=0, sticky="e")
def ok(event):
user_input = inp.get()
func(user_input)
inp = tk.Entry(pop_up, width=16)
inp.bind("<Return>", ok, add="+")
inp.bind("<Return>", lambda ev: pop_up.destroy(), add="+")
inp.bind("<Return>", self.upd, add="+")
inp.grid(column=1, row=0)
button = tk.Button(pop_up, text="OK", command=pop_up.destroy)
button.bind("<Button-1>", ok, add="+")
button.bind("<Button-1>", self.upd, add="+")
button.grid(column=0, row=1, columnspan=2, sticky="we")
return wrapper
def yes_no(self, yes_event, no_event, label=""):
def wrapper():
pop_up = tk.Toplevel(self)
pop_up.geometry(self.__POP_POS)
pop_up.label = tk.Label(pop_up, text=label)
pop_up.label.grid(row=0, column=0, columnspan=2)
yes = tk.Button(pop_up, text="Да", command=pop_up.destroy)
no = tk.Button(pop_up, text="Нет", command=pop_up.destroy)
no.bind("<Button-1>", no_event, add="+")
yes.bind("<Button-1>", yes_event, add="+")
no.bind("<Button-1>", self.upd, add="+")
yes.bind("<Button-1>", self.upd, add="+")
yes.grid(row=1, column=0, sticky="we")
no.grid(row=1, column=1, sticky="we")
return wrapper
if __name__ == '__main__':
gui = RassadkaGUI()
gui.mainloop()