-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_setup.py
185 lines (158 loc) · 6.15 KB
/
my_setup.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
"""
This module contains the SetupManager class, which is responsible for getting
the number of chapters from the user.
"""
import io_util
from speaker import SpeakerFactory
# Define the instructions as a list of strings
instructions = [
'Press any key to continue to the next instruction or "Space" key to skip.',
"This program will help you keep track of your time while studying for the PET exam.",
'Press the "n" key to move to the next chapter, and press "q" to quit.',
"The timer will run for 20 minutes, and you will be notified when the time is up.",
'You can also use the "q" key to quit the program at any time.',
]
# Define a dictionary to map integers to spoken string equivalents
number_dict = {
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
class SetupManager:
"""
A class to manage the setup of the program.
"""
def __init__(self):
print("Setting up the program...")
try:
self.synthesizer = SpeakerFactory.create_speaker()
print("Synthesizer created!")
with open("log.txt", "a") as f:
f.write("Synthesizer created!\n")
except Exception as e:
print(f"An error occurred: {e}")
raise SystemExit
try:
print("Getting the number of chapters...")
self.chapters, self.extra_essay = self.get_total_chapters()
except Exception as e:
print(f"An error occurred: {e}")
raise SystemExit
print("Setup complete!")
# log to log file
with open("log.txt", "a") as f:
f.write("Setup complete!\n")
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
del self.synthesizer
def check_valid_chapters(self, chapters) -> bool:
"""
Checks if the number of chapters is valid.
:param chapters: The number of chapters.
:return: True if the number of chapters is valid, False otherwise.
"""
if chapters < 1 or chapters > 8:
return False
return True
def get_setup(self) -> tuple:
"""
Gets the setup for the program.
:return: A tuple containing the synthesizer,
the number of chapters, and whether or not there is an extra essay.
"""
return self.synthesizer, self.chapters, self.extra_essay
# a function to get the number of chapters from the user
def get_chapters_from_input(self) -> int:
"""
Gets the number of chapters from the user.
:return: The number of chapters.
"""
self.synthesizer.speak_text_async("Welcome back!") # async = non-blocking
for instruction in instructions:
# Print the instruction to the io_util.screen
io_util.screen.clear()
io_util.screen.addstr(instruction + "\n")
io_util.screen.refresh() # Update the io_util.screen to show the instruction
key = io_util.wait_for_key() # Wait for the user to press a key
if key == (
" "
):
break
io_util.screen.clear()
self.synthesizer.speak_text_async("How many chapters?")
io_util.screen.addstr("Enter the number of chapters: ")
chapters_str = ""
while True:
char = io_util.wait_for_key()
if char == "\n":
if self.check_valid_chapters(int(chapters_str)):
break
else:
io_util.screen.addstr("Invalid number, try again: ")
chapters_str = ""
continue
elif char.isdigit():
chapters_str += char
io_util.screen.addstr(char)
elif char == "\b":
chapters_str = chapters_str[:-1]
io_util.screen.addstr("\b \b")
chapters = int(chapters_str)
return chapters
def ask_for_extra_essay(self) -> bool:
"""
Asks the user if they have an extra essay.
:return: True if the user has an extra essay, False otherwise.
"""
self.synthesizer.speak_text_async("Do you have an extra essay?")
io_util.screen.addstr('\nPress "Y" for 30 minutes essay other key to pass: ')
while True:
char = io_util.wait_for_key()
io_util.screen.addstr(char)
if char == "\n":
break
elif char == "y":
return True
def get_total_chapters(self) -> tuple:
"""
Gets the total number of chapters from the user.
:return: A tuple containing the number of chapters and whether or not
there is an extra essay.
"""
# Options for default number of chapters 8 with extra essay
chapters = 8
extra_essay = True
try:
# Ask the user if they want to change the default options
self.synthesizer.speak_text_async("Do you want to change the default options?")
io_util.screen.addstr('\nPress "Y" to change or any other key to continue: ')
char = io_util.wait_for_key()
io_util.screen.addstr(char)
if char != "y":
self.synthesizer.speak_text_async("Default options selected!")
io_util.screen.addstr("\nDefault options selected!")
io_util.wait_for_key()
return chapters, extra_essay
chapters = self.get_chapters_from_input()
extra_essay = self.ask_for_extra_essay()
if extra_essay:
chapters += 1
self.synthesizer.speak_text_async(
"Oh Yeah! press any key when you are ready... and good luck!"
)
io_util.screen.addstr(f"\nNumber of chapters: {number_dict[chapters]}")
if extra_essay:
io_util.screen.addstr(" + exta essay")
io_util.wait_for_key()
self.synthesizer.speak_text_async("Test Started!")
except Exception as e:
print(f"An error occurred: {e}")
return chapters, extra_essay