Skip to content

Commit 9dda146

Browse files
authored
Merge pull request #223 from cadenmyers13/check-saved-files
feat: Check if saved files exists and raise an early error if they do
2 parents ffac9ca + 5125fa9 commit 9dda146

6 files changed

Lines changed: 102 additions & 14 deletions

File tree

docs/source/examples/labpdfprocapp-example.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ For example,
7777
labpdfproc mud zro2_mo.xy 2.5 -w 0.71303
7878
labpdfproc mud zro2_mo.xy 2.5 -w Mo
7979
80-
This will then save the corrected file in the same directory as the input file with the name ``zro2_mo_corrected.chi``.
80+
This will then save the corrected file in the same directory as the input file with the name ``zro2_mo-mud-corrected.chi``.
8181

8282
To save the correction file, specify the ``-c`` or ``--output-correction`` flag,
8383

8484
.. code-block:: bash
8585
8686
labpdfproc mud zro2_mo.xy 2.5 -w 0.71303 -c
8787
88-
This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``.
88+
This will then save the correction file in the same directory as the input file with the name ``zro2_mo-cve.chi``.
8989

9090
``labpdfproc zscan`` Command
9191
----------------------------
@@ -123,7 +123,7 @@ To save the correction file, specify the ``-c`` or ``--output-correction`` flag,
123123
124124
labpdfproc zscan CeO2_635um_accum_0.xy CeO2_635um_zscan.xy -w 0.71303 -c
125125
126-
This will then save the correction file in the same directory as the input file with the name ``CeO2_635um_accum_0_cve.chi``.
126+
This will then save the correction file in the same directory as the input file with the name ``CeO2_635um_accum_0-cve.chi``.
127127

128128
``labpdfproc sample`` Command
129129
-----------------------------
@@ -163,7 +163,7 @@ To save the correction file, specify the ``-c`` or ``--output-correction`` flag,
163163
164164
labpdfproc sample zro2_mo.xy ZrO2 17.45 1.2 -w 0.71303 -c
165165
166-
This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``.
166+
This will then save the correction file in the same directory as the input file with the name ``zro2_mo-cve.chi``.
167167

168168
Additional CLI options
169169
----------------------

news/check-saved-files.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* Changed output file behavior to raise an error early if the saved file exists.
8+
* Changed saved file name from ``_corrected.chi`` to ``-mud-corrected.chi``.
9+
* Changed saved file name from ``_cve.chi`` to ``-cve.chi``.
10+
11+
**Deprecated:**
12+
13+
* <news item>
14+
15+
**Removed:**
16+
17+
* <news item>
18+
19+
**Fixed:**
20+
21+
* <news item>
22+
23+
**Security:**
24+
25+
* <news item>

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,14 @@ def _add_credit_args(parser, use_gui=False):
131131

132132

133133
def _save_corrected(corrected, input_path, args):
134-
outfile = args.output_directory / (input_path.stem + "_corrected.chi")
135-
if outfile.exists() and not args.force:
136-
print(f"WARNING: {outfile} exists. Use --force to overwrite.")
137-
return
134+
outfile = args.output_directory / (f"{input_path.stem}-mud-corrected.chi")
138135
corrected.metadata = corrected.metadata or {}
139136
corrected.dump(str(outfile), xtype=args.xtype)
140137
print(f"Saved corrected data to {outfile}")
141138

142139

143140
def _save_correction(correction, input_path, args):
144-
corrfile = args.output_directory / (input_path.stem + "_cve.chi")
145-
if corrfile.exists() and not args.force:
146-
print(f"WARNING: {corrfile} exists. Use --force to overwrite.")
147-
return
141+
corrfile = args.output_directory / (f"{input_path.stem}-cve.chi")
148142
correction.metadata = correction.metadata or {}
149143
correction.dump(str(corrfile), xtype=args.xtype)
150144
print(f"Saved correction data to {corrfile}")
@@ -312,7 +306,9 @@ def get_args_gui():
312306

313307
def get_args_cli(override=None):
314308
parser = create_parser(use_gui=False)
315-
return parser.parse_args(override)
309+
argv = override if override is not None else sys.argv[1:]
310+
argv = [arg for arg in argv if arg != "--ignore-gooey"]
311+
return parser.parse_args(argv)
316312

317313

318314
def main():

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 / (f"{path.stem}-mud-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 / (f"{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 & 1 deletion
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"
@@ -59,6 +60,10 @@ def user_filesystem(tmp_path):
5960
f.write("good_data.xy \n")
6061
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
6162

63+
with open(output_dir / "good_data-mud-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)
6267
home_config_data = {
6368
"wavelength": 0.3,
6469
"owner_name": "home_username",

tests/test_tools.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,38 @@ 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, monkeypatch):
853+
# Case: user tries to run absorption correction, but the output
854+
# filenames already for *-mud-corrected.chi and *-cve.chi exists.
855+
# expected: preprocess_args catches this early and raises an Error
856+
cwd = Path(user_filesystem)
857+
home_dir = cwd / "home_dir"
858+
# set cwd so program can find diffpyconfig.json
859+
monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir)
860+
input_data_file = str(user_filesystem / "good_data.chi")
861+
existing_corrected_file = str(
862+
user_filesystem / "output_dir" / "good_data-mud-corrected.chi"
863+
)
864+
existing_corrected_cve_file = str(
865+
user_filesystem / "output_dir" / "good_data-cve.chi"
866+
)
867+
cli_inputs = [
868+
"mud",
869+
input_data_file,
870+
"2.5",
871+
"-o",
872+
str(user_filesystem / "output_dir"),
873+
"-c", # -c flag saves the cve file
874+
]
875+
args = get_args_cli(cli_inputs)
876+
args = set_input_lists(args)
877+
msg = (
878+
"The following output files already exist:"
879+
f"\n{existing_corrected_file}\n"
880+
f"{existing_corrected_cve_file}\n"
881+
"Use --force to overwrite them."
882+
)
883+
with pytest.raises(FileExistsError, match=re.escape(msg)):
884+
preprocessing_args(args)

0 commit comments

Comments
 (0)