def run(): config.set_debug(True, 0) failed = 0 passed = 0 Logger.Log( Logger.Color.BOLD.value + "Unit tests:" + Logger.Color.ENDC.value, 0, Logger.Color.UNDERLINE) active_tests = [ queue_dequeue, queue_status, valid_char, invalid_char, semi_auto, auto, wait_time, high_priority ] for func in active_tests: if func(): passed = passed + 1 else: failed = failed + 1 e_Handler.get_queue().clear_queue() Logger.Log( "All done! " + "(" + str(passed) + " Passed) (" + str(failed) + " Failed)", 0, Logger.Color.BOLD)
def dequeue(self): with self._lock: if not len(self._queue) == 0: Logger.Log("Dequeue '" + self._queue[0].get_name() + "'...\n", 3) self._queue.pop(0) Logger.Log(str(self) + "\n", 1)
def parse_char(self, _str): _str = _str.lower() if Configuration.Instance.get_purpose() == Purpose.Purpose.Education: not_handled_chars = "" for char in _str: if not char.isspace(): # skip char if it's a whitespace if self.is_char_valid(char) or self.is_char_valid( char.upper()): Logger.Log("char parsed: " + char + " (valid)\n", 3) self.push_event( Event.Event(char, EventType.EventType.letter)) else: Logger.Log("char parsed: " + char + " (invalid)\n", 3) not_handled_chars = not_handled_chars + char self.push_event( Event.Event(char, EventType.EventType.invalid_letter, True)) if not_handled_chars != "": self._ui_adress.not_handled_chars_window(not_handled_chars) elif Configuration.Instance.get_purpose() == Purpose.Purpose.Quiz: if len(_str) > 1: Logger.Log( "Too many character! Please enter a single letter for Quiz mode" ) return False else: Logger.Log("Quiz answer pushed (" + _str + ")", 2) self.push_event( Event.Event(_str, EventType.EventType.quiz_answer)) return True
def print_result(test, result): if result: Logger.Log( test + " test: " + Logger.Color.GREEN.value + "Passed" + Logger.Color.ENDC.value, 0) else: Logger.Log( test + " test: " + Logger.Color.RED.value + "Failed" + Logger.Color.ENDC.value, 0) return result
def validate_answer(self, answer): is_valid = (str(answer).lower() == self.get_current_letter().lower()) if is_valid: self.increment_score() self.get_new_letter() Logger.Log("Good Answer! (" + str(answer) + ")", 1, Logger.Color.BLUE) else: Logger.Log("Wrong Answer! :( (" + str(answer) + ")", 1, Logger.Color.RED) return is_valid
def add(self, event): with self._lock: #If event is 'High priority' it will be added at the beggining of the queue if event.is_high_priority(): Logger.Log( "Event added: " + str(event.get_type()) + " (" + event.get_name() + ") high priority\n", 2) self._queue.insert(0, event) #If event is not 'High priority' it will be added at the end of the queue else: Logger.Log( "Event added: " + str(event.get_type()) + " (" + event.get_name() + ") low priority\n", 2) self._queue.append(event)
def get_valid_string(): available_chars = config.get_available_chars() _str = "" for _char in available_chars: _str = _str + _char Logger.Log("valid string: " + _str, 0) return _str
def _random_letter(self): is_same_as_last = True rnd_letter = None while is_same_as_last: rnd_int = random.randint( 0, len(Configuration.Instance._available_chars) - 1) rnd_letter = Configuration.Instance._available_chars[rnd_int] if not rnd_letter == self._last_letter: is_same_as_last = False Logger.Log("New random letter: " + str(rnd_letter), 1) return rnd_letter
def high_priority(): e_Handler.clear_queue() sucess = True if not config.is_semi_auto(): config.toggle_semi_auto() textAnalyser.parse_char(valid_string) textAnalyser.push_event( Event.Event('first', EventType.EventType.letter, True)) textAnalyser.push_event( Event.Event('last', EventType.EventType.letter, False)) sucess = sucess and e_Handler.get_queue().first().get_name( ) == 'first' and e_Handler.get_queue().last().get_name() == 'last' Logger.Log( 'Excpected "first" got "' + str(e_Handler.get_queue().first().get_name()), 0.5) Logger.Log( 'Excpected "last" got "' + str(e_Handler.get_queue().last().get_name()), 0.5) return print_result("High priority", sucess)
def wait_time(): e_Handler.clear_queue() sucess = True test_wait_time = 3 if not config.is_semi_auto(): config.toggle_semi_auto() config.set_wait_time(test_wait_time) sucess = sucess and config.get_wait_time() == test_wait_time Logger.Log(str(sucess) + ' wait time: ' + str(config.get_wait_time()), 0.5) textAnalyser.parse_char(valid_string[0] + valid_string[0]) original_length = e_Handler.get_queue().get_length() config.toggle_semi_auto() for i in range(0, test_wait_time): sucess = sucess and e_Handler.get_queue().get_length( ) == original_length Logger.Log( 'Success: ' + str(sucess) + ' - waited : ' + str(i) + 's expected length: ' + str(original_length) + ' and got length: ' + str(e_Handler.get_queue().get_length()), 0.5) time.sleep(1) time.sleep(1) sucess = sucess and e_Handler.get_queue().get_length( ) == original_length - 1 Logger.Log( 'Success: ' + str(sucess) + ' - waited : ' + str(test_wait_time) + 's expected length: ' + str(original_length - 1) + ' and got length: ' + str(e_Handler.get_queue().get_length()), 0.5) return print_result('Wait time', sucess)
def execute_event(self, event): # This section handles events while in purpose "Quiz" if Configuration.Instance.get_purpose() == Purpose.Purpose.Quiz: #Initiate the quiz if not done already if Quiz.Instance.get_current_letter() is None: Quiz.Instance.get_new_letter() #Send the letter to be guessed to the Arduino Communication.Instance.update_stream( ord(Quiz.Instance.get_current_letter()), Configuration.Instance.get_purpose_string(), Configuration.Instance.get_wait_time()) Communication.Instance.send_stream() self._ui_adress.change_state_picture( True, Quiz.Instance.get_current_letter()) self._ui_adress.change_hand_ready_state(False) self.wait_arduino_ready_state() self._ui_adress.enable_entry() self._ui_adress.change_hand_ready_state(True) if event is None: return if not event.is_type(EventType.EventType.quiz_answer): Logger.Log( "'" + str(event.get_type()) + "' is not a valid event type\n", 1) else: #Check if the given answer is valid and set a new one if it was if Quiz.Instance.validate_answer(event.get_name()): #Send a new letter to the Arduino if the answer was valid Communication.Instance.update_stream( ord(Quiz.Instance.get_current_letter()), Configuration.Instance.get_purpose_string(), Configuration.Instance.get_wait_time()) Communication.Instance.send_stream() self._ui_adress.change_state_picture( True, Quiz.Instance.get_current_letter()) self._ui_adress.change_hand_ready_state(False) self.wait_arduino_ready_state() self._ui_adress.enable_entry() self._ui_adress.change_hand_ready_state(True) self._ui_adress.quiz_answer_conclusion(True) self._ui_adress.modify_quiz_score() else: #Re-enable entry if the answer was invalid self._ui_adress.enable_entry() self._ui_adress.change_hand_ready_state(True) self._ui_adress.quiz_answer_conclusion(False) self._queue.dequeue() #This section handles event while in "Education" purpose elif Configuration.Instance.get_purpose() == Purpose.Purpose.Education: if event is None: return if event.is_type(EventType.EventType.invalid_letter ): # Invalid letter event Logger.Log( "'" + event.get_name() + "' is not a valid character\n", 1) self._queue.dequeue() if self._queue.is_empty(): self._ui_adress.enable_entry() self._ui_adress.change_hand_ready_state(True) self._ui_adress.enable_radiobuttons() self._ui_adress.change_state_picture(False, '') elif event.is_type(EventType.EventType.letter): # Letter event # Only execute if in auto mode or if trigger is true if self._trigger: Logger.Log( "Executing letter '" + event.get_name() + "'...\n", 2) Communication.Instance.update_stream( ord(event.get_name()), Configuration.Instance.get_purpose_string(), Configuration.Instance.get_wait_time()) Communication.Instance.send_stream() self._ui_adress.change_state_picture( True, event.get_name()) self._ui_adress.change_hand_ready_state(False) self.wait_arduino_ready_state() self._queue.dequeue() if Configuration.Instance.is_semi_auto( ): # Reset the trigger if in semi automatic mode self._ui_adress.change_hand_ready_state(True) self._trigger = False else: time.sleep(int(Configuration.Instance.get_wait_time()) ) # Delay between letters in automatic mode elif not self._trigger_warned: if not self._queue.is_empty(): Logger.Log("Waiting for trigger...\n", 1) else: Logger.Log("Queue Empty...\n", 1) self._trigger_warned = True #Re-enable user input and set ready state to true if the EventQueue is empty if self._queue.is_empty(): self._ui_adress.enable_entry() self._ui_adress.change_hand_ready_state(True) self._ui_adress.enable_radiobuttons() self._ui_adress.change_state_picture(False, '') else: Logger.Log("Event type is invalid\n", 2)
def next_letter(self): self._trigger = True self._trigger_warned = False Logger.Log('Trigger sent ...\n', 2)
def print_queue(self): Logger.Log(self, 1) print("\n")