-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.py
More file actions
1317 lines (1112 loc) · 53.9 KB
/
Copy pathindex.py
File metadata and controls
1317 lines (1112 loc) · 53.9 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import json
import sqlite3
import hashlib
import shutil
import threading
import subprocess
import csv
import time
from datetime import datetime
from tkinter import *
from tkinter import ttk, filedialog, messagebox, scrolledtext
from google.cloud import firestore
from google.oauth2 import service_account
import fitz
# ========================== CONFIGURATION ==========================
if getattr(sys, 'frozen', False):
DATA_DIR = os.path.expanduser("~/.omr_test_manager")
os.makedirs(DATA_DIR, exist_ok=True)
CONFIG_FILE = os.path.join(DATA_DIR, "app_config.json")
DB_FILE = os.path.join(DATA_DIR, "tests.db")
else:
CONFIG_FILE = "app_config.json"
DB_FILE = "tests.db"
PIN_SALT = "some_salt" # Keep fixed for hashing
# ========================== DATABASE ==========================
class Database:
def __init__(self, db_file=DB_FILE):
self.conn = sqlite3.connect(db_file)
self.cursor = self.conn.cursor()
self._create_table()
def _create_table(self):
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS tests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
date TEXT NOT NULL,
template_folder TEXT NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
)
''')
self.conn.commit()
def insert_test(self, name, date, template_folder):
self.cursor.execute(
"INSERT INTO tests (name, date, template_folder) VALUES (?, ?, ?)",
(name, date, template_folder)
)
self.conn.commit()
return self.cursor.lastrowid
def get_all_tests(self):
self.cursor.execute("SELECT id, name, date, template_folder FROM tests ORDER BY created_at DESC")
return self.cursor.fetchall()
def get_test(self, test_id):
self.cursor.execute("SELECT id, name, date, template_folder FROM tests WHERE id=?", (test_id,))
return self.cursor.fetchone()
def update_test(self, test_id, name, date, template_folder):
self.cursor.execute(
"UPDATE tests SET name=?, date=?, template_folder=? WHERE id=?",
(name, date, template_folder, test_id)
)
self.conn.commit()
def delete_test(self, test_id):
self.cursor.execute("DELETE FROM tests WHERE id=?", (test_id,))
self.conn.commit()
def close(self):
self.conn.close()
# ========================== SETTINGS ==========================
class SettingsManager:
def __init__(self, config_file=CONFIG_FILE):
self.config_file = config_file
self.defaults = {
"input_dir": "",
"output_dir": "",
"python_command": "python3 main.py --inputDir {input} --outputDir {output}",
"templates_dir": "",
"firestore_auth_key": "", # path to service account JSON
"firestore_collection": "parents_token",
"pin_hash": self._hash_pin("123456") # default PIN: 123456
}
self.data = self._load()
self._deploy_bundled_samples()
self.current_test_id = None
def _deploy_bundled_samples(self):
if getattr(sys, 'frozen', False):
user_samples_dir = os.path.expanduser("~/OMR_Test_Manager/samples")
if not os.path.exists(user_samples_dir) or not os.listdir(user_samples_dir):
os.makedirs(user_samples_dir, exist_ok=True)
bundled_samples = os.path.join(sys._MEIPASS, "samples")
if os.path.exists(bundled_samples):
for item in os.listdir(bundled_samples):
src_item = os.path.join(bundled_samples, item)
dst_item = os.path.join(user_samples_dir, item)
try:
if os.path.isdir(src_item):
shutil.copytree(src_item, dst_item, dirs_exist_ok=True)
else:
shutil.copy2(src_item, dst_item)
except Exception as e:
print(f"Failed to copy template {item}: {e}")
def _hash_pin(self, pin):
return hashlib.sha256((pin + PIN_SALT).encode()).hexdigest()
def _load(self):
if os.path.exists(self.config_file):
with open(self.config_file, 'r') as f:
try:
return json.load(f)
except:
return self.defaults.copy()
else:
return self.defaults.copy()
def save(self):
with open(self.config_file, 'w') as f:
json.dump(self.data, f, indent=4)
def get(self, key, default=None, raw=False):
if key in ["input_dir", "output_dir", "python_command", "templates_dir"]:
platform_key = f"{key}_{sys.platform}"
val = None
if platform_key in self.data:
val = self.data[platform_key]
elif sys.platform == "win32" and key in self.data:
val = self.data[key]
else:
if getattr(sys, 'frozen', False):
base_dir = os.path.expanduser("~/OMR_Test_Manager")
else:
base_dir = os.path.dirname(os.path.abspath(__file__))
defaults_map = {
"input_dir": os.path.join(base_dir, "inputs"),
"output_dir": os.path.join(base_dir, "outputs"),
"templates_dir": os.path.join(base_dir, "samples"),
"python_command": "python3 main.py --inputDir {input} --outputDir {output}"
}
val = defaults_map.get(key, default)
# Make relative directory paths absolute relative to base_dir
if key in ["input_dir", "output_dir", "templates_dir"] and val and not os.path.isabs(val):
if getattr(sys, 'frozen', False):
base_dir = os.path.expanduser("~/OMR_Test_Manager")
else:
base_dir = os.path.dirname(os.path.abspath(__file__))
val = os.path.abspath(os.path.join(base_dir, val))
if not raw and key in ["input_dir", "output_dir"] and getattr(self, "current_test_id", None) is not None:
val = os.path.join(val, str(self.current_test_id))
return val
return self.data.get(key, default)
def set(self, key, value):
if key in ["input_dir", "output_dir", "python_command", "templates_dir"]:
platform_key = f"{key}_{sys.platform}"
self.data[platform_key] = value
else:
self.data[key] = value
self.save()
def verify_pin(self, pin):
return self._hash_pin(pin) == self.data.get("pin_hash")
def change_pin(self, old_pin, new_pin):
if not self.verify_pin(old_pin):
return False
self.data["pin_hash"] = self._hash_pin(new_pin)
self.save()
return True
# ========================== PDF PROCESSOR ==========================
class PDFProcessor:
def __init__(self, settings):
self.settings = settings
def configure_answer_key(self, test_id, input_dir):
base_input_dir = self.settings.get("input_dir", raw=True)
# Check which type was uploaded
found_ext = None
uploaded_path = None
for ext in [".csv", ".jpg", ".jpeg", ".png", ".json"]:
path = os.path.join(base_input_dir, f"answer_key_{test_id}{ext}")
if os.path.exists(path):
found_ext = ext
uploaded_path = path
break
if not found_ext:
return
# 1. If it's a JSON file, it replaces evaluation.json
if found_ext == ".json":
dst_evaluation_path = os.path.join(input_dir, "evaluation.json")
try:
shutil.copy2(uploaded_path, dst_evaluation_path)
except Exception as e:
print(f"Error copying evaluation JSON: {e}")
return
# 2. If it's CSV, copy as answer_key.csv and configure evaluation.json
if found_ext == ".csv":
dst_csv_path = os.path.join(input_dir, "answer_key.csv")
try:
shutil.copy2(uploaded_path, dst_csv_path)
except Exception as e:
print(f"Error copying answer key CSV: {e}")
return
evaluation_json_path = os.path.join(input_dir, "evaluation.json")
if os.path.exists(evaluation_json_path):
try:
with open(evaluation_json_path, 'r') as f:
data = json.load(f)
except Exception as e:
print(f"Error loading evaluation.json: {e}")
data = {}
else:
data = {}
data["source_type"] = "csv"
if "options" not in data:
data["options"] = {}
data["options"]["answer_key_csv_path"] = "answer_key.csv"
if "answer_key_image_path" in data["options"]:
del data["options"]["answer_key_image_path"]
if "marking_schemes" not in data:
data["marking_schemes"] = {
"DEFAULT": {
"correct": "1",
"incorrect": "0",
"unmarked": "0"
}
}
try:
with open(evaluation_json_path, 'w') as f:
json.dump(data, f, indent=4)
except Exception as e:
print(f"Error writing evaluation.json: {e}")
return
# 3. If it's an Image (jpg, jpeg, png), copy as answer_key.<ext> and configure evaluation.json
if found_ext in [".jpg", ".jpeg", ".png"]:
# Normalize extension to .jpg or .png
norm_ext = ".jpg" if found_ext in [".jpg", ".jpeg"] else ".png"
dst_img_name = f"answer_key{norm_ext}"
dst_img_path = os.path.join(input_dir, dst_img_name)
try:
shutil.copy2(uploaded_path, dst_img_path)
except Exception as e:
print(f"Error copying answer key Image: {e}")
return
evaluation_json_path = os.path.join(input_dir, "evaluation.json")
if os.path.exists(evaluation_json_path):
try:
with open(evaluation_json_path, 'r') as f:
data = json.load(f)
except Exception as e:
print(f"Error loading evaluation.json: {e}")
data = {}
else:
data = {}
data["source_type"] = "csv"
if "options" not in data:
data["options"] = {}
data["options"]["answer_key_csv_path"] = "answer_key.csv"
data["options"]["answer_key_image_path"] = dst_img_name
# Make sure we don't have a stray answer_key.csv left over in the folder
stray_csv = os.path.join(input_dir, "answer_key.csv")
if os.path.exists(stray_csv):
try:
os.remove(stray_csv)
except Exception as e:
print(f"Error removing stray CSV: {e}")
if "marking_schemes" not in data:
data["marking_schemes"] = {
"DEFAULT": {
"correct": "1",
"incorrect": "0",
"unmarked": "0"
}
}
try:
with open(evaluation_json_path, 'w') as f:
json.dump(data, f, indent=4)
except Exception as e:
print(f"Error writing evaluation.json: {e}")
return
# -------------------------------------------------------
# Get PDF Page Count
# -------------------------------------------------------
def get_page_count(self, pdf_path):
try:
import fitz
doc = fitz.open(pdf_path)
return len(doc)
except Exception as e:
raise Exception(f"Unable to read PDF.\n\n{e}")
# -------------------------------------------------------
# Convert PDF to Images
# -------------------------------------------------------
def process_pdf(self, pdf_path, template_folder, progress_callback=None):
base_input_dir = self.settings.get("input_dir", raw=True)
base_output_dir = self.settings.get("output_dir", raw=True)
templates_dir = self.settings.get("templates_dir")
# Validate base folders
if not os.path.exists(base_input_dir):
raise Exception("Base input directory does not exist. Check settings.")
if not os.path.exists(base_output_dir):
raise Exception("Base output directory does not exist. Check settings.")
if not os.path.exists(templates_dir):
raise Exception("Templates directory does not exist.")
# Resolve actual folders and create them
input_dir = self.settings.get("input_dir")
output_dir = self.settings.get("output_dir")
os.makedirs(input_dir, exist_ok=True)
os.makedirs(output_dir, exist_ok=True)
template_source = os.path.join(
templates_dir,
template_folder
)
if not os.path.exists(template_source):
raise Exception(
f"Template folder '{template_folder}' not found."
)
# ---------------------------------------------------
# Clear Input & Output folders
# ---------------------------------------------------
for folder in [input_dir, output_dir]:
for item in os.listdir(folder):
path = os.path.join(folder, item)
print("Deleting:", path)
try:
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
except PermissionError:
print(f"Permission denied: {path}")
# Skip folders that Windows is using
continue
except Exception as e:
print(f"Error deleting {path}: {e}")
continue
# ---------------------------------------------------
# Convert PDF to Images
# ---------------------------------------------------
if progress_callback:
progress_callback("Converting PDF to Images...")
try:
import fitz
doc = fitz.open(pdf_path)
zoom = 300 / 72
matrix = fitz.Matrix(zoom, zoom)
page_count = len(doc)
for i, page in enumerate(doc, start=1):
pix = page.get_pixmap(matrix=matrix)
pix.save(os.path.join(input_dir, f"page_{i}.jpg"))
if progress_callback:
progress_callback(f"{page_count} pages converted.")
except Exception as e:
raise Exception(f"Failed to convert PDF pages: {e}")
# ---------------------------------------------------
# Copy Template Files
# ---------------------------------------------------
if progress_callback:
progress_callback("Copying Template...")
for item in os.listdir(template_source):
src = os.path.join(template_source, item)
dst = os.path.join(input_dir, item)
if os.path.isdir(src):
shutil.copytree(src, dst, dirs_exist_ok=True)
else:
shutil.copy2(src, dst)
if progress_callback:
progress_callback("Template copied successfully.")
# Configure uploaded answer key if exists
test_id = getattr(self.settings, "current_test_id", None)
if test_id is not None:
self.configure_answer_key(test_id, input_dir)
return page_count
# -------------------------------------------------------
# Run OMR Command
# -------------------------------------------------------
def run_command(self, progress_callback=None):
test_id = getattr(self.settings, "current_test_id", None)
input_dir = self.settings.get("input_dir")
if test_id is not None:
self.configure_answer_key(test_id, input_dir)
cmd_template = self.settings.get("python_command")
input_dir = self.settings.get("input_dir")
output_dir = self.settings.get("output_dir")
# Determine if we should use the built-in OMRChecker or fallback to shell command
is_default_cmd = "OMRChecker" in cmd_template or getattr(sys, 'frozen', False)
if is_default_cmd:
if progress_callback:
progress_callback("Initializing built-in OMR Engine...")
try:
import logging
# Create a custom log handler to stream logs to GUI callback
class GUIProgressLogHandler(logging.Handler):
def __init__(self, callback):
super().__init__()
self.callback = callback
def emit(self, record):
try:
msg = self.format(record)
self.callback(msg)
except Exception:
pass
# Add our handler to the root logger
handler = GUIProgressLogHandler(progress_callback)
handler.setFormatter(logging.Formatter('%(message)s'))
root_logger = logging.getLogger()
root_logger.addHandler(handler)
try:
from src.entry import entry_point
from pathlib import Path
from src.utils.interaction import InteractionUtils
InteractionUtils.disable_gui = True
args = {
"input_paths": [input_dir],
"output_dir": output_dir,
"debug": False,
"autoAlign": False,
"setLayout": False
}
for root_path in args["input_paths"]:
entry_point(Path(root_path), args)
if progress_callback:
progress_callback("OMR completed successfully.")
finally:
# Clean up handler
root_logger.removeHandler(handler)
except Exception as e:
import traceback
print(traceback.format_exc())
raise Exception(f"OMR Engine error: {e}")
else:
# Fallback to subprocess execution (original logic)
cmd = (
cmd_template
.replace("{input}", input_dir)
.replace("{output}", output_dir)
)
if progress_callback:
progress_callback(f"Running:\n{cmd}")
try:
result = subprocess.run(
cmd,
shell=True,
capture_output=True,
text=True,
timeout=300
)
time.sleep(2)
# Show command output
print("========== STDOUT ==========")
print(result.stdout)
print("========== STDERR ==========")
print(result.stderr)
if progress_callback:
progress_callback(result.stdout + "\n" + result.stderr)
if result.returncode != 0:
raise Exception(result.stderr)
if progress_callback:
progress_callback("OMR completed successfully.")
return result.stdout
except subprocess.TimeoutExpired:
raise Exception("OMR process timed out.")
# -------------------------------------------------------
# CSV Files
# -------------------------------------------------------
def get_csv_files(self, output_dir):
results_dir = os.path.join(output_dir, "Results")
if not os.path.exists(results_dir):
return []
return [
os.path.join(results_dir, f)
for f in os.listdir(results_dir)
if f.lower().endswith(".csv")
]
# -------------------------------------------------------
# Read CSV
# -------------------------------------------------------
def read_csv(self, csv_path):
rows = []
with open(
csv_path,
newline="",
encoding="utf-8"
) as file:
reader = csv.DictReader(file)
for row in reader:
rows.append(row)
return rows
# ========================== FIRESTORE UPLOADER ==========================
class FirestoreUploader:
def __init__(self, settings):
self.settings = settings
def upload_csv(self, csv_path, progress_callback=None):
"""Upload CSV data to Firestore collection."""
auth_key_path = self.settings.get("firestore_auth_key")
if not auth_key_path or not os.path.exists(auth_key_path):
raise Exception("Firestore auth key file not found. Please set it in Settings.")
collection = "parents_token"
# Initialize Firestore
credentials = service_account.Credentials.from_service_account_file(auth_key_path)
db = firestore.Client(credentials=credentials)
# Read CSV
rows = []
with open(csv_path, 'r', newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
rows.append(row)
if not rows:
raise Exception("CSV file is empty or invalid.")
# Upload each row as a document
batch = db.batch()
for i, row in enumerate(rows):
# Use auto-generated ID or use a field if available
doc_ref = db.collection(collection).document()
batch.set(doc_ref, row)
if i % 500 == 499: # Firestore batch limit is 500
batch.commit()
batch = db.batch()
batch.commit()
if progress_callback:
progress_callback(f"Uploaded {len(rows)} rows to Firestore collection '{collection}'.")
# ========================== MAIN APPLICATION ==========================
class TestManagerApp:
def __init__(self, root):
self.root = root
self.root.title("Test Manager")
self.root.geometry("900x700")
self.settings = SettingsManager()
self.db = Database()
self.processor = PDFProcessor(self.settings)
# Check PIN on startup
self.show_login()
# ---------- LOGIN ----------
def show_login(self):
self.login_frame = Frame(self.root)
self.login_frame.pack(expand=True)
Label(self.login_frame, text="Enter 6-digit PIN", font=('Arial', 16)).pack(pady=20)
self.pin_entry = Entry(self.login_frame, show='*', font=('Arial', 20), width=10, justify='center')
self.pin_entry.pack(pady=10)
self.pin_entry.bind('<Return>', lambda e: self.check_pin())
Button(self.login_frame, text="Login", command=self.check_pin, width=15).pack(pady=10)
self.pin_error = Label(self.login_frame, text="", fg="red")
self.pin_error.pack()
self.pin_entry.focus()
def check_pin(self):
pin = self.pin_entry.get()
if len(pin) != 6 or not pin.isdigit():
self.pin_error.config(text="PIN must be exactly 6 digits.")
return
if self.settings.verify_pin(pin):
self.login_frame.destroy()
self.setup_main_ui()
else:
self.pin_error.config(text="Invalid PIN. Try again.")
self.pin_entry.delete(0, END)
# ---------- MAIN UI ----------
def setup_main_ui(self):
# Menu bar
menubar = Menu(self.root)
self.root.config(menu=menubar)
settings_menu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Settings", menu=settings_menu)
settings_menu.add_command(label="Preferences", command=self.open_settings)
settings_menu.add_separator()
settings_menu.add_command(label="Change PIN", command=self.change_pin_dialog)
settings_menu.add_separator()
settings_menu.add_command(label="Exit", command=self.root.quit)
# Main container with left (CRUD) and right (details/actions)
main_paned = PanedWindow(self.root, orient=HORIZONTAL)
main_paned.pack(fill=BOTH, expand=True, padx=5, pady=5)
# Left frame: list of tests
left_frame = Frame(main_paned)
main_paned.add(left_frame, width=400)
Label(left_frame, text="Tests", font=('Arial', 14)).pack(pady=5)
# CRUD buttons
crud_frame = Frame(left_frame)
crud_frame.pack(fill=X, pady=5)
Button(crud_frame, text="Add Test", command=self.add_test_dialog).pack(side=LEFT, padx=2)
Button(crud_frame, text="Edit", command=self.edit_test_dialog).pack(side=LEFT, padx=2)
Button(crud_frame, text="Delete", command=self.delete_test).pack(side=LEFT, padx=2)
# Test list (Treeview)
self.tree = ttk.Treeview(left_frame, columns=("ID", "Name", "Date", "Template"), show="headings", height=20)
self.tree.heading("ID", text="ID")
self.tree.heading("Name", text="Test Name")
self.tree.heading("Date", text="Date")
self.tree.heading("Template", text="Template")
self.tree.column("ID", width=30)
self.tree.column("Name", width=150)
self.tree.column("Date", width=100)
self.tree.column("Template", width=100)
self.tree.pack(fill=BOTH, expand=True, pady=5)
# Bind selection
self.tree.bind('<<TreeviewSelect>>', self.on_test_select)
# Right frame: details and actions
right_frame = Frame(main_paned)
main_paned.add(right_frame, width=500)
# Test details
self.details_frame = LabelFrame(right_frame, text="Test Details", padx=5, pady=5)
self.details_frame.pack(fill=X, pady=5)
self.test_info_label = Label(self.details_frame, text="Select a test", font=('Arial', 12))
self.test_info_label.pack(anchor=W)
# Action buttons
action_frame = Frame(right_frame)
action_frame.pack(fill=X, pady=5)
self.btn_input_pdf = Button(action_frame, text="Input PDF", command=self.input_pdf, state=DISABLED)
self.btn_input_pdf.pack(side=LEFT, padx=2)
self.btn_run = Button(action_frame, text="Run Command", command=self.run_command, state=DISABLED)
self.btn_run.pack(side=LEFT, padx=2)
self.btn_push = Button(action_frame, text="Push to Firestore", command=self.push_to_firestore, state=DISABLED)
self.btn_push.pack(side=LEFT, padx=2)
# Output display area
self.output_frame = LabelFrame(right_frame, text="CSV Output", padx=5, pady=5)
self.output_frame.pack(fill=BOTH, expand=True, pady=5)
self.output_text = scrolledtext.ScrolledText(self.output_frame, height=10, wrap=NONE)
self.output_text.pack(fill=BOTH, expand=True)
# Progress / status bar
self.status_var = StringVar()
self.status_var.set("Ready")
self.status_bar = Label(self.root, textvariable=self.status_var, relief=SUNKEN, anchor=W)
self.status_bar.pack(fill=X, side=BOTTOM, ipady=2)
# Initially populate list
self.refresh_test_list()
# Store currently selected test id
self.current_test_id = None
self.current_test_data = None
# ---------- TEST LIST OPERATIONS ----------
def refresh_test_list(self):
for item in self.tree.get_children():
self.tree.delete(item)
tests = self.db.get_all_tests()
for test in tests:
self.tree.insert("", END, values=test)
def on_test_select(self, event):
selection = self.tree.selection()
if selection:
item = self.tree.item(selection[0])
values = item['values']
if values:
self.current_test_id = values[0]
self.settings.current_test_id = values[0]
self.current_test_data = {
"id": values[0],
"name": values[1],
"date": values[2],
"template": values[3]
}
# Check for answer key
base_input_dir = self.settings.get("input_dir", raw=True)
key_status = "None"
for ext in [".csv", ".jpg", ".jpeg", ".png", ".json"]:
uploaded_key = os.path.join(base_input_dir, f"answer_key_{self.current_test_id}{ext}")
if os.path.exists(uploaded_key):
key_status = f"Uploaded ({ext[1:].upper()})"
break
self.test_info_label.config(text=f"Test: {values[1]} | Template: {values[3]} | Answer Key: {key_status}")
self.btn_input_pdf.config(state=NORMAL)
self.btn_run.config(state=NORMAL)
self.btn_push.config(state=NORMAL)
# Clear output display
self.output_text.delete(1.0, END)
# Check if CSV exists in output dir and display it
self.display_latest_csv()
else:
self.current_test_id = None
self.settings.current_test_id = None
self.current_test_data = None
self.test_info_label.config(text="Select a test")
self.btn_input_pdf.config(state=DISABLED)
self.btn_run.config(state=DISABLED)
self.btn_push.config(state=DISABLED)
# ---------- CRUD DIALOGS ----------
def add_test_dialog(self):
self._open_test_dialog("Add Test", None)
def edit_test_dialog(self):
if not self.current_test_id:
messagebox.showwarning("No selection", "Please select a test to edit.")
return
test = self.db.get_test(self.current_test_id)
if test:
self._open_test_dialog("Edit Test", test)
def _open_test_dialog(self, title, test_data):
dialog = Toplevel(self.root)
dialog.title(title)
dialog.geometry("500x300")
dialog.transient(self.root)
dialog.grab_set()
# Load template folders from templates_dir (find folders containing template.json)
templates_dir = self.settings.get("templates_dir")
template_options = []
if os.path.exists(templates_dir):
for root, dirs, files in os.walk(templates_dir):
if "template.json" in files:
rel_path = os.path.relpath(root, templates_dir)
if rel_path != ".":
template_options.append(rel_path)
template_options.sort()
else:
messagebox.showwarning("Templates folder not set", "Please set the templates folder in Settings.")
# Variables
name_var = StringVar()
date_var = StringVar()
template_var = StringVar()
csv_file_path_var = StringVar()
test_id = test_data[0] if test_data else None
has_existing_key = False
existing_filename = ""
if test_id:
base_input_dir = self.settings.get("input_dir", raw=True)
for ext in [".csv", ".jpg", ".jpeg", ".png", ".json"]:
path = os.path.join(base_input_dir, f"answer_key_{test_id}{ext}")
if os.path.exists(path):
existing_filename = f"answer_key{ext}"
csv_file_path_var.set(f"Already uploaded ({existing_filename})")
has_existing_key = True
break
if not has_existing_key:
csv_file_path_var.set("No file selected")
else:
csv_file_path_var.set("No file selected")
selected_csv = [None]
def browse_csv():
path = filedialog.askopenfilename(
title="Select Answer Key File",
filetypes=[
("CSV Files", "*.csv"),
("Image Files", "*.jpg *.jpeg *.png"),
("JSON Files", "*.json"),
("All Files", "*")
]
)
if path:
ext = os.path.splitext(path)[1].lower()
selected_csv[0] = (path, ext)
csv_file_path_var.set(os.path.basename(path))
def clear_csv():
selected_csv[0] = "CLEAR"
csv_file_path_var.set("No file selected")
if test_data:
name_var.set(test_data[1])
date_var.set(test_data[2])
template_var.set(test_data[3])
# Layout
Label(dialog, text="Test Name:").grid(row=0, column=0, sticky=W, padx=5, pady=5)
Entry(dialog, textvariable=name_var, width=30).grid(row=0, column=1, padx=5, pady=5)
Label(dialog, text="Date (YYYY-MM-DD):").grid(row=1, column=0, sticky=W, padx=5, pady=5)
Entry(dialog, textvariable=date_var, width=30).grid(row=1, column=1, padx=5, pady=5)
Label(dialog, text="Template Folder:").grid(row=2, column=0, sticky=W, padx=5, pady=5)
template_combo = ttk.Combobox(dialog, textvariable=template_var, values=template_options, width=27)
template_combo.grid(row=2, column=1, padx=5, pady=5)
template_combo['state'] = 'readonly'
if not template_options:
template_combo.set("No templates found")
elif template_var.get() and template_var.get() in template_options:
template_combo.set(template_var.get())
elif template_options:
template_combo.set(template_options[0])
Label(dialog, text="Answer Key CSV/Img/JSON:").grid(row=3, column=0, sticky=W, padx=5, pady=5)
csv_info_frame = Frame(dialog)
csv_info_frame.grid(row=3, column=1, sticky=W, padx=5, pady=5)
csv_label = Label(csv_info_frame, textvariable=csv_file_path_var, width=20, anchor=W)
if has_existing_key:
csv_label.config(fg="green")
csv_label.pack(side=LEFT)
Button(csv_info_frame, text="Browse...", command=browse_csv).pack(side=LEFT, padx=2)
Button(csv_info_frame, text="Clear", command=clear_csv).pack(side=LEFT, padx=2)
def save():
name = name_var.get().strip()
date = date_var.get().strip()
template = template_var.get()
if not name or not date or not template:
messagebox.showerror("Error", "All fields are required.")
return
# Validate date format
try:
datetime.strptime(date, "%Y-%m-%d")
except ValueError:
messagebox.showerror("Error", "Date must be in YYYY-MM-DD format.")
return
if test_data:
# Update
self.db.update_test(test_data[0], name, date, template)
inserted_id = test_data[0]
else:
# Insert
inserted_id = self.db.insert_test(name, date, template)
# Save uploaded answer key if one was selected
if inserted_id:
base_input_dir = self.settings.get("input_dir", raw=True)
os.makedirs(base_input_dir, exist_ok=True)
# If we cleared or selected a new one, delete any old ones first
if selected_csv[0] == "CLEAR" or selected_csv[0] is not None:
for ext in [".csv", ".jpg", ".jpeg", ".png", ".json"]:
old_path = os.path.join(base_input_dir, f"answer_key_{inserted_id}{ext}")
if os.path.exists(old_path):
try:
os.remove(old_path)
except Exception as e:
print(f"Error removing old key: {e}")
if selected_csv[0] is not None and selected_csv[0] != "CLEAR":
src_path, ext = selected_csv[0]
target_path = os.path.join(base_input_dir, f"answer_key_{inserted_id}{ext}")
try:
shutil.copy2(src_path, target_path)
except Exception as e:
messagebox.showerror("Error", f"Failed to save answer key file: {e}")
return
self.refresh_test_list()
dialog.destroy()
Button(dialog, text="Save", command=save, width=10).grid(row=4, column=0, pady=20)
Button(dialog, text="Cancel", command=dialog.destroy, width=10).grid(row=4, column=1, pady=20)
def delete_test(self):
if not self.current_test_id:
messagebox.showwarning("No selection", "Please select a test to delete.")
return
if messagebox.askyesno("Delete", "Are you sure you want to delete this test?"):
# Delete uploaded answer key if exists
base_input_dir = self.settings.get("input_dir", raw=True)
for ext in [".csv", ".jpg", ".jpeg", ".png", ".json"]:
uploaded_key = os.path.join(base_input_dir, f"answer_key_{self.current_test_id}{ext}")
if os.path.exists(uploaded_key):
try:
os.remove(uploaded_key)
except Exception as e:
print(f"Error removing key on test delete: {e}")
self.db.delete_test(self.current_test_id)
self.refresh_test_list()
self.on_test_select(None) # clear selection
# ---------- PDF INPUT AND PROCESSING ----------
def input_pdf(self):
if not self.current_test_data:
return
# Select PDF file
pdf_path = filedialog.askopenfilename(
title="Select PDF file",
filetypes=[("PDF files", "*.pdf"), ("All files", "*.*")]
)
if not pdf_path:
return
# Show page count
try:
page_count = self.processor.get_page_count(pdf_path)
answer = messagebox.askyesno(
"PDF Info",
f"PDF has {page_count} pages.\nProceed with processing? This will clear input/output folders."
)
if not answer:
return
except Exception as e:
messagebox.showerror("Error", f"Cannot read PDF: {e}")