Skip to content

Commit ead5830

Browse files
intermediate process (more tests need to be added): using input_directory only for simplication
1 parent 7d39a74 commit ead5830

File tree

3 files changed

+68
-80
lines changed

3 files changed

+68
-80
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22
from argparse import ArgumentParser
3-
from pathlib import Path
43

54
from diffpy.labpdfproc.functions import apply_corr, compute_cve
65
from diffpy.labpdfproc.tools import (
@@ -20,8 +19,11 @@ def get_args(override_cli_inputs=None):
2019
p.add_argument(
2120
"input",
2221
nargs="+",
23-
help="The filename or directory of the datafile to load. Required. "
24-
"Supports either a single input file, a directory, a file containing a list of files, or multiple files. ",
22+
help="The filename(s) or folder(s) of the datafile(s) to load. Required. "
23+
"Supports multiple arguments of input file or directory. "
24+
"The file can be either a data file or a file containing a list of files. "
25+
"If a directory is provided, we will load all data files in it. "
26+
"For example, file.xy, data/file.xy, file_list.txt, ./data/file.xy, ./data are all valid inputs. ",
2527
)
2628
p.add_argument(
2729
"-a",
@@ -92,8 +94,7 @@ def main():
9294
args.wavelength = set_wavelength(args)
9395
args = load_user_metadata(args)
9496

95-
for input_file in args.input_file:
96-
filepath = Path(args.input_file)
97+
for filepath in args.input_directory:
9798
outfilestem = filepath.stem + "_corrected"
9899
corrfilestem = filepath.stem + "_cve"
99100
outfile = args.output_directory / (outfilestem + ".chi")

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,86 +19,80 @@
1919
# This test covers existing single input file, directory, a file list, and multiple files
2020
# We store absolute path into input_directory and file names into input_file
2121
params_input = [
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
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
2424
( # glob current directory
2525
["."],
26-
[
27-
".",
28-
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
29-
],
26+
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
3027
),
3128
( # glob input directory
3229
["./input_dir"],
3330
[
34-
"input_dir",
35-
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
31+
"input_dir/good_data.chi",
32+
"input_dir/good_data.xy",
33+
"input_dir/good_data.txt",
34+
"input_dir/unreadable_file.txt",
35+
"input_dir/binary.pkl",
3636
],
3737
),
3838
( # list of files provided (we skip if encountering invalid files)
3939
["good_data.chi", "good_data.xy", "unreadable_file.txt", "missing_file.txt"],
40-
[
41-
".",
42-
["good_data.chi", "good_data.xy", "unreadable_file.txt"],
43-
],
40+
["good_data.chi", "good_data.xy", "unreadable_file.txt"],
4441
),
4542
( # list of files provided (with invalid files and files in different directories)
46-
["input_dir/good_data.chi", "good_data.xy", "missing_file.txt"],
47-
[
48-
".",
49-
["input_dir/good_data.chi", "good_data.xy"],
50-
],
43+
["input_dir/good_data.chi", "good_data.chi", "missing_file.txt"],
44+
["input_dir/good_data.chi", "good_data.chi"],
5145
),
5246
( # file_list.txt list of files provided
5347
["file_list_dir/file_list.txt"],
54-
[".", ["good_data.chi", "good_data.xy", "good_data.txt"]],
48+
["good_data.chi", "good_data.xy", "good_data.txt"],
5549
),
5650
( # file_list_example2.txt list of files provided in different directories
5751
["file_list_dir/file_list_example2.txt"],
58-
[".", ["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"]],
52+
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
5953
),
6054
]
6155

6256

6357
@pytest.mark.parametrize("inputs, expected", params_input)
6458
def test_set_input_files(inputs, expected, user_filesystem):
65-
expected_input_directory = Path(user_filesystem) / expected[0]
66-
expected_input_files = expected[1]
59+
expected_input_directory = []
60+
for expected_path in expected:
61+
expected_input_directory.append(Path(user_filesystem) / expected_path)
6762

6863
cli_inputs = ["2.5"] + inputs
6964
actual_args = get_args(cli_inputs)
7065
actual_args = set_input_files(actual_args)
71-
assert actual_args.input_directory == expected_input_directory
72-
assert set(actual_args.input_file) == set(expected_input_files)
66+
assert set(actual_args.input_directory) == set(expected_input_directory)
7367

7468

7569
# This test is for existing single input file or directory absolute path not in cwd
7670
# Here we are in user_filesystem/input_dir, testing for a file or directory in user_filesystem
7771
params_input_not_cwd = [
78-
(["good_data.chi"], [".", "good_data.chi"]),
79-
(["."], [".", ["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"]]),
72+
(["good_data.chi"], ["good_data.chi"]),
73+
(["."], ["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"]),
8074
]
8175

8276

8377
@pytest.mark.parametrize("inputs, expected", params_input_not_cwd)
8478
def test_set_input_files_not_cwd(inputs, expected, user_filesystem):
85-
expected_input_directory = Path(user_filesystem) / expected[0]
86-
expected_input_files = expected[1]
79+
expected_input_directory = []
80+
for expected_path in expected:
81+
expected_input_directory.append(Path(user_filesystem) / expected_path)
8782
actual_input = [str(Path(user_filesystem) / inputs[0])]
8883
os.chdir("input_dir")
8984

9085
cli_inputs = ["2.5"] + actual_input
9186
actual_args = get_args(cli_inputs)
9287
actual_args = set_input_files(actual_args)
93-
assert actual_args.input_directory == expected_input_directory
94-
assert set(actual_args.input_file) == set(expected_input_files)
88+
assert set(actual_args.input_directory) == set(expected_input_directory)
9589

9690

9791
# This test covers non-existing single input file or directory, in this case we raise an error with message
9892
params_input_bad = [
99-
(["non_existing_file.xy"], "Please specify valid input file or directory."),
100-
(["./input_dir/non_existing_file.xy"], "Please specify valid input file or directory."),
101-
(["./non_existing_dir"], "Please specify valid input file or directory."),
93+
(["non_existing_file.xy"], "Please specify at least one valid input file or directory."),
94+
(["./input_dir/non_existing_file.xy"], "Please specify at least one valid input file or directory."),
95+
(["./non_existing_dir"], "Please specify at least one valid input file or directory."),
10296
]
10397

10498

src/diffpy/labpdfproc/tools.py

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,52 @@ def set_input_files(args):
1616
the arguments from the parser
1717
1818
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.
19+
For each input, we try to read it as a file or a directory.
2020
If input is a file, we first try to read it as a file list and store all listed file names.
2121
If the first filename is invalid, then we proceed to treat it as a data file.
2222
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.
23+
If any file does not exist, we raise a ValueError telling which file(s) does not exist.
24+
If all files are invalid, we raise an Error telling user to specify at least one valid file or directory.
2425
2526
Returns
2627
-------
2728
args argparse.Namespace
2829
2930
"""
3031

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():
45-
raise ValueError("Please specify valid input file or directory.")
46-
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]
64-
65-
else:
66-
input_dir = Path(args.input[0]).resolve()
67-
input_files = [file for file in glob.glob(str(input_dir) + "/*", recursive=True) if os.path.isfile(file)]
68-
input_file_name = [os.path.basename(input_file_path) for input_file_path in input_files]
69-
70-
setattr(args, "input_directory", input_dir)
71-
setattr(args, "input_file", input_file_name)
32+
input_paths = []
33+
for input in args.input:
34+
try:
35+
if Path(input).exists():
36+
if not Path(input).is_dir():
37+
with open(args.input[0], "r") as f:
38+
lines = [line.strip() for line in f]
39+
if not os.path.isfile(lines[0]):
40+
input_paths.append(Path(input).resolve())
41+
else:
42+
for line in lines:
43+
try:
44+
if os.path.isfile(line):
45+
input_paths.append(Path(line).resolve())
46+
except Exception as e:
47+
raise ValueError(f"{line} does not exist. {e}.")
48+
49+
else:
50+
input_dir = Path(input).resolve()
51+
input_files = [
52+
Path(file).resolve()
53+
for file in glob.glob(str(input_dir) + "/*", recursive=True)
54+
if os.path.isfile(file)
55+
]
56+
input_paths.extend(input_files)
57+
58+
except Exception as e:
59+
raise ValueError(f"{input} does not exist. {e}.")
60+
61+
if len(input_paths) == 0:
62+
raise ValueError("Please specify at least one valid input file or directory.")
63+
64+
setattr(args, "input_directory", input_paths)
7265
return args
7366

7467

0 commit comments

Comments
 (0)