55import re
66from ast import literal_eval
77from pathlib import Path
8- from typing import Any , Optional , Type , get_type_hints
8+ from typing import Any , Callable , Optional , Type , get_type_hints
99
1010import yaml
1111from pydantic import ValidationError
@@ -95,7 +95,8 @@ def confirm_duplicate(value: str):
9595
9696def construct_list (
9797 value_name : str ,
98- prompt_message : str ,
98+ value_method : Optional [Callable ] = None ,
99+ value_method_args : dict = {},
99100 allow_empty : bool = False ,
100101 allow_eval : bool = True ,
101102 many_types : bool = True ,
@@ -108,9 +109,17 @@ def construct_list(
108109 """
109110 lst : list = []
110111 add_entry = ask_for_input (value_name , False )
111- message = prompt_message
112112 while add_entry is True :
113- value = prompt (message , style = "yellow" ).strip ()
113+ value = (
114+ prompt (
115+ "Please enter "
116+ + ("an" if value_name .startswith (("a" , "e" , "i" , "o" , "u" )) else "a" )
117+ + f" { value_name } " ,
118+ style = "yellow" ,
119+ )
120+ if value_method is None
121+ else value_method (** value_method_args )
122+ )
114123 # Reject empty inputs if set
115124 if not value and not allow_empty :
116125 console .print ("No value provided." , style = "red" )
@@ -172,6 +181,10 @@ def construct_dict(
172181 key_name : str ,
173182 value_name : str ,
174183 allow_empty_key : bool = True ,
184+ key_method : Optional [Callable ] = None ,
185+ key_method_args : dict = {},
186+ value_method : Optional [Callable ] = None ,
187+ value_method_args : dict = {},
175188 allow_empty_value : bool = True ,
176189 allow_eval : bool = True ,
177190 sort_keys : bool = True ,
@@ -201,18 +214,28 @@ def is_type(value: str, instance: Type[Any] | tuple[Type[Any], ...]) -> bool:
201214 key_message = f"Please enter the { key_name } "
202215 value_message = f"Please enter the { value_name } "
203216 while add_entry is True :
204- key = prompt (key_message , style = "yellow" ).strip ().lower ()
217+ # Add key
218+ key = (
219+ prompt (key_message , style = "yellow" ).strip ()
220+ if key_method is None
221+ else key_method (** key_method_args )
222+ )
205223 # Reject empty keys if set
206224 if not allow_empty_key and not key :
207- console .print (f"No { key_name } provided." )
225+ console .print (f"No { key_name } provided." , style = "red" )
208226 add_entry = ask_for_input (dict_name , True )
209227 continue
210228 # Confirm overwrite key on duplicate
211229 if key in dct .keys ():
212230 if confirm_overwrite (key ) is False :
213231 add_entry = ask_for_input (dict_name , True )
214232 continue
215- value = prompt (value_message , style = "yellow" ).strip ()
233+ # Add value
234+ value = (
235+ prompt (value_message , style = "yellow" ).strip ()
236+ if value_method is None
237+ else value_method (** value_method_args )
238+ )
216239 # Reject empty values if set
217240 if not allow_empty_value and not value :
218241 console .print ("No value provided" , style = "red" )
@@ -465,7 +488,6 @@ def get_file_extension() -> str:
465488 continue
466489 substrings : list [str ] = construct_list (
467490 "file substring" ,
468- "Please enter a file substring associated with this extension" ,
469491 allow_empty = False ,
470492 allow_eval = False ,
471493 many_types = False ,
0 commit comments