Skip to content

Commit 975f04a

Browse files
committed
Added function to handle data transfer related fields in config
1 parent d90b585 commit 975f04a

File tree

1 file changed

+102
-9
lines changed

1 file changed

+102
-9
lines changed

src/murfey/cli/generate_config.py

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def is_type(value: str, instance: Type[Any] | tuple[Type[Any], ...]) -> bool:
274274
value_message = f"Please enter the {value_name}"
275275
while add_entry is True:
276276
# Add key
277-
key = (
277+
key = str(
278278
prompt(key_message, style="yellow").strip()
279279
if key_method is None
280280
else key_method(**key_method_args)
@@ -457,7 +457,7 @@ def add_calibrations(
457457
except ValidationError as error:
458458
if debug:
459459
console.print(error, style="red")
460-
console.print(f"Failed to validate {key!r}", style="red")
460+
console.print(f"Failed to validate {key!r}.", style="red")
461461
console.print("Returning an empty dictionary", style="red")
462462
return {}
463463

@@ -675,7 +675,7 @@ def add_data_directories(
675675
except ValidationError as error:
676676
if debug:
677677
console.print(error, style="red")
678-
console.print(f"Failed to validate {key!r}", style="red")
678+
console.print(f"Failed to validate {key!r}.", style="red")
679679
if ask_for_input(category, True) is True:
680680
return add_data_directories(key, field, debug)
681681
return {}
@@ -695,7 +695,7 @@ def add_create_directories(
695695
value_name="folder name",
696696
key_method=get_folder_name,
697697
key_method_args={
698-
"message": "Please enter the name Murfey should map the folder to.",
698+
"message": "Please enter the name Murfey should remember the folder as.",
699699
},
700700
value_method=get_folder_name,
701701
value_method_args={
@@ -714,7 +714,7 @@ def add_create_directories(
714714
except ValidationError as error:
715715
if debug:
716716
console.print(error, style="red")
717-
console.print(f"Failed to validate {key!r}", style="red")
717+
console.print(f"Failed to validate {key!r}.", style="red")
718718
if ask_for_input(category, True) is True:
719719
return add_create_directories(key, field, debug)
720720
return {}
@@ -728,6 +728,7 @@ def add_analyse_created_directories(
728728
"""
729729
print_field_info(field)
730730
category = "folder for Murfey to analyse"
731+
731732
folders_to_analyse: list[str] = construct_list(
732733
value_name=category,
733734
value_method=get_folder_name,
@@ -747,13 +748,108 @@ def add_analyse_created_directories(
747748
except ValidationError as error:
748749
if debug:
749750
console.print(error, style="red")
750-
console.print(f"Failed to validate {key!r}", style="red")
751+
console.print(f"Failed to validate {key!r}.", style="red")
751752
if ask_for_input(category, True) is True:
752753
return add_analyse_created_directories(key, field, debug)
753754
return []
754755

755756

756757
def set_up_data_transfer(config: dict, debug: bool = False) -> dict:
758+
"""
759+
Helper function to set up the data transfer fields in the configuration
760+
"""
761+
762+
def get_upstream_data_directories(
763+
key: str, field: ModelField, debug: bool = False
764+
) -> list[Path]:
765+
print_field_info(field)
766+
category = "upstream data directory"
767+
upstream_data_directories = construct_list(
768+
category,
769+
value_method=get_folder_path,
770+
value_method_args={
771+
"message": (
772+
"Please enter the full path to the data directory "
773+
"you wish to search for files in."
774+
),
775+
},
776+
allow_empty=False,
777+
allow_eval=False,
778+
many_types=False,
779+
restrict_to_types=Path,
780+
sort_values=True,
781+
)
782+
try:
783+
return validate_value(upstream_data_directories, key, field, debug)
784+
except ValidationError as error:
785+
if debug:
786+
console.print(error, style="red")
787+
console.print(f"Failed to validate {key!r}.", style="red")
788+
if ask_for_input(category, True) is True:
789+
return get_upstream_data_directories(key, field, debug)
790+
return []
791+
792+
def get_upstream_data_tiff_locations(
793+
key: str, field: ModelField, debug: bool = False
794+
) -> list[str]:
795+
print_field_info(field)
796+
category = "remote folder containing TIFF files"
797+
upstream_data_tiff_locations = construct_list(
798+
category,
799+
value_method=get_folder_name,
800+
value_method_args={
801+
"message": (
802+
"Please enter the name of the folder on the remote machines "
803+
"in which to search for TIFF files."
804+
)
805+
},
806+
allow_empty=False,
807+
allow_eval=False,
808+
many_types=False,
809+
restrict_to_types=str,
810+
sort_values=True,
811+
)
812+
try:
813+
return validate_value(upstream_data_tiff_locations, key, field, debug)
814+
except ValidationError as error:
815+
if debug:
816+
console.print(error, style="red")
817+
console.print(f"Failed to validate {key!r}.", style="red")
818+
if ask_for_input(category, True) is True:
819+
return get_upstream_data_tiff_locations(key, field, debug)
820+
return []
821+
822+
"""
823+
Start of set_up_data_transfer
824+
"""
825+
for key in (
826+
"data_transfer_enabled",
827+
"rsync_basepath",
828+
"rsync_module",
829+
"allow_removal",
830+
"upstream_data_directories",
831+
"upstream_data_download_directory",
832+
"upstream_data_tiff_locations",
833+
):
834+
field = MachineConfig.__fields__[key]
835+
# Use populate field to process simpler keys
836+
if key in (
837+
"data_transfer_enabled",
838+
"rsync_basepath",
839+
"rsync_module",
840+
"allow_removal",
841+
"upstream_data_download_directory",
842+
):
843+
validated_value = populate_field(key, field, debug)
844+
845+
# Construct more complicated data structures
846+
if key == "upstream_data_directories":
847+
validated_value = get_upstream_data_directories(key, field, debug)
848+
if key == "upstream_data_tiff_locations":
849+
validated_value = get_upstream_data_tiff_locations(key, field, debug)
850+
# Add to config
851+
config[key] = validated_value
852+
757853
return config
758854

759855

@@ -802,12 +898,10 @@ def set_up_machine_config(debug: bool = False):
802898
continue
803899
if key == "analyse_created_directories":
804900
new_config[key] = add_analyse_created_directories(key, field, debug)
805-
# TODO
806901
continue
807902

808903
# Data transfer block
809904
if key == "data_transfer_enabled":
810-
# TODO: Set up data transfer settings in a separate function
811905
new_config = set_up_data_transfer(new_config, debug)
812906
continue
813907
if key in (
@@ -855,7 +949,6 @@ def set_up_machine_config(debug: bool = False):
855949
"""
856950
Standard method of inputting values
857951
"""
858-
859952
new_config[key] = populate_field(key, field, debug)
860953

861954
# Validate the entire config again and convert into JSON/YAML-safe dict

0 commit comments

Comments
 (0)