Skip to content

Commit 06ae14b

Browse files
added tests for a file list and edited help message addressing the rules for inputing a file list
1 parent 3d5c5ee commit 06ae14b

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
def get_args(override_cli_inputs=None):
1818
p = ArgumentParser()
1919
p.add_argument("mud", help="Value of mu*D for your " "sample. Required.", type=float)
20-
p.add_argument("input", help="The filename or directory of the datafile to load. Required.")
20+
p.add_argument(
21+
"input",
22+
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. ",
26+
)
2127
p.add_argument(
2228
"-a",
2329
"--anode-type",

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ def user_filesystem(tmp_path):
3939
with open(os.path.join(input_dir, "binary.pkl"), "wb") as f:
4040
f.write(binary_data)
4141

42+
file_list_dir = Path(tmp_path).resolve() / "file_list_dir"
43+
file_list_dir.mkdir(parents=True, exist_ok=True)
44+
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")
50+
4251
yield tmp_path

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23
from pathlib import Path
34

@@ -15,7 +16,7 @@
1516

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

18-
# This test covers existing single input file or directory
19+
# This test covers existing single input file, directory, or a file list
1920
# We store absolute path into input_directory and file names into input_file
2021
params_input = [
2122
(["good_data.chi"], [".", "good_data.chi"]),
@@ -42,6 +43,7 @@
4243
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
4344
],
4445
),
46+
(["file_list_dir/file_list.txt"], ["file_list_dir", ["good_data.chi", "good_data.xy", "good_data.txt"]]),
4547
]
4648

4749

@@ -57,6 +59,28 @@ def test_set_input_files(inputs, expected, user_filesystem):
5759
assert set(actual_args.input_file) == set(expected_input_files)
5860

5961

62+
# This test is for existing single input file or directory absolute path not in cwd
63+
# Here we are in user_filesystem/input_dir, testing for a file or directory in user_filesystem
64+
params_input_not_cwd = [
65+
(["good_data.chi"], [".", "good_data.chi"]),
66+
(["."], [".", ["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"]]),
67+
]
68+
69+
70+
@pytest.mark.parametrize("inputs, expected", params_input_not_cwd)
71+
def test_set_input_files_not_cwd(inputs, expected, user_filesystem):
72+
expected_input_directory = Path(user_filesystem) / expected[0]
73+
expected_input_files = expected[1]
74+
actual_input = [str(Path(user_filesystem) / inputs[0])]
75+
os.chdir("input_dir")
76+
77+
cli_inputs = ["2.5"] + actual_input
78+
actual_args = get_args(cli_inputs)
79+
actual_args = set_input_files(actual_args)
80+
assert actual_args.input_directory == expected_input_directory
81+
assert set(actual_args.input_file) == set(expected_input_files)
82+
83+
6084
# This test covers non-existing single input file or directory, in this case we raise an error with message
6185
params_input_bad = [
6286
(["non_existing_file.xy"], "Please specify valid input file or directory."),

src/diffpy/labpdfproc/tools.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,42 @@ def set_input_files(args):
1515
args argparse.Namespace
1616
the arguments from the parser
1717
18+
It is implemented as this:
19+
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+
Otherwise if we have a directory, glob all files within it.
22+
1823
Returns
1924
-------
2025
args argparse.Namespace
2126
2227
"""
28+
2329
if not Path(args.input).exists():
2430
raise ValueError("Please specify valid input file or directory.")
2531

2632
if not Path(args.input).is_dir():
2733
input_dir = Path.cwd() / Path(args.input).parent
28-
input_file_name = Path(args.input).name
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
48+
2949
else:
3050
input_dir = Path(args.input).resolve()
3151
input_files = [file for file in glob.glob(str(input_dir) + "/*", recursive=True) if os.path.isfile(file)]
3252
input_file_name = [os.path.basename(input_file_path) for input_file_path in input_files]
53+
3354
setattr(args, "input_directory", input_dir)
3455
setattr(args, "input_file", input_file_name)
3556
return args

0 commit comments

Comments
 (0)