diff --git a/docs/source/examples/labpdfprocapp-example.rst b/docs/source/examples/labpdfprocapp-example.rst index c33cadf..249c5de 100644 --- a/docs/source/examples/labpdfprocapp-example.rst +++ b/docs/source/examples/labpdfprocapp-example.rst @@ -79,7 +79,7 @@ For example, labpdfproc mud zro2_mo.xy 2.5 -w 0.71303 labpdfproc mud zro2_mo.xy 2.5 -w Mo -This will then save the corrected file in the same directory as the input file with the name ``zro2_mo_corrected.chi``. +This will then save the corrected file in the same directory as the input file with the name ``zro2_mo-mud-corrected.chi``. To save the correction file, specify the ``-c`` or ``--output-correction`` flag, @@ -87,7 +87,7 @@ To save the correction file, specify the ``-c`` or ``--output-correction`` flag, labpdfproc mud zro2_mo.xy 2.5 -w 0.71303 -c -This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``. +This will then save the correction file in the same directory as the input file with the name ``zro2_mo-cve.chi``. ``labpdfproc zscan`` Command ---------------------------- @@ -125,7 +125,7 @@ To save the correction file, specify the ``-c`` or ``--output-correction`` flag, labpdfproc zscan CeO2_635um_accum_0.xy CeO2_635um_zscan.xy -w 0.71303 -c -This will then save the correction file in the same directory as the input file with the name ``CeO2_635um_accum_0_cve.chi``. +This will then save the correction file in the same directory as the input file with the name ``CeO2_635um_accum_0-cve.chi``. ``labpdfproc sample`` Command ----------------------------- @@ -165,7 +165,7 @@ To save the correction file, specify the ``-c`` or ``--output-correction`` flag, labpdfproc sample zro2_mo.xy ZrO2 17.45 1.2 -w 0.71303 -c -This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``. +This will then save the correction file in the same directory as the input file with the name ``zro2_mo-cve.chi``. Additional CLI options ---------------------- diff --git a/news/check-saved-files.rst b/news/check-saved-files.rst new file mode 100644 index 0000000..514ba98 --- /dev/null +++ b/news/check-saved-files.rst @@ -0,0 +1,25 @@ +**Added:** + +* + +**Changed:** + +* Changed output file behavior to raise an error early if the saved file exists. +* Changed saved file name from ``_corrected.chi`` to ``-mud-corrected.chi``. +* Changed saved file name from ``_cve.chi`` to ``-cve.chi``. + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/labpdfproc/labpdfprocapp.py b/src/diffpy/labpdfproc/labpdfprocapp.py index e5dd808..db7f873 100644 --- a/src/diffpy/labpdfproc/labpdfprocapp.py +++ b/src/diffpy/labpdfproc/labpdfprocapp.py @@ -131,20 +131,14 @@ def _add_credit_args(parser, use_gui=False): def _save_corrected(corrected, input_path, args): - outfile = args.output_directory / (input_path.stem + "_corrected.chi") - if outfile.exists() and not args.force: - print(f"WARNING: {outfile} exists. Use --force to overwrite.") - return + outfile = args.output_directory / (f"{input_path.stem}-mud-corrected.chi") corrected.metadata = corrected.metadata or {} corrected.dump(str(outfile), xtype=args.xtype) print(f"Saved corrected data to {outfile}") def _save_correction(correction, input_path, args): - corrfile = args.output_directory / (input_path.stem + "_cve.chi") - if corrfile.exists() and not args.force: - print(f"WARNING: {corrfile} exists. Use --force to overwrite.") - return + corrfile = args.output_directory / (f"{input_path.stem}-cve.chi") correction.metadata = correction.metadata or {} correction.dump(str(corrfile), xtype=args.xtype) print(f"Saved correction data to {corrfile}") @@ -312,7 +306,9 @@ def get_args_gui(): def get_args_cli(override=None): parser = create_parser(use_gui=False) - return parser.parse_args(override) + argv = override if override is not None else sys.argv[1:] + argv = [arg for arg in argv if arg != "--ignore-gooey"] + return parser.parse_args(argv) def main(): diff --git a/src/diffpy/labpdfproc/tools.py b/src/diffpy/labpdfproc/tools.py index 2aa6754..305291e 100644 --- a/src/diffpy/labpdfproc/tools.py +++ b/src/diffpy/labpdfproc/tools.py @@ -528,6 +528,27 @@ def load_package_info(args): return args +def _check_saved_file_exists(args): + """Check if the output files already exist based on the input paths + and output directory.""" + existing_files = [] + for path in args.input_paths: + outfile = args.output_directory / (f"{path.stem}-mud-corrected.chi") + if outfile.exists() and not args.force: + existing_files.append(outfile) + if args.output_correction: + corrfile = args.output_directory / (f"{path.stem}-cve.chi") + if corrfile.exists() and not args.force: + existing_files.append(corrfile) + if existing_files: + existing_files_str = "\n".join(str(f) for f in existing_files) + raise FileExistsError( + "The following output files already exist:" + f"\n{existing_files_str}\n" + "Use --force to overwrite them." + ) + + def preprocessing_args(args): """Perform preprocessing on the provided args. The process includes loading package and user information, setting input, output, @@ -542,6 +563,11 @@ def preprocessing_args(args): ------- args : argparse.Namespace The updated argparse Namespace with arguments preprocessed. + + Raises + ------ + FileExistsError + If the output files already exist and --force is not used. """ args = load_wavelength_from_config_file(args) args = set_mud(args) @@ -552,6 +578,7 @@ def preprocessing_args(args): args = load_user_metadata(args) args = load_user_info(args) args = load_package_info(args) + _check_saved_file_exists(args) return args diff --git a/tests/conftest.py b/tests/conftest.py index 63d4646..cda2974 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,7 +13,8 @@ def user_filesystem(tmp_path): home_dir.mkdir(parents=True, exist_ok=True) test_dir = base_dir / "test_dir" test_dir.mkdir(parents=True, exist_ok=True) - + output_dir = base_dir / "output_dir" + output_dir.mkdir(parents=True, exist_ok=True) chi_data = ( "dataformat = twotheta\n mode = " "xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n" @@ -59,6 +60,10 @@ def user_filesystem(tmp_path): f.write("good_data.xy \n") f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n") + with open(output_dir / "good_data-mud-corrected.chi", "w") as f: + f.write(chi_data) + with open(output_dir / "good_data-cve.chi", "w") as f: + f.write(chi_data) home_config_data = { "wavelength": 0.3, "owner_name": "home_username", diff --git a/tests/test_tools.py b/tests/test_tools.py index e7a137e..9112699 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -847,3 +847,38 @@ def test_load_metadata(mocker, user_filesystem, inputs, expected): **expected, } assert actual_metadata == expected_metadata + + +def test_preprocess_args_bad(user_filesystem, monkeypatch): + # Case: user tries to run absorption correction, but the output + # filenames already for *-mud-corrected.chi and *-cve.chi exists. + # expected: preprocess_args catches this early and raises an Error + cwd = Path(user_filesystem) + home_dir = cwd / "home_dir" + # set cwd so program can find diffpyconfig.json + monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir) + input_data_file = str(user_filesystem / "good_data.chi") + existing_corrected_file = str( + user_filesystem / "output_dir" / "good_data-mud-corrected.chi" + ) + existing_corrected_cve_file = str( + user_filesystem / "output_dir" / "good_data-cve.chi" + ) + cli_inputs = [ + "mud", + input_data_file, + "2.5", + "-o", + str(user_filesystem / "output_dir"), + "-c", # -c flag saves the cve file + ] + args = get_args_cli(cli_inputs) + args = set_input_lists(args) + msg = ( + "The following output files already exist:" + f"\n{existing_corrected_file}\n" + f"{existing_corrected_cve_file}\n" + "Use --force to overwrite them." + ) + with pytest.raises(FileExistsError, match=re.escape(msg)): + preprocessing_args(args)