Skip to content

Commit fc3a570

Browse files
initial commit on implementing a wildcard pattern for input
1 parent c45022d commit fc3a570

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ def get_args(override_cli_inputs=None):
2929
"'.' (load everything in the current directory), 'data' (load"
3030
"everything in the folder ./data), 'data/file_list.txt' (load"
3131
" the list of files contained in the text-file called "
32-
"file_list.txt that can be found in the folder ./data).",
32+
"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). ",
3336
)
3437
p.add_argument(
3538
"-a",
@@ -101,7 +104,7 @@ def main():
101104
args.wavelength = set_wavelength(args)
102105
args = load_user_metadata(args)
103106

104-
for filepath in args.input_directory:
107+
for filepath in args.input_paths:
105108
outfilestem = filepath.stem + "_corrected"
106109
corrfilestem = filepath.stem + "_cve"
107110
outfile = args.output_directory / (outfilestem + ".chi")
@@ -125,7 +128,7 @@ def main():
125128
yarray,
126129
"tth",
127130
scat_quantity="x-ray",
128-
name=str(args.input_file),
131+
name=filepath.stem,
129132
metadata={"muD": args.mud, "anode_type": args.anode_type},
130133
)
131134

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@
5454
["input_dir/file_list_example2.txt"],
5555
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
5656
),
57+
( # wildcard pattern, same directory
58+
["./*.chi"],
59+
["good_data.chi"],
60+
),
61+
( # wildcard pattern, input directory
62+
["input_dir/*.chi"],
63+
["input_dir/good_data.chi"],
64+
),
65+
( # mixture of valid wildcard patterns
66+
["good_data*", "./*.pkl", "unreadable*.txt", "input_dir/*.chi"],
67+
[
68+
"good_data.chi",
69+
"good_data.xy",
70+
"good_data.txt",
71+
"unreadable_file.txt",
72+
"binary.pkl",
73+
"input_dir/good_data.chi",
74+
],
75+
),
5776
]
5877

5978

src/diffpy/labpdfproc/tools.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ def set_input_lists(args):
8686
f"Cannot find {input_name}. Please specify valid input file(s) or directories."
8787
)
8888
else:
89-
raise FileNotFoundError(f"Cannot find {input_name}")
89+
if "*" in input_name.split("/")[-1] and input_name.count("*") == 1:
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}")
9099
setattr(args, "input_paths", list(set(input_paths)))
91100
return args
92101

0 commit comments

Comments
 (0)