示例#1
0
def getUserInput(text, default, choices, parser = lambda x: x):
	while True:
		try:
			userinput = user_input('%s %s: ' % (text, '[%s]' % default))
		except:
			eprint()
			sys.exit(0)
		if userinput == '':
			return parser(default)
		if parser(userinput) != None:
			return parser(userinput)
		valid = str.join(', ', map(lambda x: '"%s"' % x, choices[:-1]))
		eprint('Invalid input! Answer with %s or "%s"' % (valid, choices[-1]))
示例#2
0
def getUserInput(text, default, choices, parser=lambda x: x):
    while True:
        try:
            userinput = user_input("%s %s: " % (text, "[%s]" % default))
        except Exception:
            eprint()
            sys.exit(os.EX_OK)
        if userinput == "":
            return parser(default)
        if parser(userinput) != None:
            return parser(userinput)
        valid = str.join(", ", map(lambda x: '"%s"' % x, choices[:-1]))
        eprint('Invalid input! Answer with %s or "%s"' % (valid, choices[-1]))
示例#3
0
def getUserInput(text, default, choices, parser = identity):
	while True:
		handler = signal.signal(signal.SIGINT, signal.SIG_DFL)
		try:
			userinput = user_input('%s %s: ' % (text, '[%s]' % default))
		except Exception:
			sys.stdout.write('\n') # continue on next line
			raise
		signal.signal(signal.SIGINT, handler)
		if userinput == '':
			return parser(default)
		if parser(userinput) is not None:
			return parser(userinput)
		valid = str.join(', ', imap(lambda x: '"%s"' % x, choices[:-1]))
		logging.getLogger('user').critical('Invalid input! Answer with %s or "%s"', valid, choices[-1])
示例#4
0
def getUserInput(text, default, choices, parser=identity):
    while True:
        handler = signal.signal(signal.SIGINT, signal.SIG_DFL)
        try:
            userinput = user_input('%s %s: ' % (text, '[%s]' % default))
        except Exception:
            sys.stdout.write('\n')  # continue on next line
            raise
        signal.signal(signal.SIGINT, handler)
        if userinput == '':
            return parser(default)
        if parser(userinput) is not None:
            return parser(userinput)
        valid = str.join(', ', imap(lambda x: '"%s"' % x, choices[:-1]))
        logging.getLogger('user').critical(
            'Invalid input! Answer with %s or "%s"', valid, choices[-1])
示例#5
0
 def _getInternal(self,
                  desc,
                  obj2str,
                  str2obj,
                  def2obj,
                  option,
                  default_obj,
                  interactive=None,
                  interactiveDefault=True,
                  **kwargs):
     if (not interactive) or (option in self.getOptions()):
         return TypedConfigInterface._getInternal(self, desc, obj2str,
                                                  str2obj, def2obj, option,
                                                  default_obj, **kwargs)
     prompt = interactive
     if (default_obj != noDefault) and interactiveDefault:
         prompt += (' [%s]' %
                    self._getDefaultStr(default_obj, def2obj, obj2str))
     while True:
         handler = signal.signal(signal.SIGINT, signal.SIG_DFL)
         try:
             userInput = user_input('%s: ' % prompt)
         except Exception:
             sys.stdout.write('\n')
             sys.exit(os.EX_DATAERR)
         signal.signal(signal.SIGINT, handler)
         if userInput == '':
             obj = default_obj
         else:
             try:
                 obj = str2obj(userInput)
             except Exception:
                 raise ConfigError('Unable to parse %s: %s' %
                                   (desc, userInput))
         break
     return TypedConfigInterface._getInternal(self, desc, obj2str, str2obj,
                                              def2obj, option, obj,
                                              **kwargs)
	def _getInternal(self, desc, obj2str, str2obj, def2obj, option, default_obj,
			interactive = None, interactiveDefault = True, **kwargs):
		if (not interactive) or (option in self.getOptions()):
			return TypedConfigInterface._getInternal(self, desc, obj2str, str2obj, def2obj, option, default_obj, **kwargs)
		prompt = interactive
		if (default_obj != noDefault) and interactiveDefault:
			prompt += (' [%s]' % self._getDefaultStr(default_obj, def2obj, obj2str))
		while True:
			try:
				userInput = user_input('%s: ' % prompt)
			except Exception:
				sys.stdout.write('\n')
				sys.exit(os.EX_DATAERR)
			if userInput == '':
				obj = default_obj
			else:
				try:
					obj = str2obj(userInput)
				except Exception:
					raise UserError('Unable to parse %s: %s' % (desc, userInput))
					continue
			break
		return TypedConfigInterface._getInternal(self, desc, obj2str, str2obj, def2obj, option, obj, **kwargs)