Skip to content

Commit 79dae25

Browse files
feat: print message if no wavelength info in config; clarify test comments on loading vs validation
1 parent c6c8b67 commit 79dae25

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

news/wavelength-config.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
**Added:**
22

3-
* Functionality to allow users to specify wavelength or anode type in a diffpy config file.
3+
* Functionality to read wavelength and anode type directly from a diffpy configuration file.
44

55
**Changed:**
66

src/diffpy/labpdfproc/tools.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ def set_input_lists(args):
160160
return args
161161

162162

163-
def _load_wavelength_from_config_file(args):
163+
def load_wavelength_from_config_file(args):
164164
"""Load wavelength and anode type from config files.
165-
It takes cli inputs first, and local config, and then global config.
165+
166+
It prioritizes values in the following order:
167+
1. cli inputs, 2. local config file, 3. global config file.
166168
167169
Parameters
168170
----------
@@ -174,16 +176,33 @@ def _load_wavelength_from_config_file(args):
174176
args : argparse.Namespace
175177
The updated arguments with the updated wavelength and anode type.
176178
"""
177-
if args.wavelength or args.anode_type:
178-
return args
179179
global_config = _load_config(Path().home() / "diffpyconfig.json")
180180
local_config = _load_config(Path().cwd() / "diffpyconfig.json")
181-
if local_config:
182-
args.wavelength = local_config.get("wavelength")
183-
args.anode_type = local_config.get("anode_type")
184-
elif global_config:
185-
args.wavelength = global_config.get("wavelength")
186-
args.anode_type = global_config.get("anode_type")
181+
local_has_data = local_config and (
182+
"wavelength" in local_config or "anode_type" in local_config
183+
)
184+
global_has_data = global_config and (
185+
"wavelength" in global_config or "anode_type" in global_config
186+
)
187+
if not local_has_data and not global_has_data:
188+
print(
189+
"No configuration file was found containing information "
190+
"about the wavelength or anode type. \n"
191+
"You can add the wavelength or anode type "
192+
"to a configuration file on the current computer "
193+
"and it will be automatically associated with "
194+
"subsequent diffpy data by default. \n"
195+
"You will only have to do that once. \n"
196+
"For more information, please refer to www.diffpy.org/"
197+
"diffpy.labpdfproc/examples/toolsexample.html"
198+
)
199+
200+
if args.wavelength or args.anode_type:
201+
return args
202+
config = local_config if local_has_data else global_config
203+
if config:
204+
args.wavelength = args.wavelength or config.get("wavelength")
205+
args.anode_type = args.anode_type or config.get("anode_type")
187206
return args
188207

189208

@@ -202,7 +221,7 @@ def set_wavelength(args):
202221
------
203222
ValueError
204223
Raised if:
205-
(1) neither wavelength or anode type is provided,
224+
(1) neither wavelength or anode type is provided
206225
and xtype is not the two-theta grid,
207226
(2) both are provided,
208227
(3) anode_type is not one of the known sources,
@@ -213,7 +232,7 @@ def set_wavelength(args):
213232
args : argparse.Namespace
214233
The updated arguments with the wavelength.
215234
"""
216-
args = _load_wavelength_from_config_file(args)
235+
args = load_wavelength_from_config_file(args)
217236
if args.wavelength is None and args.anode_type is None:
218237
if args.xtype not in ANGLEQUANTITIES:
219238
raise ValueError(

tests/test_tools.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
from diffpy.labpdfproc.labpdfprocapp import get_args
99
from diffpy.labpdfproc.tools import (
10-
_load_wavelength_from_config_file,
1110
known_sources,
1211
load_metadata,
1312
load_package_info,
1413
load_user_info,
1514
load_user_metadata,
15+
load_wavelength_from_config_file,
1616
preprocessing_args,
1717
set_input_lists,
1818
set_mud,
@@ -200,9 +200,13 @@ def test_set_output_directory_bad(user_filesystem):
200200
@pytest.mark.parametrize(
201201
"inputs, expected",
202202
[
203-
# Test when only a home config file exists (no local config file),
204-
# expect to return args if wavelength or anode type is specified,
205-
# otherwise update args with values from the home config file.
203+
# Test with only a home config file (no local config),
204+
# expect to return values directly from args
205+
# if either wavelength or anode type is specified,
206+
# otherwise update args with values from the home config file
207+
# (wavelength=0.3, no anode type).
208+
# This test only checks loading behavior,
209+
# not value validation (which is handled by `set_wavelength`).
206210
# C1: no args, expect to update arg values from home config
207211
([""], {"wavelength": 0.3, "anode_type": None}),
208212
# C2: wavelength provided, expect to return args unchanged
@@ -227,7 +231,7 @@ def test_load_wavelength_from_config_file_with_home_conf_file(
227231

228232
cli_inputs = ["2.5", "data.xy"] + inputs
229233
actual_args = get_args(cli_inputs)
230-
actual_args = _load_wavelength_from_config_file(actual_args)
234+
actual_args = load_wavelength_from_config_file(actual_args)
231235
assert actual_args.wavelength == expected["wavelength"]
232236
assert actual_args.anode_type == expected["anode_type"]
233237

@@ -236,9 +240,13 @@ def test_load_wavelength_from_config_file_with_home_conf_file(
236240
"inputs, expected",
237241
[
238242
# Test when a local config file exists,
239-
# expect to return args if wavelength or anode type is specified,
240-
# otherwise update args with values from the home config file.
243+
# expect to return values directly from args
244+
# if either wavelength or anode type is specified,
245+
# otherwise update args with values from the local config file
246+
# (wavelength=0.6, no anode type).
241247
# Results should be the same whether if the home config exists.
248+
# This test only checks loading behavior,
249+
# not value validation (which is handled by `set_wavelength`).
242250
# C1: no args, expect to update arg values from local config
243251
([""], {"wavelength": 0.6, "anode_type": None}),
244252
# C2: wavelength provided, expect to return args unchanged
@@ -266,7 +274,7 @@ def test_load_wavelength_from_config_file_with_local_conf_file(
266274

267275
cli_inputs = ["2.5", "data.xy"] + inputs
268276
actual_args = get_args(cli_inputs)
269-
actual_args = _load_wavelength_from_config_file(actual_args)
277+
actual_args = load_wavelength_from_config_file(actual_args)
270278
assert actual_args.wavelength == expected["wavelength"]
271279
assert actual_args.anode_type == expected["anode_type"]
272280

@@ -282,6 +290,8 @@ def test_load_wavelength_from_config_file_with_local_conf_file(
282290
[
283291
# Test when no config files exist,
284292
# expect to return args without modification.
293+
# This test only checks loading behavior,
294+
# not value validation (which is handled by `set_wavelength`).
285295
# C1: no args
286296
([""], {"wavelength": None, "anode_type": None}),
287297
# C1: wavelength provided
@@ -307,7 +317,7 @@ def test_load_wavelength_from_config_file_without_conf_files(
307317

308318
cli_inputs = ["2.5", "data.xy"] + inputs
309319
actual_args = get_args(cli_inputs)
310-
actual_args = _load_wavelength_from_config_file(actual_args)
320+
actual_args = load_wavelength_from_config_file(actual_args)
311321
assert actual_args.wavelength == expected["wavelength"]
312322
assert actual_args.anode_type == expected["anode_type"]
313323

0 commit comments

Comments
 (0)