Skip to content

Commit b9be468

Browse files
committed
add function that raises an error if output file(s) already exist, and add a test for this
1 parent 75b02ba commit b9be468

5 files changed

Lines changed: 62 additions & 54 deletions

File tree

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -316,33 +316,11 @@ def get_args_cli(override=None):
316316
return parser.parse_args(argv)
317317

318318

319-
def _check_saved_file_exists(args):
320-
"""Check if the output files already exist based on the input paths
321-
and output directory."""
322-
existing_files = []
323-
for path in args.input_paths:
324-
outfile = args.output_directory / (path.stem + "_corrected.chi")
325-
if outfile.exists() and not args.force:
326-
existing_files.append(outfile)
327-
if args.output_correction:
328-
corrfile = args.output_directory / (path.stem + "_cve.chi")
329-
if corrfile.exists() and not args.force:
330-
existing_files.append(corrfile)
331-
if existing_files:
332-
existing_files_str = "\n".join(str(f) for f in existing_files)
333-
raise FileExistsError(
334-
"The following output files already exist:"
335-
f"\n{existing_files_str}\n"
336-
"Use --force to overwrite them."
337-
)
338-
339-
340319
def main():
341320
use_gui = len(sys.argv) == 1 or "--gui" in sys.argv
342321
args = get_args_gui() if use_gui else get_args_cli()
343322
args = _handle_old_api_conversion(args)
344323
args = preprocessing_args(args)
345-
_check_saved_file_exists(args)
346324
apply_absorption_correction(args)
347325

348326

src/diffpy/labpdfproc/tools.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,27 @@ def load_package_info(args):
528528
return args
529529

530530

531+
def _check_saved_file_exists(args):
532+
"""Check if the output files already exist based on the input paths
533+
and output directory."""
534+
existing_files = []
535+
for path in args.input_paths:
536+
outfile = args.output_directory / (path.stem + "_corrected.chi")
537+
if outfile.exists() and not args.force:
538+
existing_files.append(outfile)
539+
if args.output_correction:
540+
corrfile = args.output_directory / (path.stem + "_cve.chi")
541+
if corrfile.exists() and not args.force:
542+
existing_files.append(corrfile)
543+
if existing_files:
544+
existing_files_str = "\n".join(str(f) for f in existing_files)
545+
raise FileExistsError(
546+
"The following output files already exist:"
547+
f"\n{existing_files_str}\n"
548+
"Use --force to overwrite them."
549+
)
550+
551+
531552
def preprocessing_args(args):
532553
"""Perform preprocessing on the provided args. The process includes
533554
loading package and user information, setting input, output,
@@ -542,6 +563,11 @@ def preprocessing_args(args):
542563
-------
543564
args : argparse.Namespace
544565
The updated argparse Namespace with arguments preprocessed.
566+
567+
Raises
568+
------
569+
FileExistsError
570+
If the output files already exist and --force is not used.
545571
"""
546572
args = load_wavelength_from_config_file(args)
547573
args = set_mud(args)
@@ -552,6 +578,7 @@ def preprocessing_args(args):
552578
args = load_user_metadata(args)
553579
args = load_user_info(args)
554580
args = load_package_info(args)
581+
_check_saved_file_exists(args)
555582
return args
556583

557584

tests/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def user_filesystem(tmp_path):
1313
home_dir.mkdir(parents=True, exist_ok=True)
1414
test_dir = base_dir / "test_dir"
1515
test_dir.mkdir(parents=True, exist_ok=True)
16-
16+
output_dir = base_dir / "output_dir"
17+
output_dir.mkdir(parents=True, exist_ok=True)
1718
chi_data = (
1819
"dataformat = twotheta\n mode = "
1920
"xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n"
@@ -24,10 +25,6 @@ def user_filesystem(tmp_path):
2425
)
2526
binary_data = b"\x00\x01\x02\x03\x04"
2627

27-
with open(base_dir / "data.chi", "w") as f:
28-
f.write(chi_data)
29-
with open(base_dir / "data_corrected.chi", "w") as f:
30-
f.write(chi_data)
3128
with open(base_dir / "good_data.chi", "w") as f:
3229
f.write(chi_data)
3330
with open(base_dir / "good_data.xy", "w") as f:
@@ -63,6 +60,10 @@ def user_filesystem(tmp_path):
6360
f.write("good_data.xy \n")
6461
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
6562

63+
with open(output_dir / "good_data_corrected.chi", "w") as f:
64+
f.write(chi_data)
65+
with open(output_dir / "good_data_cve.chi", "w") as f:
66+
f.write(chi_data)
6667
home_config_data = {
6768
"wavelength": 0.3,
6869
"owner_name": "home_username",

tests/test_labpdfprocapp.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/test_tools.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,32 @@ def test_load_metadata(mocker, user_filesystem, inputs, expected):
847847
**expected,
848848
}
849849
assert actual_metadata == expected_metadata
850+
851+
852+
def test_preprocess_args_bad(user_filesystem):
853+
# Case: user tries to run absorption correction, but the output
854+
# filenames already for *_corrected.chi and *_cve.chi exists.
855+
# expected: preprocess_args catches this early and raises an Error
856+
input_data_file = str(user_filesystem / "good_data.chi")
857+
existing_corrected_file = str(
858+
user_filesystem / "output_dir" / "good_data_corrected.chi"
859+
)
860+
existing_corrected_cve_file = str(
861+
user_filesystem / "output_dir" / "good_data_cve.chi"
862+
)
863+
cli_inputs = (
864+
["mud"]
865+
+ [input_data_file]
866+
+ ["2.5", "-w", "Mo", "-o", str(user_filesystem / "output_dir")]
867+
+ ["-c"] # -c flag saves the cve file
868+
)
869+
args = get_args_cli(cli_inputs)
870+
args = set_input_lists(args)
871+
msg = (
872+
"The following output files already exist:"
873+
f"\n{existing_corrected_file}\n"
874+
f"{existing_corrected_cve_file}\n"
875+
"Use --force to overwrite them."
876+
)
877+
with pytest.raises(FileExistsError, match=re.escape(msg)):
878+
preprocessing_args(args)

0 commit comments

Comments
 (0)