Skip to content

Commit 0c19e3d

Browse files
added pre-filters to expand_user_input private function, removed unnecessary test cases
1 parent f0ffd0f commit 0c19e3d

File tree

3 files changed

+12
-67
lines changed

3 files changed

+12
-67
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from diffpy.labpdfproc.functions import apply_corr, compute_cve
55
from diffpy.labpdfproc.tools import (
6-
expand_list_file,
76
known_sources,
87
load_user_metadata,
98
set_input_lists,
@@ -29,14 +28,9 @@ def get_args(override_cli_inputs=None):
2928
"'.' (load everything in the current directory), 'data' (load"
3029
"everything in the folder ./data), 'data/file_list.txt' (load"
3130
" the list of files contained in the text-file called "
32-
"file_list.txt that can be found 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). ",
31+
"file_list.txt that can be found in the folder ./data), "
32+
"'data/*.chi' (load all files with extension .chi in the "
33+
"folder ./data), 'data*' (load all files and directories starting with 'data').",
4034
)
4135
p.add_argument(
4236
"-a",
@@ -102,7 +96,6 @@ def get_args(override_cli_inputs=None):
10296

10397
def main():
10498
args = get_args()
105-
args = expand_list_file(args)
10699
args = set_input_lists(args)
107100
args.output_directory = set_output_directory(args)
108101
args.wavelength = set_wavelength(args)
@@ -126,7 +119,7 @@ def main():
126119
)
127120

128121
input_pattern = Diffraction_object(wavelength=args.wavelength)
129-
xarray, yarray = loadData(args.input_file, unpack=True)
122+
xarray, yarray = loadData(filepath, unpack=True)
130123
input_pattern.insert_scattering_quantity(
131124
xarray,
132125
yarray,

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
from diffpy.labpdfproc.labpdfprocapp import get_args
88
from diffpy.labpdfproc.tools import (
9-
expand_list_file,
10-
expand_wildcard_file,
119
known_sources,
1210
load_user_metadata,
1311
set_input_lists,
@@ -81,10 +79,6 @@
8179
["unreadable*.txt"],
8280
["unreadable_file.txt"],
8381
),
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-
),
8882
]
8983

9084

@@ -96,8 +90,6 @@ def test_set_input_lists(inputs, expected, user_filesystem):
9690

9791
cli_inputs = ["2.5"] + inputs
9892
actual_args = get_args(cli_inputs)
99-
actual_args = expand_wildcard_file(actual_args)
100-
actual_args = expand_list_file(actual_args)
10193
actual_args = set_input_lists(actual_args)
10294
assert sorted(actual_args.input_paths) == sorted(expected_paths)
10395

@@ -121,16 +113,6 @@ def test_set_input_lists(inputs, expected, user_filesystem):
121113
["input_dir/file_list.txt"],
122114
"Cannot find missing_file.txt. Please specify valid input file(s) or directories.",
123115
),
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-
),
134116
]
135117

136118

@@ -141,8 +123,6 @@ def test_set_input_files_bad(inputs, msg, user_filesystem):
141123
cli_inputs = ["2.5"] + inputs
142124
actual_args = get_args(cli_inputs)
143125
with pytest.raises(FileNotFoundError, match=msg[0]):
144-
actual_args = expand_wildcard_file(actual_args)
145-
actual_args = expand_list_file(actual_args)
146126
actual_args = set_input_lists(actual_args)
147127

148128

src/diffpy/labpdfproc/tools.py

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import glob
21
from pathlib import Path
32

43
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
@@ -29,9 +28,9 @@ def set_output_directory(args):
2928
return output_dir
3029

3130

32-
def expand_wildcard_file(args):
31+
def _expand_user_input(args):
3332
"""
34-
Expands wildcard inputs by adding all files or directories within directories matching the pattern.
33+
Expands the list of inputs by adding files from file lists and wildcards.
3534
3635
Parameters
3736
----------
@@ -40,45 +39,17 @@ def expand_wildcard_file(args):
4039
4140
Returns
4241
-------
43-
the arguments with the wildcard inputs expanded
42+
the arguments with the modified input list
4443
4544
"""
4645
wildcard_inputs = [input_name for input_name in args.input if "*" in input_name]
46+
file_list_inputs = [input_name for input_name in args.input if "file_list" in input_name]
47+
4748
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}.")
49+
input_files = [str(file) for file in Path(".").glob(wildcard_input)]
50+
args.input.extend(input_files)
6351
args.input.remove(wildcard_input)
64-
return args
6552

66-
67-
def expand_list_file(args):
68-
"""
69-
Expands the list of inputs by adding files from file lists and removing the file list.
70-
71-
Parameters
72-
----------
73-
args argparse.Namespace
74-
the arguments from the parser
75-
76-
Returns
77-
-------
78-
the arguments with the modified input list
79-
80-
"""
81-
file_list_inputs = [input_name for input_name in args.input if "file_list" in input_name]
8253
for file_list_input in file_list_inputs:
8354
with open(file_list_input, "r") as f:
8455
file_inputs = [input_name.strip() for input_name in f.readlines()]
@@ -106,6 +77,7 @@ def set_input_lists(args):
10677
"""
10778

10879
input_paths = []
80+
args = _expand_user_input(args)
10981
for input_name in args.input:
11082
input_path = Path(input_name).resolve()
11183
if input_path.exists():

0 commit comments

Comments
 (0)