Skip to content

implement wildcard pattern for input #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/diffpy/labpdfproc/labpdfprocapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ 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). "
"Wildcard character (*) is accepted. Examples include './*chi'"
" (load all files with .chi extension) and 'data/test*' (load "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe change to, ./*.chi to be closer to that description.

What happens if the wild-card expands to files and directories? We should tell the user what happens then.

"all files starting with 'test' in the folder ./data). ",
)
p.add_argument(
"-a",
Expand Down Expand Up @@ -101,7 +104,7 @@ def main():
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")
Expand All @@ -125,7 +128,7 @@ def main():
yarray,
"tth",
scat_quantity="x-ray",
name=str(args.input_file),
name=filepath.stem,
metadata={"muD": args.mud, "anode_type": args.anode_type},
)

Expand Down
19 changes: 19 additions & 0 deletions src/diffpy/labpdfproc/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@
["input_dir/file_list_example2.txt"],
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
),
( # wildcard pattern, same directory
["./*.chi"],
["good_data.chi"],
),
( # wildcard pattern, input directory
["input_dir/*.chi"],
["input_dir/good_data.chi"],
),
( # mixture of valid wildcard patterns
["good_data*", "./*.pkl", "unreadable*.txt", "input_dir/*.chi"],
[
"good_data.chi",
"good_data.xy",
"good_data.txt",
"unreadable_file.txt",
"binary.pkl",
"input_dir/good_data.chi",
],
),
]


Expand Down
11 changes: 10 additions & 1 deletion src/diffpy/labpdfproc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ 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}")
if "*" in input_name:
input_parent_directory = input_path.parents[0]
input_pattern = input_path.relative_to(input_parent_directory)
input_files = Path(input_parent_directory).glob(str(input_pattern))
input_files = [
file.resolve() for file in input_files if file.is_file() and "file_list" not in file.name
]
input_paths.extend(input_files)
else:
raise FileNotFoundError(f"Cannot find {input_name}")
setattr(args, "input_paths", list(set(input_paths)))
return args

Expand Down
Loading