Skip to content

Commit 13b1e4f

Browse files
removed nesting functions and the private function, moved file list test with missing file to errorous case
1 parent a9facce commit 13b1e4f

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

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

44
from diffpy.labpdfproc.functions import apply_corr, compute_cve
55
from diffpy.labpdfproc.tools import (
6+
expand_list_file,
67
known_sources,
78
load_user_metadata,
89
set_input_lists,
@@ -94,6 +95,7 @@ def get_args(override_cli_inputs=None):
9495

9596
def main():
9697
args = get_args()
98+
args = expand_list_file(args)
9799
args = set_input_lists(args)
98100
args.output_directory = set_output_directory(args)
99101
args.wavelength = set_wavelength(args)

src/diffpy/labpdfproc/tests/test_tools.py

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

77
from diffpy.labpdfproc.labpdfprocapp import get_args
88
from diffpy.labpdfproc.tools import (
9+
expand_list_file,
910
known_sources,
1011
load_user_metadata,
1112
set_input_lists,
@@ -49,10 +50,6 @@
4950
"input_dir/binary.pkl",
5051
],
5152
),
52-
( # file_list.txt list of files provided
53-
["input_dir/file_list.txt"],
54-
["good_data.chi", "good_data.xy", "good_data.txt"],
55-
),
5653
( # file_list_example2.txt list of files provided in different directories
5754
["input_dir/file_list_example2.txt"],
5855
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
@@ -68,6 +65,7 @@ def test_set_input_lists(inputs, expected, user_filesystem):
6865

6966
cli_inputs = ["2.5"] + inputs
7067
actual_args = get_args(cli_inputs)
68+
actual_args = expand_list_file(actual_args)
7169
actual_args = set_input_lists(actual_args)
7270
assert sorted(actual_args.input_paths) == sorted(expected_paths)
7371

@@ -87,6 +85,10 @@ def test_set_input_lists(inputs, expected, user_filesystem):
8785
["good_data.chi", "good_data.xy", "unreadable_file.txt", "missing_file.txt"],
8886
"Cannot find missing_file.txt. Please specify valid input file(s) or directories.",
8987
),
88+
( # file_list.txt list of files provided (with missing files)
89+
["input_dir/file_list.txt"],
90+
"Cannot find missing_file.txt. Please specify valid input file(s) or directories.",
91+
),
9092
]
9193

9294

@@ -96,6 +98,7 @@ def test_set_input_files_bad(inputs, msg, user_filesystem):
9698
os.chdir(base_dir)
9799
cli_inputs = ["2.5"] + inputs
98100
actual_args = get_args(cli_inputs)
101+
actual_args = expand_list_file(actual_args)
99102
with pytest.raises(FileNotFoundError, match=msg[0]):
100103
actual_args = set_input_lists(actual_args)
101104

src/diffpy/labpdfproc/tools.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,27 @@ def set_output_directory(args):
2828
return output_dir
2929

3030

31-
def _parse_file_list_file(file_list_path):
32-
with open(file_list_path, "r") as f:
33-
# file_paths = [Path(file_path.strip()).resolve() for file_path in f.readlines()
34-
# if Path(file_path.strip()).is_file()]
35-
file_paths = [file_path.strip() for file_path in f.readlines()]
36-
return file_paths
31+
def expand_list_file(args):
32+
"""
33+
Expands the list of inputs by adding files from file lists and removing the file list.
3734
35+
Parameters
36+
----------
37+
args argparse.Namespace
38+
the arguments from the parser
3839
39-
def expand_list_file(input):
40-
file_list_inputs = [input_name for input_name in input if "file_list" in str(input_name)]
40+
Returns
41+
-------
42+
the arguments with the modified input list
43+
44+
"""
45+
file_list_inputs = [input_name for input_name in args.input if "file_list" in input_name]
4146
for file_list_input in file_list_inputs:
42-
input.remove(file_list_input)
43-
input.extend(_parse_file_list_file(file_list_input))
44-
return input
47+
with open(file_list_input, "r") as f:
48+
file_inputs = [input_name.strip() for input_name in f.readlines()]
49+
args.input.extend(file_inputs)
50+
args.input.remove(file_list_input)
51+
return args
4552

4653

4754
def set_input_lists(args):
@@ -63,9 +70,8 @@ def set_input_lists(args):
6370
"""
6471

6572
input_paths = []
66-
expanded_input = expand_list_file(args.input)
67-
for input in expanded_input:
68-
input_path = Path(input).resolve()
73+
for input_name in args.input:
74+
input_path = Path(input_name).resolve()
6975
if input_path.exists():
7076
if input_path.is_file():
7177
input_paths.append(input_path)
@@ -76,9 +82,11 @@ def set_input_lists(args):
7682
]
7783
input_paths.extend(input_files)
7884
else:
79-
raise FileNotFoundError(f"Cannot find {input}. Please specify valid input file(s) or directories.")
85+
raise FileNotFoundError(
86+
f"Cannot find {input_name}. Please specify valid input file(s) or directories."
87+
)
8088
else:
81-
raise FileNotFoundError(f"Cannot find {input}")
89+
raise FileNotFoundError(f"Cannot find {input_name}")
8290
setattr(args, "input_paths", list(set(input_paths)))
8391
return args
8492

0 commit comments

Comments
 (0)