Skip to content

Commit 99d73aa

Browse files
added functionality of handling an input of a file list
1 parent 495b394 commit 99d73aa

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_args(override_cli_inputs=None):
2121
"data-files in that directory will be processed. Examples of valid "
2222
"inputs are 'file.xy', 'data/file.xy', 'file.xy, data/file.xy', "
2323
"'.' (load everything in the current directory), 'data' (load"
24-
"everything in the folder ./data', 'data/file_list.txt' (load"
24+
"everything in the folder ./data), 'data/file_list.txt' (load"
2525
" the list of files contained in the text-file called "
2626
"file_list.txt that can be found in the folder ./data).",
2727
)

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
( # glob input directory
2828
["./input_dir"],
2929
[
30+
"./good_data.chi",
31+
"./good_data.xy",
32+
"./good_data.txt",
3033
"input_dir/good_data.chi",
3134
"input_dir/good_data.xy",
3235
"input_dir/good_data.txt",
@@ -69,7 +72,7 @@ def test_set_input_lists(inputs, expected, user_filesystem):
6972
cli_inputs = ["2.5"] + inputs
7073
actual_args = get_args(cli_inputs)
7174
actual_args = set_input_lists(actual_args)
72-
assert list(actual_args.input_paths).sort() == expected_paths.sort()
75+
assert set(actual_args.input_paths) == set(expected_paths)
7376

7477

7578
# This test covers non-existing single input file or directory, in this case we raise an error with message

src/diffpy/labpdfproc/tools.py

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

3030

31+
def _parse_file_list_file(input_path):
32+
with open(input_path, "r") as f:
33+
lines = [line.strip() for line in f]
34+
input_files = [Path(line).resolve() for line in lines if Path(line).is_file()]
35+
return input_files
36+
37+
38+
def _parse_input_paths(input_path):
39+
# Takes a path to return either a list of files paths if it is a file list,
40+
# a list of single file path if it is a data file, or nothing
41+
if "file_list" in input_path.name:
42+
return _parse_file_list_file(input_path)
43+
elif input_path.is_file():
44+
return [input_path]
45+
else:
46+
return []
47+
48+
3149
def set_input_lists(args):
3250
"""
3351
Set input directory and files.
@@ -51,16 +69,16 @@ def set_input_lists(args):
5169
input_path = Path(input).resolve()
5270
if input_path.exists():
5371
if input_path.is_file():
54-
input_paths.append(input_path)
72+
input_paths.extend(_parse_input_paths(input_path))
5573
elif input_path.is_dir():
5674
input_files = input_path.glob("*")
57-
input_files = [file.resolve() for file in input_files if file.is_file()]
58-
input_paths.extend(input_files)
75+
for file in input_files:
76+
input_paths.extend(_parse_input_paths(file))
5977
else:
6078
raise FileNotFoundError(f"Cannot find {input}. Please specify valid input file(s) or directories.")
6179
else:
6280
raise FileNotFoundError(f"Cannot find {input}")
63-
setattr(args, "input_paths", input_paths)
81+
setattr(args, "input_paths", list(set(input_paths)))
6482
return args
6583

6684

0 commit comments

Comments
 (0)