Skip to content

Commit 436fab7

Browse files
committed
Added logic to use default values for fields if empty console input is received; updated 'data_directories' creation function to reflect change in MachineConfig
1 parent aab451f commit 436fab7

File tree

1 file changed

+81
-42
lines changed

1 file changed

+81
-42
lines changed

src/murfey/cli/create_config.py

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ def ask_for_input(parameter: str, again: bool = False):
103103
return ask_for_permission(message)
104104

105105

106+
def ask_to_use_default(field: ModelField):
107+
"""
108+
Asks the user if they want to populate the current configuration key with the
109+
default value.
110+
"""
111+
message = (
112+
"Would you like to use the default value for this field? \n"
113+
f"Default: {field.field_info.default!r} \n"
114+
"[bold bright_magenta](y/n)[/bold bright_magenta]"
115+
)
116+
return ask_for_permission(message)
117+
118+
106119
def confirm_overwrite(value: str):
107120
"""
108121
Asks the user if a value that already exists should be overwritten.
@@ -410,8 +423,16 @@ def populate_field(key: str, field: ModelField, debug: bool = False) -> Any:
410423
# Get value
411424
answer = prompt(message, style="bright_yellow")
412425

413-
# Convert empty console input into default values
414-
value = field.field_info.default if not answer else answer
426+
# Convert empty console input into default values if they are None-like
427+
if not field.field_info.default:
428+
value = field.field_info.default if not answer else answer
429+
# Convert inverted commas into empty strings if defaults are not None-like
430+
else:
431+
value = (
432+
""
433+
if answer in ("''", '""') and isinstance(field.field_info.default, str)
434+
else answer
435+
)
415436

416437
# Validate and return
417438
try:
@@ -691,21 +712,27 @@ def get_settings_tree_path() -> str:
691712

692713
def add_data_directories(
693714
key: str, field: ModelField, debug: bool = False
694-
) -> dict[str, str]:
715+
) -> list[Path]:
695716
"""
696717
Function to facilitate populating the data_directories field.
697718
"""
698719
print_field_info(field)
699-
category = "data directory"
700-
data_directories: dict[str, str] = construct_dict(
701-
category,
702-
"full file path to the data directory",
703-
"data type",
704-
allow_empty_key=False,
705-
allow_empty_value=False,
720+
description = "data directory path"
721+
data_directories: list[Path] = construct_list(
722+
description,
723+
value_method=get_folder_path,
724+
value_method_args={
725+
"message": (
726+
"Please enter the full path to the data directory "
727+
"where the files are stored."
728+
),
729+
},
730+
allow_empty=False,
706731
allow_eval=False,
707-
sort_keys=True,
708-
restrict_to_types=str,
732+
many_types=False,
733+
restrict_to_types=Path,
734+
sort_values=True,
735+
debug=debug,
709736
)
710737

711738
# Validate and return
@@ -715,10 +742,10 @@ def add_data_directories(
715742
if debug:
716743
console.print(error, style="bright_red")
717744
console.print(f"Failed to validate {key!r}.", style="bright_red")
718-
if ask_for_input(category, True) is True:
745+
if ask_for_input(description, True) is True:
719746
return add_data_directories(key, field, debug)
720747
console.print("Returning an empty dictionary.", style="bright_red")
721-
return {}
748+
return []
722749

723750

724751
def add_create_directories(
@@ -728,24 +755,30 @@ def add_create_directories(
728755
Function to populate the create_directories field.
729756
"""
730757
print_field_info(field)
731-
category = "folder for Murfey to create"
732-
folders_to_create: dict[str, str] = construct_dict(
733-
dict_name=category,
734-
key_name="folder alias",
735-
value_name="folder name",
736-
key_method=get_folder_name,
737-
key_method_args={
738-
"message": "Please enter the name Murfey should remember the folder as.",
739-
},
740-
value_method=get_folder_name,
741-
value_method_args={
742-
"message": "Please enter the name of the folder for Murfey to create.",
743-
},
744-
allow_empty_key=False,
745-
allow_empty_value=False,
746-
allow_eval=False,
747-
sort_keys=True,
748-
restrict_to_types=str,
758+
759+
# Manually enter fields if default value is not used
760+
description = "folder for Murfey to create"
761+
folders_to_create: dict[str, str] = (
762+
field.field_info.default
763+
if ask_to_use_default(field) is True
764+
else construct_dict(
765+
dict_name=description,
766+
key_name="folder alias",
767+
value_name="folder name",
768+
key_method=get_folder_name,
769+
key_method_args={
770+
"message": "Please enter the name Murfey should remember the folder as.",
771+
},
772+
value_method=get_folder_name,
773+
value_method_args={
774+
"message": "Please enter the name of the folder for Murfey to create.",
775+
},
776+
allow_empty_key=False,
777+
allow_empty_value=False,
778+
allow_eval=False,
779+
sort_keys=True,
780+
restrict_to_types=str,
781+
)
749782
)
750783

751784
# Validate and return
@@ -755,7 +788,7 @@ def add_create_directories(
755788
if debug:
756789
console.print(error, style="bright_red")
757790
console.print(f"Failed to validate {key!r}.", style="bright_red")
758-
if ask_for_input(category, True) is True:
791+
if ask_for_input(description, True) is True:
759792
return add_create_directories(key, field, debug)
760793
console.print("Returning an empty dictionary.", style="bright_red")
761794
return {}
@@ -901,16 +934,22 @@ def set_up_data_processing(config: dict, debug: bool = False) -> dict:
901934

902935
def add_recipes(key: str, field: ModelField, debug: bool = False) -> dict[str, str]:
903936
print_field_info(field)
937+
938+
# Manually construct the dictionary if the default value is not used
904939
category = "processing recipe"
905-
recipes = construct_dict(
906-
category,
907-
key_name="name of the recipe",
908-
value_name="name of the recipe file",
909-
allow_empty_key=False,
910-
allow_empty_value=False,
911-
allow_eval=False,
912-
sort_keys=True,
913-
restrict_to_types=str,
940+
recipes = (
941+
field.field_info.default
942+
if ask_to_use_default(field) is True
943+
else construct_dict(
944+
category,
945+
key_name="name of the recipe",
946+
value_name="name of the recipe file",
947+
allow_empty_key=False,
948+
allow_empty_value=False,
949+
allow_eval=False,
950+
sort_keys=True,
951+
restrict_to_types=str,
952+
)
914953
)
915954
try:
916955
return validate_value(recipes, key, field, debug)

0 commit comments

Comments
 (0)