def _ask(self, answers: Dict) -> Union[Dict, None]: """ Really ask the question. We may need to populate multiple validators with answers here. Then ask the question and insert the default value if appropriate. Finally call the validate function to check all validators for this question and returning the answer. """ if isinstance(self.validator, list): for v in self.validator: v.answers = answers else: self.validator.answers = answers while(True): q = self.question % answers if not self.choices(): logger.warning('No choices were supplied for "%s"' % q) return None if self.value in answers: default = Validator.stringify(answers[self.value]) answer = self._get_input("%s [%s]: " % (q, default)) if answer == '': answer = answers[self.value] else: answer = self._get_input("%s: " % q) # if we are in multiple mode and the answer is just the empty # string (enter/return pressed) then we will just answer None # to indicate we are done if answer == '.' and self.multiple: return None if self.validate(answer): return self.answer() else: if isinstance(self.validator, list): for v in self.validator: if v.error() != '': print(v.error()) else: print(self.validator.error())
def _ask(self, answers): """ Really ask the question. We may need to populate multiple validators with answers here. Then ask the question and insert the default value if appropriate. Finally call the validate function to check all validators for this question and returning the answer. """ if isinstance(self.validator, list): for v in self.validator: v.answers = answers else: self.validator.answers = answers while(True): q = self.question % answers if not self.choices(): return None if self.value in answers: default = Validator.stringify(answers[self.value]) answer = raw_input("%s [%s]: " % (q, default)) if answer == '': answer = answers[self.value] else: answer = raw_input("%s: " % q) # if we are in multiple mode and the answer is just the empty # string (enter/return pressed) then we will just answer None # to indicate we are done if answer == '.' and self.multiple: return None if self.validate(answer): return self.answer() else: if isinstance(self.validator, list): for v in self.validator: if v.error() != '': print v.error() else: print self.validator.error()
def test_stringify(self): assert Validator.stringify(5) == '5' assert Validator.stringify('5') == '5'