Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/examples/labpdfprocapp-example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
24 changes: 24 additions & 0 deletions news/check-saved-files.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
**Added:**

* <news item>

**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``.

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
12 changes: 4 additions & 8 deletions src/diffpy/labpdfproc/labpdfprocapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 / (input_path.stem + "-mud-corrected.chi")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no biggie, but as a general rule, let's compose as f-strings, not using +

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
correction.metadata = correction.metadata or {}
correction.dump(str(corrfile), xtype=args.xtype)
print(f"Saved correction data to {corrfile}")
Expand Down Expand Up @@ -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():
Expand Down
27 changes: 27 additions & 0 deletions src/diffpy/labpdfproc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 / (path.stem + "-mud-corrected.chi")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

if outfile.exists() and not args.force:
existing_files.append(outfile)
if args.output_correction:
corrfile = args.output_directory / (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,
Expand All @@ -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)
Expand All @@ -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


Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,32 @@ def test_load_metadata(mocker, user_filesystem, inputs, expected):
**expected,
}
assert actual_metadata == expected_metadata


def test_preprocess_args_bad(user_filesystem):
# 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
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", "-w", "Mo", "-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)
Loading