cornsnake.util_input

Functions for handling user input with various formats and validations.

Documentation

  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: str, default: str) -> str:
 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: str, default: str) -> str:
 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: bool) -> str:
 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: str, default: list[str]) -> list[str]:
 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)
 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_required(question: str, default: str) -> str:
112    """
113    Function to get a required 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.
121    """
122    question = (
123        util_color.colorize(question, QUESTION_COLOR)
124        + f" [default is {default}]"
125        + prompt_token
126    )
127    while True:
128        answer = input_custom(question, default)
129        if len(answer) > 0:
130            return answer
131
132
133def input_branch_name_required(question: str, default: str) -> str:
134    """
135    Function to get a required branch name 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 branch name.
143    """
144    question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]"
145    while True:
146        answer = input_custom(question, default)
147        if answer and len(answer) > 0:
148            error = util_validate.check_is_branch_name_or_empty(answer)
149            if error:
150                print(error)
151            else:
152                return answer
153
154
155def input_required__dir_path(question: str, default: str) -> str:
156    """
157    Function to get a required directory path from user input.
158
159    Args:
160    question (str): The question to prompt the user.
161    default: The default value for the input.
162
163    Returns:
164    str: The user input directory path.
165    """
166    question = (
167        util_color.colorize(question, QUESTION_COLOR)
168        + f" [default is {default}]"
169        + prompt_token
170    )
171    while True:
172        answer = input_custom(question, default)
173        if len(answer) > 0:
174            if not os.path.isdir(answer):
175                print(" ! ERROR: That directory does not exist")
176            else:
177                return answer
178
179
180def input_with_format_y_or_n(question: str, default: bool) -> bool:
181    """
182    Function to get user input with Y or N format validation.
183
184    Args:
185    question (str): The question to prompt the user.
186    default: The default value for the input.
187
188    Returns:
189    bool: True for 'Y', False for 'N'.
190    """
191    default_str = boolToYorN(default)
192    question = (
193        util_color.colorize(question, QUESTION_COLOR)
194        + f" [Y or N] [default is {default_str}]"
195        + prompt_token
196    )
197    while True:
198        answer = input_custom(question, default_str)
199        if not config.IS_INTERACTIVE:
200            return default
201        if len(answer) == 0 and default is not None:
202            return default
203        if answer == "Y":
204            return True
205        if answer == "N":
206            return False
207
208
209def input_with_format_git_filter_repo_size(question: str, default: str) -> str:
210    """
211    Function to get user input with Git filter repo size format validation.
212
213    Args:
214    question (str): The question to prompt the user.
215    default: The default value for the input.
216
217    Returns:
218    str: The user input Git filter repo size.
219    """
220    question = (
221        util_color.colorize(question, QUESTION_COLOR)
222        + f"[Values are like 256K or 1M or 1G][Default = {default}]"
223        + prompt_token
224    )
225    while True:
226        answer = input_custom(question, default)
227        if len(answer) == 0:
228            return default
229        if util_validate.is_valid_blob_size(answer):
230            return answer
231
232
233def input_optional(question: str, default: str) -> str:
234    """
235    Function to get optional user input.
236
237    Args:
238    question (str): The question to prompt the user.
239    default: The default value for the input.
240
241    Returns:
242    str: The user input value.
243    """
244    question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]"
245    answer = input_custom(question, default)
246    return answer
prompt_token = ' >'
QUESTION_COLOR = '\x1b[96m'
def input_custom(question: str, default: str) -> str:
20def input_custom(question: str, default: str) -> str:
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.

def input_with_format_date(question: str, default: str) -> str:
39def input_with_format_date(question: str, default: str) -> str:
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.

def boolToYorN(value: bool) -> str:
61def boolToYorN(value: bool) -> str:
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.

def input_list_of_branches(question: str, default: list[str]) -> list[str]:
 76def input_list_of_branches(question: str, default: list[str]) -> list[str]:
 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)
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.

def input_required(question: str, default: str) -> str:
112def input_required(question: str, default: str) -> str:
113    """
114    Function to get a required 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.
122    """
123    question = (
124        util_color.colorize(question, QUESTION_COLOR)
125        + f" [default is {default}]"
126        + prompt_token
127    )
128    while True:
129        answer = input_custom(question, default)
130        if len(answer) > 0:
131            return answer

Function to get a required user input.

Args: question (str): The question to prompt the user. default: The default value for the input.

Returns: str: The user input.

def input_branch_name_required(question: str, default: str) -> str:
134def input_branch_name_required(question: str, default: str) -> str:
135    """
136    Function to get a required branch name 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 branch name.
144    """
145    question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]"
146    while True:
147        answer = input_custom(question, default)
148        if answer and len(answer) > 0:
149            error = util_validate.check_is_branch_name_or_empty(answer)
150            if error:
151                print(error)
152            else:
153                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.

def input_required__dir_path(question: str, default: str) -> str:
156def input_required__dir_path(question: str, default: str) -> str:
157    """
158    Function to get a required directory path from user input.
159
160    Args:
161    question (str): The question to prompt the user.
162    default: The default value for the input.
163
164    Returns:
165    str: The user input directory path.
166    """
167    question = (
168        util_color.colorize(question, QUESTION_COLOR)
169        + f" [default is {default}]"
170        + prompt_token
171    )
172    while True:
173        answer = input_custom(question, default)
174        if len(answer) > 0:
175            if not os.path.isdir(answer):
176                print(" ! ERROR: That directory does not exist")
177            else:
178                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.

def input_with_format_y_or_n(question: str, default: bool) -> bool:
181def input_with_format_y_or_n(question: str, default: bool) -> bool:
182    """
183    Function to get user input with Y or N format validation.
184
185    Args:
186    question (str): The question to prompt the user.
187    default: The default value for the input.
188
189    Returns:
190    bool: True for 'Y', False for 'N'.
191    """
192    default_str = boolToYorN(default)
193    question = (
194        util_color.colorize(question, QUESTION_COLOR)
195        + f" [Y or N] [default is {default_str}]"
196        + prompt_token
197    )
198    while True:
199        answer = input_custom(question, default_str)
200        if not config.IS_INTERACTIVE:
201            return default
202        if len(answer) == 0 and default is not None:
203            return default
204        if answer == "Y":
205            return True
206        if answer == "N":
207            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'.

def input_with_format_git_filter_repo_size(question: str, default: str) -> str:
210def input_with_format_git_filter_repo_size(question: str, default: str) -> str:
211    """
212    Function to get user input with Git filter repo size format validation.
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 Git filter repo size.
220    """
221    question = (
222        util_color.colorize(question, QUESTION_COLOR)
223        + f"[Values are like 256K or 1M or 1G][Default = {default}]"
224        + prompt_token
225    )
226    while True:
227        answer = input_custom(question, default)
228        if len(answer) == 0:
229            return default
230        if util_validate.is_valid_blob_size(answer):
231            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.

def input_optional(question: str, default: str) -> str:
234def input_optional(question: str, default: str) -> str:
235    """
236    Function to get optional user input.
237
238    Args:
239    question (str): The question to prompt the user.
240    default: The default value for the input.
241
242    Returns:
243    str: The user input value.
244    """
245    question = util_color.colorize(question, QUESTION_COLOR) + f"[Default = {default}]"
246    answer = input_custom(question, default)
247    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.