Skip to content

Commit 422d1f5

Browse files
committed
Added function to set up folders to analyse; added validation to setup functions
1 parent 71de649 commit 422d1f5

File tree

1 file changed

+82
-26
lines changed

1 file changed

+82
-26
lines changed

src/murfey/cli/generate_config.py

Lines changed: 82 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,6 @@ def get_calibration():
197197

198198

199199
def add_software_packages(config: dict, debug: bool = False) -> dict[str, Any]:
200-
def ask_about_xml_path() -> bool:
201-
message = (
202-
"Does this software package have a settings file that needs modification? "
203-
"(y/n)"
204-
)
205-
answer = prompt(message, style="yellow").lower().strip()
206-
207-
# Validate
208-
if answer in ("y", "yes"):
209-
return True
210-
if answer in ("n", "no"):
211-
return False
212-
console.print("Invalid input.", style="red")
213-
return ask_about_xml_path()
214-
215200
def get_software_name() -> str:
216201
name = (
217202
prompt(
@@ -235,6 +220,21 @@ def get_software_name() -> str:
235220
return get_software_name()
236221
return ""
237222

223+
def ask_about_xml_path() -> bool:
224+
message = (
225+
"Does this software package have a settings file that needs modification? "
226+
"(y/n)"
227+
)
228+
answer = prompt(message, style="yellow").lower().strip()
229+
230+
# Validate
231+
if answer in ("y", "yes"):
232+
return True
233+
if answer in ("n", "no"):
234+
return False
235+
console.print("Invalid input.", style="red")
236+
return ask_about_xml_path()
237+
238238
def get_xml_file() -> Optional[Path]:
239239
xml_file = Path(
240240
prompt(
@@ -525,7 +525,7 @@ def get_directory_type():
525525
console.print(f"Validated {key!r} successfully", style="bright_green")
526526
if debug:
527527
console.print(f"{type(validated_data_directories)}")
528-
console.print(f"{validated_data_directories}")
528+
console.print(f"{validated_data_directories!r}")
529529
return data_directories
530530
console.print(f"Failed to validate {key!r}", style="red")
531531
if ask_for_input(category, True) is True:
@@ -537,10 +537,7 @@ def add_create_directories(
537537
key: str, field: ModelField, debug: bool = False
538538
) -> dict[str, str]:
539539
def get_folder() -> str:
540-
message = (
541-
"Please input the name of the folder for Murfey to create. Press Enter "
542-
"to skip this."
543-
)
540+
message = "Please enter the name of the folder for Murfey to create."
544541
answer = prompt(message, style="yellow").lower().strip()
545542
if bool(re.fullmatch(r"[\w\s\-]*", answer)) is False:
546543
console.print(
@@ -554,7 +551,7 @@ def get_folder() -> str:
554551
return answer
555552

556553
def get_folder_alias() -> str:
557-
message = "Please enter the name you want to map this folder to within Murfey."
554+
message = "Please enter the name Murfey should map this folder to."
558555
answer = prompt(message, style="yellow").lower().strip()
559556
if bool(re.fullmatch(r"[\w\s\-]*", answer)) is False:
560557
console.print(
@@ -571,8 +568,8 @@ def get_folder_alias() -> str:
571568
Start of add_create_directories
572569
"""
573570
print_field_info(field)
574-
directories_to_create: dict[str, str] = {}
575-
category = "directory for Murfey to create"
571+
folders_to_create: dict[str, str] = {}
572+
category = "folder for Murfey to create"
576573
add_directory: bool = ask_for_input(category, False)
577574
while add_directory is True:
578575
folder_name = get_folder()
@@ -591,10 +588,69 @@ def get_folder_alias() -> str:
591588
)
592589
add_directory = ask_for_input(category, True)
593590
continue
594-
directories_to_create[folder_alias] = folder_name
591+
folders_to_create[folder_alias] = folder_name
595592
add_directory = ask_for_input(category, True)
596593
continue
597-
return directories_to_create
594+
595+
# Validate and return
596+
validated_folders, errors = field.validate(folders_to_create, {}, loc=key)
597+
if not errors:
598+
console.print(f"{key!r} validated successfully", style="bright_green")
599+
if debug:
600+
console.print(f"{type(validated_folders)}", style="bright_green")
601+
console.print(f"{validated_folders!r}", style="bright_green")
602+
return folders_to_create
603+
console.print(f"Failed to validate {key!r}")
604+
if ask_for_input(category, True) is True:
605+
return add_create_directories(key, field, debug)
606+
return {}
607+
608+
609+
def add_analyse_created_directories(
610+
key: str, field: ModelField, debug: bool = False
611+
) -> list[str]:
612+
def get_folder() -> str:
613+
message = "Please enter the name of the folder that Murfey is to analyse."
614+
answer = prompt(message, style="yellow").lower().strip()
615+
if bool(re.fullmatch(r"[\w\s\-]*", answer)) is False:
616+
console.print(
617+
"There are unsafe characters present in the folder name. Please "
618+
"use a different folder.",
619+
style="red",
620+
)
621+
if ask_for_input("folder name", True) is True:
622+
return get_folder()
623+
return ""
624+
return answer
625+
626+
"""
627+
Start of add_analyse_created_directories
628+
"""
629+
folders_to_create: list[str] = []
630+
category = "folder for Murfey to analyse"
631+
add_folder = ask_for_input(category, False)
632+
while add_folder is True:
633+
folder_name = get_folder()
634+
if not folder_name:
635+
console.print("No folder name provided", style="red")
636+
add_folder = ask_for_input(category, True)
637+
continue
638+
folders_to_create.append(folder_name)
639+
add_folder = ask_for_input(category, True)
640+
continue
641+
642+
# Validate and return
643+
validated_folders, errors = field.validate(folders_to_create, {}, loc=key)
644+
if not errors:
645+
console.print(f"{key!r} validated successfully", style="bright_green")
646+
if debug:
647+
console.print(f"{type(validated_folders)}", style="bright_green")
648+
console.print(f"{validated_folders!r}", style="bright_green")
649+
return sorted(validated_folders)
650+
console.print(f"Failed to validate {key!r}", style="red")
651+
if ask_for_input(category, True) is True:
652+
return add_analyse_created_directories(key, field, debug)
653+
return []
598654

599655

600656
def set_up_data_transfer(config: dict, debug: bool = False) -> dict:
@@ -643,9 +699,9 @@ def set_up_machine_config(debug: bool = False):
643699
continue
644700
if key == "create_directories":
645701
new_config[key] = add_create_directories(key, field, debug)
646-
# TODO
647702
continue
648703
if key == "analyse_created_directories":
704+
new_config[key] = add_analyse_created_directories(key, field, debug)
649705
# TODO
650706
continue
651707

0 commit comments

Comments
 (0)