Skip to content

Commit a02b573

Browse files
added tests for file list and multiple files
1 parent 06ae14b commit a02b573

File tree

4 files changed

+68
-40
lines changed

4 files changed

+68
-40
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ def get_args(override_cli_inputs=None):
1919
p.add_argument("mud", help="Value of mu*D for your " "sample. Required.", type=float)
2020
p.add_argument(
2121
"input",
22+
nargs="+",
2223
help="The filename or directory of the datafile to load. Required. "
23-
"Supports either a single input file or directory, or a file containing a list of files. "
24-
"If providing a file list, please ensure all files are in the same directory as the file list, "
25-
"and each filename is written line by line in the file list. ",
24+
"Supports either a single input file, a directory, a file containing a list of files, or multiple files. ",
2625
)
2726
p.add_argument(
2827
"-a",

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def user_filesystem(tmp_path):
4242
file_list_dir = Path(tmp_path).resolve() / "file_list_dir"
4343
file_list_dir.mkdir(parents=True, exist_ok=True)
4444
with open(os.path.join(file_list_dir, "file_list.txt"), "w") as f:
45-
f.write("good_data.chi \n good_data.xy \n good_data.txt")
46-
with open(os.path.join(file_list_dir, "invalid_file_list.txt"), "w") as f:
47-
f.write("good_data.chi \n non_existing_file.xy \n non_existing_file.txt")
48-
with open(os.path.join(file_list_dir, "invalid_format_file_list.txt"), "w") as f:
49-
f.write("good_data.chi good_data.xy \n non_existing_file.txt")
45+
f.write("good_data.chi \n good_data.xy \n good_data.txt \n missing_file.txt")
46+
with open(os.path.join(file_list_dir, "file_list_example2.txt"), "w") as f:
47+
f.write("input_dir/good_data.chi \n")
48+
f.write("good_data.xy \n")
49+
f.write(str(os.path.abspath(os.path.join(input_dir, "good_data.txt"))) + "\n")
5050

5151
yield tmp_path

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,47 @@
1616

1717
# Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
1818

19-
# This test covers existing single input file, directory, or a file list
20-
# We store absolute path into input_directory and file names into input_file
19+
# This test covers existing single input file, directory, a file list, and multiple files
20+
# We store absolute paths into input_directory and file names into input_file
2121
params_input = [
22-
(["good_data.chi"], [".", "good_data.chi"]),
23-
(["input_dir/good_data.chi"], ["input_dir", "good_data.chi"]),
24-
(["./input_dir/good_data.chi"], ["input_dir", "good_data.chi"]),
25-
(
22+
(["good_data.chi"], [".", "good_data.chi"]), # single good file, same directory
23+
(["input_dir/good_data.chi"], ["input_dir", "good_data.chi"]), # single good file, input directory
24+
( # glob current directory
2625
["."],
2726
[
2827
".",
2928
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
3029
],
3130
),
32-
(
31+
( # glob input directory
3332
["./input_dir"],
3433
[
3534
"input_dir",
3635
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
3736
],
3837
),
39-
(
40-
["input_dir"],
38+
( # list of files provided (we skip if encountering an invalid files)
39+
["good_data.chi", "good_data.xy", "unreadable_file.txt", "missing_file.txt"],
4140
[
42-
"input_dir",
43-
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
41+
".",
42+
["good_data.chi", "good_data.xy", "unreadable_file.txt"],
4443
],
4544
),
46-
(["file_list_dir/file_list.txt"], ["file_list_dir", ["good_data.chi", "good_data.xy", "good_data.txt"]]),
45+
( # list of files provided (with invalid files and files in different directory)
46+
["input_dir/good_data.chi", "good_data.xy", "missing_file.txt"],
47+
[
48+
".",
49+
["input_dir/good_data.chi", "good_data.xy"],
50+
],
51+
),
52+
( # file_list.txt list of files provided
53+
["file_list_dir/file_list.txt"],
54+
[".", ["good_data.chi", "good_data.xy", "good_data.txt"]],
55+
),
56+
( # file_list_example2.txt list of files provided with different paths
57+
["file_list_dir/file_list_example2.txt"],
58+
[".", ["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"]],
59+
),
4760
]
4861

4962

src/diffpy/labpdfproc/tools.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,55 @@ def set_input_files(args):
1515
args argparse.Namespace
1616
the arguments from the parser
1717
18-
It is implemented as this:
18+
It is implemented as the following:
19+
If user input multiple files, we store their common directory as input directory and all of their names.
1920
If input is a file, we first try to read it as a file list and store all listed file names.
20-
If any filename is invalid, then proceed to treat it as a data file.
21+
If the first filename is invalid, then we proceed to treat it as a data file.
2122
Otherwise if we have a directory, glob all files within it.
23+
If there are any invalid filenames (for the cases of multiple files, file list, or directory), we skip them.
2224
2325
Returns
2426
-------
2527
args argparse.Namespace
2628
2729
"""
2830

29-
if not Path(args.input).exists():
31+
if len(args.input) > 1:
32+
input_paths = []
33+
input_paths_parent = []
34+
for input in args.input:
35+
if Path(input).is_file():
36+
input_paths.append(Path(input).resolve())
37+
input_paths_parent.append(Path(input).resolve().parent)
38+
input_dir = Path(os.path.commonprefix([str(path) for path in input_paths_parent]))
39+
input_file_name = [str(path.relative_to(input_dir)) for path in input_paths]
40+
setattr(args, "input_directory", input_dir)
41+
setattr(args, "input_file", input_file_name)
42+
return args
43+
44+
if not Path(args.input[0]).exists():
3045
raise ValueError("Please specify valid input file or directory.")
3146

32-
if not Path(args.input).is_dir():
33-
input_dir = Path.cwd() / Path(args.input).parent
34-
file_names = []
35-
with open(args.input, "r") as f:
36-
for line in f:
37-
if not os.path.isfile(line.strip()):
38-
file_names = []
39-
break
40-
else:
41-
file_name = line.strip()
42-
file_names.append(file_name)
43-
44-
if len(file_names) > 0:
45-
input_file_name = file_names
46-
else:
47-
input_file_name = Path(args.input).name
47+
if not Path(args.input[0]).is_dir():
48+
input_paths = []
49+
input_paths_parent = []
50+
with open(args.input[0], "r") as f:
51+
lines = [line.strip() for line in f]
52+
if not os.path.isfile(lines[0]):
53+
input_dir = Path.cwd() / Path(args.input[0]).parent
54+
input_file_name = Path(args.input[0]).name
55+
else:
56+
for line in lines:
57+
if not os.path.isfile(line):
58+
continue
59+
else:
60+
input_paths.append(Path(line).resolve())
61+
input_paths_parent.append(Path(line).resolve().parent)
62+
input_dir = Path(os.path.commonprefix([str(path) for path in input_paths_parent]))
63+
input_file_name = [str(path.relative_to(input_dir)) for path in input_paths]
4864

4965
else:
50-
input_dir = Path(args.input).resolve()
66+
input_dir = Path(args.input[0]).resolve()
5167
input_files = [file for file in glob.glob(str(input_dir) + "/*", recursive=True) if os.path.isfile(file)]
5268
input_file_name = [os.path.basename(input_file_path) for input_file_path in input_files]
5369

0 commit comments

Comments
 (0)