forked from awfulwaffle77/ExamTopicsQuizMaker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
80 lines (63 loc) · 2.69 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
import os
import shutil
from quiz import Quiz
RES_DIR = "./res" # Directory of resources
EXAM_DIR = "./exams" # Directory of exams
def read_exam(exam_dir):
"""Reads exam directories from the exam directory and returns them as a list."""
return [exam for exam in os.listdir(exam_dir) if os.path.isdir(os.path.join(exam_dir, exam))]
def choose_exam(exam_dir, res_dir):
"""Choose the exam to be read."""
if os.path.exists(res_dir):
current_exams = [item for item in os.listdir(res_dir) if os.path.isdir(os.path.join(res_dir, item))]
if current_exams:
print("Your current exam(s):")
for exam in current_exams:
print(f"- {exam}")
else:
print("No current exams found in the resource directory.")
if input("Do you want to delete the current exam? [Y/n] ").lower() != 'n':
shutil.rmtree(res_dir)
print("Deleted the current exam. Please choose a new one.")
else:
print("Keeping the current exam.")
return next((item for item in os.listdir(res_dir) if os.path.isdir(os.path.join(res_dir, item))), None)
exams = read_exam(exam_dir)
if not exams:
print("No exams available.")
return None
print("Choose the exam you want to take: ")
for i, exam in enumerate(exams, start=1):
print(f"{i}. {exam}")
while True:
try:
exam_choice = int(input("Enter the number of the exam you want to take: ")) - 1
if 0 <= exam_choice < len(exams):
return exams[exam_choice]
print("Invalid number, please try again.")
except ValueError:
print("Invalid input, please enter a number.")
def copy_exam(exam_dir, exam, res_dir):
"""Copy the exam to the resources directory."""
os.makedirs(res_dir, exist_ok=True)
src_exam_dir = os.path.join(exam_dir, exam)
src_html_file = os.path.join(exam_dir, f"{exam}.html")
dest_exam_dir = os.path.join(res_dir, exam)
dest_html_file = os.path.join(res_dir, f"{exam}.html")
if os.path.exists(dest_exam_dir):
shutil.rmtree(dest_exam_dir)
shutil.copytree(src_exam_dir, dest_exam_dir)
if os.path.exists(dest_html_file):
os.remove(dest_html_file)
shutil.copy2(src_html_file, dest_html_file)
if __name__ == "__main__":
chosen_exam = choose_exam(EXAM_DIR, RES_DIR)
if chosen_exam:
os.makedirs(RES_DIR, exist_ok=True)
current_exams = os.listdir(RES_DIR)
if chosen_exam not in current_exams:
copy_exam(EXAM_DIR, chosen_exam, RES_DIR)
quiz = Quiz(str(chosen_exam), RES_DIR)
quiz.start_quiz()
else:
print("No exam selected. Exiting.")