MCPcopy Index your code
hub / github.com/Tencent/CodeAnalysis / SmartInput

Class SmartInput

client/util/smartinput.py:20–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18
19
20class SmartInput(object):
21 def __base_input(self, prompt, is_password):
22 """
23 基础的输入,带密码掩码功能
24 :param prompt: 提示信息
25 :param is_password: 是否是密码输入
26 :return: 输入的数据
27 """
28 if is_password:
29 input_data = getpass.getpass(prompt)
30 else:
31 input_data = input(prompt)
32 return input_data
33
34 def __error_retry_input(self, input_data, check_func, retry_prompt, retry_times, is_password=False):
35 """
36 检查输入,如果输入有误,重新提示输入,直到输入正确,或超过最多重试次数
37 :param input_data: 输入数据
38 :param check_func: 检查回调函数,返回True(输入正确)|False(输入有误)
39 :param retry_prompt: 重试输入的提示信息
40 :param retry_times: 重试总次数,默认可以重试2次
41 :param is_password: 是否是密码输入
42 :return:
43 """
44 if check_func(input_data):
45 return input_data
46 else: # 不符合要求,需要重试
47 retry_cnt = 1
48 while retry_cnt <= retry_times:
49 input_data = self.__base_input(retry_prompt, is_password)
50 if check_func(input_data):
51 return input_data
52 retry_cnt += 1
53 logger.error("%s次输入错误,退出!" % (retry_times+1))
54 raise InputRetryError("%s次输入错误,退出!" % (retry_times+1))
55
56 def input(self, prompt, check_func=None, retry_prompt=None, retry_times=2, is_password=False):
57 """
58 提示用户输入,可以设置错误重试次数,可以设置密码掩码输入,不在屏幕显示实时输入的信息
59 :param prompt: 首次输入提示信息
60 :param timeout: 每次输入的超时值(秒),默认10s
61 :param check_func: 检查回调函数,返回True(输入正确)|False(输入有误)
62 :param retry_prompt: 重试输入的提示信息
63 :param retry_times: 允许重试的最大次数, 默认可以重试2次
64 :param is_password: 是否是密码输入
65 :return:
66 """
67 data = self.__base_input(prompt, is_password)
68 if check_func and retry_prompt and retry_times:
69 data = self.__error_retry_input(data, check_func, retry_prompt, retry_times, is_password)
70 return data

Callers 6

input_tokenMethod · 0.90
input_proj_idMethod · 0.90
input_source_dirMethod · 0.90
input_scm_typeMethod · 0.90
input_user_infoMethod · 0.90
input_languagesMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected