Skip to content

Commit 9d6679a

Browse files
committed
Replaced directory creation functions with the generic ones
1 parent 9b7f6de commit 9d6679a

File tree

1 file changed

+48
-102
lines changed

1 file changed

+48
-102
lines changed

src/murfey/cli/generate_config.py

Lines changed: 48 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ def get_folder_name(message: Optional[str] = None) -> str:
112112
continue
113113

114114

115-
def get_folder_path(
116-
message: Optional[str] = None, as_path: bool = True
117-
) -> str | Path | None:
115+
def get_folder_path(message: Optional[str] = None) -> Path | None:
118116
"""
119117
Helper function to interactively generate, validate, and return the full path
120118
to a folder.
@@ -128,17 +126,15 @@ def get_folder_path(
128126
return None
129127
try:
130128
path = Path(value).resolve()
131-
return path if as_path is True else path.as_posix()
129+
return path
132130
except Exception:
133131
console.print("Unable to resolve provided file path", style="red")
134132
if ask_for_input("file path", True) is False:
135133
return None
136134
continue
137135

138136

139-
def get_file_path(
140-
message: Optional[str] = None, as_path: bool = True
141-
) -> str | Path | None:
137+
def get_file_path(message: Optional[str] = None) -> Path | None:
142138
"""
143139
Helper function to interactively generate, validate, and return the full path
144140
to a file.
@@ -152,7 +148,7 @@ def get_file_path(
152148
return None
153149
file = Path(value).resolve()
154150
if file.suffix:
155-
return file if as_path is True else file.as_posix()
151+
return file
156152
console.print(f"{str(file)!r} doesn't appear to be a file", style="red")
157153
if ask_for_input("file", True) is False:
158154
return None
@@ -258,7 +254,7 @@ def construct_dict(
258254
debug: bool = False,
259255
) -> dict[str, Any]:
260256
"""
261-
Helper function to facilitate interative construction of a dictionary.
257+
Helper function to facilitate the interative construction of a dictionary.
262258
"""
263259

264260
def is_type(value: str, instance: Type[Any] | tuple[Type[Any], ...]) -> bool:
@@ -272,9 +268,6 @@ def is_type(value: str, instance: Type[Any] | tuple[Type[Any], ...]) -> bool:
272268
eval_value = value
273269
return isinstance(eval_value, instance)
274270

275-
"""
276-
Start of construct_dict
277-
"""
278271
dct: dict = {}
279272
add_entry = ask_for_input(dict_name, False)
280273
key_message = f"Please enter the {key_name}"
@@ -354,7 +347,7 @@ def is_type(value: str, instance: Type[Any] | tuple[Type[Any], ...]) -> bool:
354347

355348
def validate_value(value: Any, key: str, field: ModelField, debug: bool = False) -> Any:
356349
"""
357-
Helper function to validate the value of the desired field for a Pydantic model.
350+
Helper function to validate the value of a field in the Pydantic model.
358351
"""
359352
validated_value, errors = field.validate(value, {}, loc=key)
360353
if errors:
@@ -490,31 +483,14 @@ def get_software_name() -> str:
490483
return get_software_name()
491484
return ""
492485

493-
def ask_about_xml_path() -> bool:
486+
def ask_about_settings_file() -> bool:
494487
message = (
495488
"Does this software package have a settings file that needs modification? "
496489
"(y/n)"
497490
)
498491
return ask_for_permission(message)
499492

500-
def get_xml_file() -> Optional[Path]:
501-
message = (
502-
"What is the full file path of the settings file? This should be an "
503-
"XML file."
504-
)
505-
xml_file = Path(prompt(message, style="yellow").strip())
506-
# Validate
507-
if xml_file.suffix:
508-
return xml_file
509-
console.print(
510-
"The path entered does not point to a file.",
511-
style="red",
512-
)
513-
if ask_for_input("settings file", True) is True:
514-
return get_xml_file()
515-
return None
516-
517-
def get_xml_tree_path() -> str:
493+
def get_settings_tree_path() -> str:
518494
message = "What is the path through the XML file to the node to overwrite?"
519495
xml_tree_path = prompt(message, style="yellow").strip()
520496
# TODO: Currently no test cases for this method
@@ -579,12 +555,16 @@ def get_xml_tree_path() -> str:
579555
"provided.",
580556
style="italic bright_cyan",
581557
)
582-
if ask_about_xml_path() is True:
583-
xml_file = get_xml_file()
584-
xml_tree_path = get_xml_tree_path()
585-
else:
586-
xml_file = None
587-
xml_tree_path = ""
558+
settings_file: Optional[Path] = (
559+
get_file_path(
560+
"What is the full path to the settings file? This is usually an XML file."
561+
)
562+
if ask_about_settings_file() is True
563+
else None
564+
)
565+
settings_tree_path = (
566+
get_settings_tree_path().split("/") if settings_file else []
567+
)
588568

589569
# Collect extensions and filename substrings
590570
console.print(
@@ -597,7 +577,7 @@ def get_xml_tree_path() -> str:
597577
"for data processing. They are listed out here.",
598578
style="italic bright_cyan",
599579
)
600-
extensions_and_substrings = construct_dict(
580+
extensions_and_substrings: dict[str, list[str]] = construct_dict(
601581
dict_name="file extension configuration",
602582
key_name="file extension",
603583
value_name="file substrings",
@@ -620,8 +600,8 @@ def get_xml_tree_path() -> str:
620600
# Compile keys for this package as a dict
621601
package_info[name] = {
622602
"version": version,
623-
"xml_file": xml_file,
624-
"xml_tree_path": xml_tree_path,
603+
"settings_file": settings_file,
604+
"settings_tree_path": settings_tree_path,
625605
"extensions_and_substrings": extensions_and_substrings,
626606
}
627607
add_input = ask_for_input(category, again=True)
@@ -639,10 +619,10 @@ def get_xml_tree_path() -> str:
639619
acquisition_software.append(key)
640620
if package_info[key]["version"]:
641621
software_versions[key] = package_info[key]["version"]
642-
if package_info[key]["xml_file"]:
643-
software_settings_output_directories[str(package_info[key]["xml_file"])] = (
644-
package_info[key]["xml_tree_path"]
645-
)
622+
if package_info[key]["settings_file"]:
623+
software_settings_output_directories[
624+
str(package_info[key]["settings_file"])
625+
] = package_info[key]["settings_tree_path"]
646626
if package_info[key]["extensions_and_substrings"]:
647627
data_required_substrings[key] = package_info[key][
648628
"extensions_and_substrings"
@@ -704,45 +684,23 @@ def add_data_directories(
704684
def add_create_directories(
705685
key: str, field: ModelField, debug: bool = False
706686
) -> dict[str, str]:
707-
def get_folder() -> str:
708-
message = "Please enter the name of the folder for Murfey to create."
709-
answer = prompt(message, style="yellow").lower().strip()
710-
if bool(re.fullmatch(r"[\w\s\-]*", answer)) is False:
711-
console.print(
712-
"There are unsafe characters present in this folder name. Please "
713-
"use a different one.",
714-
style="red",
715-
)
716-
if ask_for_input("folder name", True) is True:
717-
return get_folder()
718-
return ""
719-
return answer
720-
721-
def get_folder_alias() -> str:
722-
message = "Please enter the name Murfey should map this folder to."
723-
answer = prompt(message, style="yellow").lower().strip()
724-
if bool(re.fullmatch(r"[\w\s\-]*", answer)) is False:
725-
console.print(
726-
"There are unsafe characters present in this folder name. Please "
727-
"use a different one.",
728-
style="red",
729-
)
730-
if ask_for_input("folder alias", True) is True:
731-
return get_folder_alias()
732-
return ""
733-
return answer
734-
735687
"""
736-
Start of add_create_directories
688+
Function to populate the create_directories field.
737689
"""
738690
print_field_info(field)
739691
category = "folder for Murfey to create"
740692
folders_to_create: dict[str, str] = construct_dict(
741693
dict_name=category,
742694
key_name="folder alias",
743695
value_name="folder name",
744-
key_method=get_folder_alias,
745-
value_method=get_folder,
696+
key_method=get_folder_name,
697+
key_method_args={
698+
"message": "Please enter the name Murfey should map the folder to.",
699+
},
700+
value_method=get_folder_name,
701+
value_method_args={
702+
"message": "Please enter the name of the folder for Murfey to create.",
703+
},
746704
allow_empty_key=False,
747705
allow_empty_value=False,
748706
allow_eval=False,
@@ -765,35 +723,23 @@ def get_folder_alias() -> str:
765723
def add_analyse_created_directories(
766724
key: str, field: ModelField, debug: bool = False
767725
) -> list[str]:
768-
def get_folder() -> str:
769-
message = "Please enter the name of the folder that Murfey is to analyse."
770-
answer = prompt(message, style="yellow").lower().strip()
771-
if bool(re.fullmatch(r"[\w\s\-]*", answer)) is False:
772-
console.print(
773-
"There are unsafe characters present in the folder name. Please "
774-
"use a different folder.",
775-
style="red",
776-
)
777-
if ask_for_input("folder name", True) is True:
778-
return get_folder()
779-
return ""
780-
return answer
781-
782726
"""
783-
Start of add_analyse_created_directories
727+
Function to populate the analyse_created_directories field
784728
"""
785-
folders_to_analyse: list[str] = []
729+
print_field_info(field)
786730
category = "folder for Murfey to analyse"
787-
add_folder = ask_for_input(category, False)
788-
while add_folder is True:
789-
folder_name = get_folder()
790-
if not folder_name:
791-
console.print("No folder name provided", style="red")
792-
add_folder = ask_for_input(category, True)
793-
continue
794-
folders_to_analyse.append(folder_name)
795-
add_folder = ask_for_input(category, True)
796-
continue
731+
folders_to_analyse: list[str] = construct_list(
732+
value_name=category,
733+
value_method=get_folder_name,
734+
value_method_args={
735+
"message": "Please enter the name of the folder that Murfey is to analyse."
736+
},
737+
allow_empty=False,
738+
allow_eval=False,
739+
many_types=False,
740+
restrict_to_types=str,
741+
sort_values=True,
742+
)
797743

798744
# Validate and return
799745
try:

0 commit comments

Comments
 (0)