cornsnake.util_input
Functions for handling user input with various formats and validations.
1""" 2Functions for handling user input with various formats and validations. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_input.html) 5""" 6 7import os 8 9from . import config 10from . import util_color 11from . import util_validate 12from . import util_date 13 14prompt_token = " >" 15 16QUESTION_COLOR = util_color.bcolors.OKCYAN 17 18 19def input_custom(question, default): 20 """ 21 Function to get user input with default value. 22 23 Args: 24 question (str): The question to prompt the user. 25 default: The default value for the input. 26 27 Returns: 28 str: The user input value. 29 """ 30 if isinstance(default, str): 31 default = default.strip() 32 if not config.IS_INTERACTIVE: 33 print(question + str(default)) 34 return default 35 return input(question).strip() or default 36 37 38def input_with_format_date(question, default): 39 """ 40 Function to get user input with date format validation. 41 42 Args: 43 question (str): The question to prompt the user. 44 default: The default value for the input. 45 46 Returns: 47 str: The user input date value. 48 """ 49 question = ( 50 util_color.colorize(question, QUESTION_COLOR) 51 + f" [format = yyyy-mm-dd] [default is {default}]" 52 + prompt_token 53 ) 54 while True: 55 answer = input_custom(question, default) 56 if util_date.is_valid_date_yyyy_mm_dd(answer): 57 return answer 58 59 60def boolToYorN(value): 61 """ 62 Function to convert boolean value to Y or N. 63 64 Args: 65 value: Boolean value to convert. 66 67 Returns: 68 str: 'Y' if True, 'N' if False. 69 """ 70 if value: 71 return "Y" 72 return "N" 73 74 75def input_list_of_branches(question, default): 76 """ 77 Function to get a list of branch names from user input. 78 79 Args: 80 question (str): The question to prompt the user. 81 default: The default value for the input. 82 83 Returns: 84 list: List of branch names. 85 """ 86 if not config.IS_INTERACTIVE: 87 return default 88 items = [] 89 question = ( 90 util_color.colorize(question, QUESTION_COLOR) 91 + f" [default is {default}]" 92 + prompt_token 93 ) 94 is_valid = False 95 while not is_valid: 96 answer = input_custom(question, "") 97 if len(answer) > 0: 98 error = util_validate.check_is_branch_name_or_empty(answer, "branch name") 99 if error: 100 print(error) 101 else: 102 items.append(answer) 103 elif len(items) > 0: 104 is_valid = True 105 elif len(default) > 0: 106 items = default 107 is_valid = True 108 return items 109 110 111def input_branch_name_required(question, default): 112 """ 113 Function to get a required branch name from user input. 114 115 Args: 116 question (str): The question to prompt the user. 117 default: The default value for the input. 118 119 Returns: 120 str: The user input branch name. 121 """ 122 question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]" 123 while True: 124 answer = input_custom(question, default) 125 if answer and len(answer) > 0: 126 error = util_validate.check_is_branch_name_or_empty(answer, "branch name") 127 if error: 128 print(error) 129 else: 130 return answer 131 132 133def input_required__dir_path(question, default): 134 """ 135 Function to get a required directory path from user input. 136 137 Args: 138 question (str): The question to prompt the user. 139 default: The default value for the input. 140 141 Returns: 142 str: The user input directory path. 143 """ 144 question = ( 145 util_color.colorize(question, QUESTION_COLOR) 146 + f" [default is {default}]" 147 + prompt_token 148 ) 149 while True: 150 answer = input_custom(question, default) 151 if len(answer) > 0: 152 if not os.path.isdir(answer): 153 print(" ! ERROR: That directory does not exist") 154 else: 155 return answer 156 157 158def input_with_format_y_or_n(question, default): 159 """ 160 Function to get user input with Y or N format validation. 161 162 Args: 163 question (str): The question to prompt the user. 164 default: The default value for the input. 165 166 Returns: 167 bool: True for 'Y', False for 'N'. 168 """ 169 question = ( 170 util_color.colorize(question, QUESTION_COLOR) 171 + f" [Y or N] [default is {boolToYorN(default)}]" 172 + prompt_token 173 ) 174 while True: 175 answer = input_custom(question, default) 176 if not config.IS_INTERACTIVE: 177 return answer 178 if len(answer) == 0 and default is not None: 179 return default 180 if answer == "Y": 181 return True 182 if answer == "N": 183 return False 184 185 186def input_with_format_git_filter_repo_size(question, default): 187 """ 188 Function to get user input with Git filter repo size format validation. 189 190 Args: 191 question (str): The question to prompt the user. 192 default: The default value for the input. 193 194 Returns: 195 str: The user input Git filter repo size. 196 """ 197 question = ( 198 util_color.colorize(question, QUESTION_COLOR) 199 + f"[Values are like 256K or 1M or 1G][Default = {default}]" 200 + prompt_token 201 ) 202 while True: 203 answer = input_custom(question, default) 204 if len(answer) == 0: 205 return default 206 if util_validate.is_valid_blob_size(answer): 207 return answer 208 209 210def input_optional(question, default): 211 """ 212 Function to get optional user input. 213 214 Args: 215 question (str): The question to prompt the user. 216 default: The default value for the input. 217 218 Returns: 219 str: The user input value. 220 """ 221 question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]" 222 answer = input_custom(question, default) 223 return answer
20def input_custom(question, default): 21 """ 22 Function to get user input with default value. 23 24 Args: 25 question (str): The question to prompt the user. 26 default: The default value for the input. 27 28 Returns: 29 str: The user input value. 30 """ 31 if isinstance(default, str): 32 default = default.strip() 33 if not config.IS_INTERACTIVE: 34 print(question + str(default)) 35 return default 36 return input(question).strip() or default
Function to get user input with default value.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: str: The user input value.
39def input_with_format_date(question, default): 40 """ 41 Function to get user input with date format validation. 42 43 Args: 44 question (str): The question to prompt the user. 45 default: The default value for the input. 46 47 Returns: 48 str: The user input date value. 49 """ 50 question = ( 51 util_color.colorize(question, QUESTION_COLOR) 52 + f" [format = yyyy-mm-dd] [default is {default}]" 53 + prompt_token 54 ) 55 while True: 56 answer = input_custom(question, default) 57 if util_date.is_valid_date_yyyy_mm_dd(answer): 58 return answer
Function to get user input with date format validation.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: str: The user input date value.
61def boolToYorN(value): 62 """ 63 Function to convert boolean value to Y or N. 64 65 Args: 66 value: Boolean value to convert. 67 68 Returns: 69 str: 'Y' if True, 'N' if False. 70 """ 71 if value: 72 return "Y" 73 return "N"
Function to convert boolean value to Y or N.
Args: value: Boolean value to convert.
Returns: str: 'Y' if True, 'N' if False.
76def input_list_of_branches(question, default): 77 """ 78 Function to get a list of branch names from user input. 79 80 Args: 81 question (str): The question to prompt the user. 82 default: The default value for the input. 83 84 Returns: 85 list: List of branch names. 86 """ 87 if not config.IS_INTERACTIVE: 88 return default 89 items = [] 90 question = ( 91 util_color.colorize(question, QUESTION_COLOR) 92 + f" [default is {default}]" 93 + prompt_token 94 ) 95 is_valid = False 96 while not is_valid: 97 answer = input_custom(question, "") 98 if len(answer) > 0: 99 error = util_validate.check_is_branch_name_or_empty(answer, "branch name") 100 if error: 101 print(error) 102 else: 103 items.append(answer) 104 elif len(items) > 0: 105 is_valid = True 106 elif len(default) > 0: 107 items = default 108 is_valid = True 109 return items
Function to get a list of branch names from user input.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: list: List of branch names.
112def input_branch_name_required(question, default): 113 """ 114 Function to get a required branch name from user input. 115 116 Args: 117 question (str): The question to prompt the user. 118 default: The default value for the input. 119 120 Returns: 121 str: The user input branch name. 122 """ 123 question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]" 124 while True: 125 answer = input_custom(question, default) 126 if answer and len(answer) > 0: 127 error = util_validate.check_is_branch_name_or_empty(answer, "branch name") 128 if error: 129 print(error) 130 else: 131 return answer
Function to get a required branch name from user input.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: str: The user input branch name.
134def input_required__dir_path(question, default): 135 """ 136 Function to get a required directory path from user input. 137 138 Args: 139 question (str): The question to prompt the user. 140 default: The default value for the input. 141 142 Returns: 143 str: The user input directory path. 144 """ 145 question = ( 146 util_color.colorize(question, QUESTION_COLOR) 147 + f" [default is {default}]" 148 + prompt_token 149 ) 150 while True: 151 answer = input_custom(question, default) 152 if len(answer) > 0: 153 if not os.path.isdir(answer): 154 print(" ! ERROR: That directory does not exist") 155 else: 156 return answer
Function to get a required directory path from user input.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: str: The user input directory path.
159def input_with_format_y_or_n(question, default): 160 """ 161 Function to get user input with Y or N format validation. 162 163 Args: 164 question (str): The question to prompt the user. 165 default: The default value for the input. 166 167 Returns: 168 bool: True for 'Y', False for 'N'. 169 """ 170 question = ( 171 util_color.colorize(question, QUESTION_COLOR) 172 + f" [Y or N] [default is {boolToYorN(default)}]" 173 + prompt_token 174 ) 175 while True: 176 answer = input_custom(question, default) 177 if not config.IS_INTERACTIVE: 178 return answer 179 if len(answer) == 0 and default is not None: 180 return default 181 if answer == "Y": 182 return True 183 if answer == "N": 184 return False
Function to get user input with Y or N format validation.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: bool: True for 'Y', False for 'N'.
187def input_with_format_git_filter_repo_size(question, default): 188 """ 189 Function to get user input with Git filter repo size format validation. 190 191 Args: 192 question (str): The question to prompt the user. 193 default: The default value for the input. 194 195 Returns: 196 str: The user input Git filter repo size. 197 """ 198 question = ( 199 util_color.colorize(question, QUESTION_COLOR) 200 + f"[Values are like 256K or 1M or 1G][Default = {default}]" 201 + prompt_token 202 ) 203 while True: 204 answer = input_custom(question, default) 205 if len(answer) == 0: 206 return default 207 if util_validate.is_valid_blob_size(answer): 208 return answer
Function to get user input with Git filter repo size format validation.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: str: The user input Git filter repo size.
211def input_optional(question, default): 212 """ 213 Function to get optional user input. 214 215 Args: 216 question (str): The question to prompt the user. 217 default: The default value for the input. 218 219 Returns: 220 str: The user input value. 221 """ 222 question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]" 223 answer = input_custom(question, default) 224 return answer
Function to get optional user input.
Args: question (str): The question to prompt the user. default: The default value for the input.
Returns: str: The user input value.