Skip to content

Commit f0ffd0f

Browse files
added pre-filter for wildcard patterns and tests, included more examples for help message
1 parent a02a085 commit f0ffd0f

File tree

3 files changed

+78
-23
lines changed

3 files changed

+78
-23
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ def get_args(override_cli_inputs=None):
3030
"everything in the folder ./data), 'data/file_list.txt' (load"
3131
" the list of files contained in the text-file called "
3232
"file_list.txt that can be found in the folder ./data). "
33-
"Wildcard character (*) is accepted. Examples include './*chi'"
34-
" (load all files with .chi extension) and 'data/test*' (load "
35-
"all files starting with 'test' in the folder ./data). ",
33+
"\nWildcard character (*) is accepted. Examples include './*.chi'"
34+
" (load all files with .chi extension), 'data/*.chi' (load all "
35+
"files in 'data' file with .chi extension), 'file*.chi' (load all "
36+
"files starting with 'file' and ending with .chi extension), 'test*' "
37+
"(load all files and directories starting with 'test'), 'test*/*.chi' "
38+
"(load all directories starting with 'test' and all files under "
39+
"with .chi extension). ",
3640
)
3741
p.add_argument(
3842
"-a",

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from diffpy.labpdfproc.labpdfprocapp import get_args
88
from diffpy.labpdfproc.tools import (
99
expand_list_file,
10+
expand_wildcard_file,
1011
known_sources,
1112
load_user_metadata,
1213
set_input_lists,
@@ -54,25 +55,36 @@
5455
["input_dir/file_list_example2.txt"],
5556
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
5657
),
57-
( # wildcard pattern, same directory
58+
( # wildcard pattern, matching files with .chi extension in the same directory
5859
["./*.chi"],
5960
["good_data.chi"],
6061
),
61-
( # wildcard pattern, input directory
62+
( # wildcard pattern, matching files with .chi extension in the input directory
6263
["input_dir/*.chi"],
6364
["input_dir/good_data.chi"],
6465
),
65-
( # mixture of valid wildcard patterns
66-
["good_data*", "./*.pkl", "unreadable*.txt", "input_dir/*.chi"],
66+
( # wildcard pattern, matching files starting with good_data
67+
["good_data*"],
68+
["good_data.chi", "good_data.xy", "good_data.txt"],
69+
),
70+
( # wildcard pattern, matching files or directories starting with input
71+
["input*"],
6772
[
68-
"good_data.chi",
69-
"good_data.xy",
70-
"good_data.txt",
71-
"unreadable_file.txt",
72-
"binary.pkl",
7373
"input_dir/good_data.chi",
74+
"input_dir/good_data.xy",
75+
"input_dir/good_data.txt",
76+
"input_dir/unreadable_file.txt",
77+
"input_dir/binary.pkl",
7478
],
7579
),
80+
( # wildcard pattern, matching files or directories starting with unreadable and ending with .txt extension
81+
["unreadable*.txt"],
82+
["unreadable_file.txt"],
83+
),
84+
( # wildcard pattern, matching directories starting with input and all files under with .chi extension
85+
["input*/*.chi"],
86+
["input_dir/good_data.chi"],
87+
),
7688
]
7789

7890

@@ -84,6 +96,7 @@ def test_set_input_lists(inputs, expected, user_filesystem):
8496

8597
cli_inputs = ["2.5"] + inputs
8698
actual_args = get_args(cli_inputs)
99+
actual_args = expand_wildcard_file(actual_args)
87100
actual_args = expand_list_file(actual_args)
88101
actual_args = set_input_lists(actual_args)
89102
assert sorted(actual_args.input_paths) == sorted(expected_paths)
@@ -108,6 +121,16 @@ def test_set_input_lists(inputs, expected, user_filesystem):
108121
["input_dir/file_list.txt"],
109122
"Cannot find missing_file.txt. Please specify valid input file(s) or directories.",
110123
),
124+
( # valid wildcard pattern, but does not match any files or directories
125+
["non_existing_dir*"],
126+
"Invalid wildcard input non_existing_dir*. "
127+
"Please ensure the wildcard pattern matches at least one file or directory.",
128+
),
129+
( # invalid wildcard pattern
130+
["invalid_dir**"],
131+
"Invalid wildcard input invalid_dir**. "
132+
"Please ensure the wildcard pattern matches at least one file or directory.",
133+
),
111134
]
112135

113136

@@ -117,8 +140,9 @@ def test_set_input_files_bad(inputs, msg, user_filesystem):
117140
os.chdir(base_dir)
118141
cli_inputs = ["2.5"] + inputs
119142
actual_args = get_args(cli_inputs)
120-
actual_args = expand_list_file(actual_args)
121143
with pytest.raises(FileNotFoundError, match=msg[0]):
144+
actual_args = expand_wildcard_file(actual_args)
145+
actual_args = expand_list_file(actual_args)
122146
actual_args = set_input_lists(actual_args)
123147

124148

src/diffpy/labpdfproc/tools.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import glob
12
from pathlib import Path
23

34
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
@@ -28,6 +29,41 @@ def set_output_directory(args):
2829
return output_dir
2930

3031

32+
def expand_wildcard_file(args):
33+
"""
34+
Expands wildcard inputs by adding all files or directories within directories matching the pattern.
35+
36+
Parameters
37+
----------
38+
args argparse.Namespace
39+
the arguments from the parser
40+
41+
Returns
42+
-------
43+
the arguments with the wildcard inputs expanded
44+
45+
"""
46+
wildcard_inputs = [input_name for input_name in args.input if "*" in input_name]
47+
for wildcard_input in wildcard_inputs:
48+
if not glob.glob(wildcard_input):
49+
raise FileNotFoundError(
50+
f"Invalid wildcard input {wildcard_input}. "
51+
f"Please ensure the wildcard pattern matches at least one file or directory."
52+
)
53+
input_files = Path(".").glob(wildcard_input)
54+
for input_file in input_files:
55+
if input_file.is_file():
56+
args.input.append(str(input_file))
57+
elif input_file.is_dir():
58+
files = input_file.glob("*")
59+
inputs = [str(file) for file in files if file.is_file() and "file_list" not in file.name]
60+
args.input.extend(inputs)
61+
else:
62+
raise FileNotFoundError(f"Invalid wildcard input {wildcard_input}.")
63+
args.input.remove(wildcard_input)
64+
return args
65+
66+
3167
def expand_list_file(args):
3268
"""
3369
Expands the list of inputs by adding files from file lists and removing the file list.
@@ -86,16 +122,7 @@ def set_input_lists(args):
86122
f"Cannot find {input_name}. Please specify valid input file(s) or directories."
87123
)
88124
else:
89-
if "*" in input_name:
90-
input_parent_directory = input_path.parents[0]
91-
input_pattern = input_path.relative_to(input_parent_directory)
92-
input_files = Path(input_parent_directory).glob(str(input_pattern))
93-
input_files = [
94-
file.resolve() for file in input_files if file.is_file() and "file_list" not in file.name
95-
]
96-
input_paths.extend(input_files)
97-
else:
98-
raise FileNotFoundError(f"Cannot find {input_name}")
125+
raise FileNotFoundError(f"Cannot find {input_name}.")
99126
setattr(args, "input_paths", list(set(input_paths)))
100127
return args
101128

0 commit comments

Comments
 (0)