diff --git a/src/diffpy/labpdfproc/labpdfprocapp.py b/src/diffpy/labpdfproc/labpdfprocapp.py index 6dca178..2807fc8 100644 --- a/src/diffpy/labpdfproc/labpdfprocapp.py +++ b/src/diffpy/labpdfproc/labpdfprocapp.py @@ -3,7 +3,6 @@ from diffpy.labpdfproc.functions import apply_corr, compute_cve from diffpy.labpdfproc.tools import ( - expand_list_file, known_sources, load_user_metadata, set_input_lists, @@ -29,7 +28,9 @@ def get_args(override_cli_inputs=None): "'.' (load everything in the current directory), 'data' (load" "everything in the folder ./data), 'data/file_list.txt' (load" " the list of files contained in the text-file called " - "file_list.txt that can be found in the folder ./data).", + "file_list.txt that can be found in the folder ./data), " + "'./*.chi', 'data/*.chi' (load all files with extension .chi in the " + "folder ./data).", ) p.add_argument( "-a", @@ -95,13 +96,12 @@ def get_args(override_cli_inputs=None): def main(): args = get_args() - args = expand_list_file(args) args = set_input_lists(args) args.output_directory = set_output_directory(args) args.wavelength = set_wavelength(args) args = load_user_metadata(args) - for filepath in args.input_directory: + for filepath in args.input_paths: outfilestem = filepath.stem + "_corrected" corrfilestem = filepath.stem + "_cve" outfile = args.output_directory / (outfilestem + ".chi") @@ -119,13 +119,13 @@ def main(): ) input_pattern = Diffraction_object(wavelength=args.wavelength) - xarray, yarray = loadData(args.input_file, unpack=True) + xarray, yarray = loadData(filepath, unpack=True) input_pattern.insert_scattering_quantity( xarray, yarray, "tth", scat_quantity="x-ray", - name=str(args.input_file), + name=filepath.stem, metadata={"muD": args.mud, "anode_type": args.anode_type}, ) diff --git a/src/diffpy/labpdfproc/tests/conftest.py b/src/diffpy/labpdfproc/tests/conftest.py index 5b8c323..717d148 100644 --- a/src/diffpy/labpdfproc/tests/conftest.py +++ b/src/diffpy/labpdfproc/tests/conftest.py @@ -39,6 +39,7 @@ def user_filesystem(tmp_path): with open(input_dir / "file_list.txt", "w") as f: f.write("good_data.chi \n good_data.xy \n good_data.txt \n missing_file.txt") with open(input_dir / "file_list_example2.txt", "w") as f: + f.write("input_dir/*.txt \n") f.write("input_dir/good_data.chi \n") f.write("good_data.xy \n") f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n") diff --git a/src/diffpy/labpdfproc/tests/test_tools.py b/src/diffpy/labpdfproc/tests/test_tools.py index 23e56bb..be036b7 100644 --- a/src/diffpy/labpdfproc/tests/test_tools.py +++ b/src/diffpy/labpdfproc/tests/test_tools.py @@ -6,7 +6,6 @@ from diffpy.labpdfproc.labpdfprocapp import get_args from diffpy.labpdfproc.tools import ( - expand_list_file, known_sources, load_user_metadata, set_input_lists, @@ -50,9 +49,21 @@ "input_dir/binary.pkl", ], ), - ( # file_list_example2.txt list of files provided in different directories + ( # file_list_example2.txt list of files provided in different directories with wildcard ["input_dir/file_list_example2.txt"], - ["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"], + ["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt", "input_dir/unreadable_file.txt"], + ), + ( # wildcard pattern, matching files with .chi extension in the same directory + ["./*.chi"], + ["good_data.chi"], + ), + ( # wildcard pattern, matching files with .chi extension in the input directory + ["input_dir/*.chi"], + ["input_dir/good_data.chi"], + ), + ( # wildcard pattern, matching files starting with good_data + ["good_data*"], + ["good_data.chi", "good_data.xy", "good_data.txt"], ), ] @@ -65,7 +76,6 @@ def test_set_input_lists(inputs, expected, user_filesystem): cli_inputs = ["2.5"] + inputs actual_args = get_args(cli_inputs) - actual_args = expand_list_file(actual_args) actual_args = set_input_lists(actual_args) assert sorted(actual_args.input_paths) == sorted(expected_paths) @@ -98,7 +108,6 @@ def test_set_input_files_bad(inputs, msg, user_filesystem): os.chdir(base_dir) cli_inputs = ["2.5"] + inputs actual_args = get_args(cli_inputs) - actual_args = expand_list_file(actual_args) with pytest.raises(FileNotFoundError, match=msg[0]): actual_args = set_input_lists(actual_args) diff --git a/src/diffpy/labpdfproc/tools.py b/src/diffpy/labpdfproc/tools.py index a95bf92..ea80ec9 100644 --- a/src/diffpy/labpdfproc/tools.py +++ b/src/diffpy/labpdfproc/tools.py @@ -28,9 +28,9 @@ def set_output_directory(args): return output_dir -def expand_list_file(args): +def _expand_user_input(args): """ - Expands the list of inputs by adding files from file lists and removing the file list. + Expands the list of inputs by adding files from file lists and wildcards. Parameters ---------- @@ -48,6 +48,11 @@ def expand_list_file(args): file_inputs = [input_name.strip() for input_name in f.readlines()] args.input.extend(file_inputs) args.input.remove(file_list_input) + wildcard_inputs = [input_name for input_name in args.input if "*" in input_name] + for wildcard_input in wildcard_inputs: + input_files = [str(file) for file in Path(".").glob(wildcard_input) if "file_list" not in file.name] + args.input.extend(input_files) + args.input.remove(wildcard_input) return args @@ -70,6 +75,7 @@ def set_input_lists(args): """ input_paths = [] + args = _expand_user_input(args) for input_name in args.input: input_path = Path(input_name).resolve() if input_path.exists(): @@ -86,7 +92,7 @@ def set_input_lists(args): f"Cannot find {input_name}. Please specify valid input file(s) or directories." ) else: - raise FileNotFoundError(f"Cannot find {input_name}") + raise FileNotFoundError(f"Cannot find {input_name}.") setattr(args, "input_paths", list(set(input_paths))) return args