def get_previously_sent_item(self, menu_system): """ Get the item that was previously sent to the client. It is needed to be able to determine what question the incoming answer is answering. """ previous_index = self.session_manager.data.get("previous_index", None) # we can't check for just previous_index, since zero resolves to False # in an if statement if previous_index >= 0: return copy_generator(menu_system.stack[previous_index - 1])
def case(*cases): """Returns the first prompt for which the test function returns True""" while True: ms, session = yield for test, prompt in cases: if test(ms, session): prompt = copy_generator(prompt) prompt.next() question = prompt.send((ms, session)) yield question answer = yield prompt.next() validated_answer = prompt.send(answer) yield validated_answer yield False, False # no message and not end of menu
def next(self): """ Proceed to the next coroutine in the stack """ # If we've reached the end of the stack stop iterating if self.__iter_index > len(self.stack): raise StopIteration # If we're at the last time there isn't a next item to send # to the client so return None if self.__iter_index == len(self.stack): next_item = None else: # otherwise, copy the coming coroutine, the index is always # one ahead of where we are actually at. next_item = copy_generator(self.stack[self.__iter_index]) # increment, ready for next round self.__iter_index += 1 return self.__iter_index, next_item