|
| 1 | +from fnmatch import fnmatch |
| 2 | +from pathlib import Path |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from cirro_api_client.v1.models import Dataset, Project |
| 6 | + |
| 7 | +from cirro.cli.interactive.common_args import ask_project, ask_dataset |
| 8 | +from cirro.cli.interactive.utils import ask, prompt_wrapper, InputError |
| 9 | +from cirro.cli.models import DownloadArguments |
| 10 | +from cirro.models.file import File |
| 11 | + |
| 12 | + |
| 13 | +def ask_dataset_files(files: List[File]) -> List[File]: |
| 14 | + """Get the list of files which the user would like to download from the dataset.""" |
| 15 | + |
| 16 | + choices = [ |
| 17 | + "Download all files", |
| 18 | + "Select files from a list", |
| 19 | + "Select files with a naming pattern (glob)" |
| 20 | + ] |
| 21 | + |
| 22 | + selection_mode_prompt = { |
| 23 | + 'type': 'select', |
| 24 | + 'name': 'mode', |
| 25 | + 'message': 'Which files would you like to download from this dataset?', |
| 26 | + 'choices': choices |
| 27 | + } |
| 28 | + |
| 29 | + answers = prompt_wrapper(selection_mode_prompt) |
| 30 | + |
| 31 | + if answers['mode'] == choices[0]: |
| 32 | + return files |
| 33 | + elif answers['mode'] == choices[1]: |
| 34 | + return ask_dataset_files_list(files) |
| 35 | + else: |
| 36 | + return ask_dataset_files_glob(files) |
| 37 | + |
| 38 | + |
| 39 | +def strip_prefix(fp: str, prefix: str): |
| 40 | + assert fp.startswith(prefix), f"Expected {fp} to start with {prefix}" |
| 41 | + return fp[len(prefix):] |
| 42 | + |
| 43 | + |
| 44 | +def ask_dataset_files_list(files: List[File]) -> List[File]: |
| 45 | + answers = prompt_wrapper({ |
| 46 | + 'type': 'checkbox', |
| 47 | + 'name': 'files', |
| 48 | + 'message': 'Select the files to download', |
| 49 | + 'choices': [ |
| 50 | + strip_prefix(file.relative_path, "data/") |
| 51 | + for file in files |
| 52 | + ] |
| 53 | + }) |
| 54 | + |
| 55 | + selected_files = [ |
| 56 | + file |
| 57 | + for file in files |
| 58 | + if strip_prefix(file.relative_path, "data/") in set(answers['files']) |
| 59 | + ] |
| 60 | + |
| 61 | + if len(selected_files) == 0: |
| 62 | + if ask( |
| 63 | + "confirm", |
| 64 | + "No files were selected - try again?" |
| 65 | + ): |
| 66 | + return ask_dataset_files_list(files) |
| 67 | + else: |
| 68 | + raise InputError("No files selected") |
| 69 | + else: |
| 70 | + return selected_files |
| 71 | + |
| 72 | + |
| 73 | +def ask_dataset_files_glob(files: List[File]) -> List[File]: |
| 74 | + |
| 75 | + confirmed = False |
| 76 | + while not confirmed: |
| 77 | + selected_files = ask_dataset_files_glob_single(files) |
| 78 | + confirmed = ask( |
| 79 | + "confirm", |
| 80 | + f'Number of files selected: {len(selected_files):} / {len(files):,}' |
| 81 | + ) |
| 82 | + |
| 83 | + if len(selected_files) == 0: |
| 84 | + raise InputError("No files selected") |
| 85 | + |
| 86 | + return selected_files |
| 87 | + |
| 88 | + |
| 89 | +def ask_dataset_files_glob_single(files: List[File]) -> List[File]: |
| 90 | + |
| 91 | + print("All Files:") |
| 92 | + for file in files: |
| 93 | + print(f" - {strip_prefix(file.relative_path, 'data/')}") |
| 94 | + |
| 95 | + answers = prompt_wrapper({ |
| 96 | + 'type': 'text', |
| 97 | + 'name': 'glob', |
| 98 | + 'message': 'Select files by naming pattern (using the * wildcard)', |
| 99 | + 'default': '*' |
| 100 | + }) |
| 101 | + |
| 102 | + selected_files = [ |
| 103 | + file |
| 104 | + for file in files |
| 105 | + if fnmatch(strip_prefix(file.relative_path, "data/"), answers['glob']) |
| 106 | + ] |
| 107 | + |
| 108 | + print("Selected Files:") |
| 109 | + for file in selected_files: |
| 110 | + print(f" - {strip_prefix(file.relative_path, 'data/')}") |
| 111 | + |
| 112 | + return selected_files |
| 113 | + |
| 114 | + |
| 115 | +def ask_directory(input_value: str) -> str: |
| 116 | + directory_prompt = { |
| 117 | + 'type': 'path', |
| 118 | + 'name': 'directory', |
| 119 | + 'only_directories': True, |
| 120 | + 'message': 'What local folder would you like to compare data contents for?', |
| 121 | + 'default': input_value or str(Path.cwd()) |
| 122 | + } |
| 123 | + |
| 124 | + answers = prompt_wrapper(directory_prompt) |
| 125 | + return answers['directory'] |
| 126 | + |
| 127 | + |
| 128 | +def gather_validate_arguments(input_params: DownloadArguments, projects: List[Project]): |
| 129 | + input_params['project'] = ask_project(projects, input_params.get('project')) |
| 130 | + return input_params |
| 131 | + |
| 132 | + |
| 133 | +def gather_validate_arguments_dataset(input_params: DownloadArguments, datasets: List[Dataset]): |
| 134 | + input_params['dataset'] = ask_dataset(datasets, input_params.get('dataset'), 'validate') |
| 135 | + input_params['data_directory'] = ask_directory(input_params.get('data_directory')) |
| 136 | + return input_params |
0 commit comments