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
|
#!/usr/bin/env python3
# Copyright 2023 Florian Fischer
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
"""simple interactive quiz with variable question pool"""
import argparse
import csv
import fnmatch
from pathlib import Path
import random
import platform
import os
import tomllib
from typing import NoReturn
import subprocess
import sys
import PySimpleGUI as sg
import psutil
from playsound3 import playsound
WRONG_SOUND = Path(__file__).parent / 'Wrong.mp3'
CORRECT_SOUND = Path(__file__).parent / 'Correct.mp3'
SHOW_QUESTION_CMD = 'xdg-open'
sg.SetOptions(window_location=(0, -200))
def err(msg: str, status=1) -> NoReturn:
"""Print the error message to stderr and exit with status"""
print(msg)
sys.exit(status)
def terminate_subprocesses():
"""Terminate all started subprocesses"""
procs = psutil.Process().children(recursive=True)
for proc in procs:
proc.terminate()
_, alive = psutil.wait_procs(procs)
for proc in alive:
proc.kill()
def possible_questions(question_dirs: list[Path], match='*') -> list[Path]:
"""return all files in the question directories"""
questions = []
ignored_file_names = [Path(__file__).name, 'solutions']
for question_dir in question_dirs:
if not question_dir.is_dir():
err(f'question directory {question_dir} is not a directory')
questions.extend([
p for p in question_dir.iterdir() if p.is_file()
if p.name not in ignored_file_names and fnmatch.fnmatch(p, match)
])
return questions
def exclude_questions(questions: list[Path],
excludes_file_path: str) -> list[Path]:
"""exclude all paths in excludes_file from the questions"""
with open(excludes_file_path, 'r', encoding='utf-8') as excludes_file:
excludes = [line[:-1] for line in excludes_file.readlines()]
return [q for q in questions if q.name not in excludes]
def read_solutions_from_toml(solution_path: Path,
selector='') -> dict[str, str]:
"""return solutions from a toml solutions file
Nested tables can be selected using the selector.
"""
with open(solution_path, 'rb') as solution_file:
solution_data = tomllib.load(solution_file)
if selector:
for key in selector.split('.'):
solution_data = solution_data[key]
return solution_data
def read_solutions_from_csv(path: Path) -> dict[str, str]:
"""return solutions from a csv solutions file"""
with open(path, 'r', encoding='utf-8') as solution_file:
csvreader = csv.reader(solution_file, delimiter=',')
solution_data = list(csvreader)
# Skip obtional header
if solution_data[0] == ['question', 'solution']:
solution_data = solution_data[1:]
return dict(solution_data)
def load_solutions(question_dirs: list[Path],
solution_files: list[Path],
selector='') -> dict[str, str]:
"""return solutions for the loaded questions
solutions must be either in a solutions file per question directory or in
a single solutions file.
Solution files must be toml or csv files.
Nested tables can be selected using the selector.
"""
solutions = {}
if len(question_dirs) == len(solution_files):
q_s_mapping = list(zip(question_dirs, solution_files))
elif len(solution_files) == 1:
q_s_mapping = [(qd, solution_files[0]) for qd in question_dirs]
else:
err('question and solution file mismatch')
for question_dir, solution_path in q_s_mapping:
if not solution_path.is_file():
err(f'solution file {solution_path} does not exist')
if solution_path.suffix == '.toml':
solutions_from_file = read_solutions_from_toml(solution_path,
selector=selector)
elif solution_path.suffix == '.csv':
solutions_from_file = read_solutions_from_csv(solution_path)
else:
err(f'Unsupported solution file format: {solution_path.suffix}')
flattened_solutions = {
f'{question_dir / k}': str(v)
for k, v in solutions_from_file.items()
}
solutions.update(flattened_solutions)
return solutions
def show_question(question: Path):
"""show the question in an external programm and return the process"""
if platform.system() == 'Windows':
os.startfile(question) # type: ignore # pylint: disable=no-member
return
cmd = SHOW_QUESTION_CMD.split() + [str(question)]
subprocess.Popen(cmd)
def color_button(layout, text: str, color: str):
"""Colorize the button containing the text"""
for button in layout:
if button.get_text() == text:
button.update(button_color=color)
break
def prompt_solution(solution: str, prompts=None):
"""prompt for the solution"""
if prompts is None:
prompts = ['A', 'B', 'C', 'D', 'E']
layout = [[sg.Button(c) for c in prompts]]
window = sg.Window('', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
try:
event, _ = window.read()
except KeyboardInterrupt:
sys.exit(0)
if event == sg.WIN_CLOSED: # if user closes window or clicks cancel
break
if event == solution:
break
color_button(layout[0], event, '#FF0000')
playsound(str(WRONG_SOUND))
color_button(layout[0], event, '#00FF00')
playsound(str(CORRECT_SOUND))
window.read()
window.close()
def main():
"""main entry point of the quiz"""
parser = argparse.ArgumentParser()
parser.add_argument('question_dir',
nargs='*',
help='directories containing questions')
parser.add_argument('-s',
'--solution-file',
help='file containing the solutions')
parser.add_argument('-x',
'--excludes-file',
help='file containing the questions to exclude')
parser.add_argument(
'--select', help='selector string to identify a subset of solutions')
parser.add_argument('--question-match',
help='match question files names',
default='*')
parser.add_argument('--answers',
help='match question files names',
nargs='*')
parser.add_argument('--sort',
default='random',
const='random',
nargs='?',
choices=['random', 'filename', 'solutions'])
args = parser.parse_args()
question_dirs = [Path(p) for p in args.question_dir or ['.']]
questions = possible_questions(question_dirs, match=args.question_match)
if args.excludes_file:
questions = exclude_questions(questions, args.excludes_file)
if args.sort == 'filename':
questions.sort()
solution_files = []
if args.solution_file:
solution_files.append(Path(args.solution_file))
else:
solution_files.extend([p / 'solutions.csv' for p in question_dirs])
solutions = load_solutions(question_dirs, solution_files, args.select)
# Sort the questions acording to the loaded solutions
if args.sort == 'solutions':
sorted_questions = []
for s in solutions:
key = Path(s).stem
for q in questions:
if q.stem == key:
sorted_questions.append(q)
continue
questions = sorted_questions
solved = []
while questions:
if args.sort != 'random':
next_question = questions[0]
else:
next_question = random.choice(questions)
solution = solutions[f'{next_question.parent / next_question.stem}']
print(f'{next_question}: {solution}', end='')
show_question(next_question)
prompt_solution(solution, prompts=args.answers)
terminate_subprocesses()
solved.append(next_question)
questions.remove(next_question)
print(' Done.')
print(f'Solved: {solved}')
if __name__ == '__main__':
main()
|