Skip to content

Commit 411ba59

Browse files
initial commit
1 parent 1a263b4 commit 411ba59

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Diff for: src/diffpy/labpdfproc/tools.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
XQUANTITIES,
88
)
99
from diffpy.utils.tools import (
10+
_load_config,
1011
check_and_build_global_config,
1112
compute_mud,
1213
get_package_info,
@@ -159,6 +160,33 @@ def set_input_lists(args):
159160
return args
160161

161162

163+
def _load_wavelength_from_config_file(args):
164+
"""Load wavelength and anode type from config files.
165+
It takes cli inputs first, and local config, and then global config.
166+
167+
Parameters
168+
----------
169+
args : argparse.Namespace
170+
The arguments from the parser.
171+
172+
Returns
173+
-------
174+
args : argparse.Namespace
175+
The updated arguments with the updated wavelength and anode type.
176+
"""
177+
if args.wavelength or args.anode_type:
178+
return args
179+
global_config = _load_config(Path().home() / "diffpyconfig.json")
180+
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")
187+
return args
188+
189+
162190
def set_wavelength(args):
163191
"""Set the wavelength based on the given anode_type or wavelength.
164192
@@ -185,7 +213,7 @@ def set_wavelength(args):
185213
args : argparse.Namespace
186214
The updated arguments with the wavelength.
187215
"""
188-
# first load values from config file
216+
args = _load_wavelength_from_config_file(args)
189217
if args.wavelength is None and args.anode_type is None:
190218
if args.xtype not in ANGLEQUANTITIES:
191219
raise ValueError(

Diff for: tests/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def user_filesystem(tmp_path):
6060
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
6161

6262
home_config_data = {
63+
"wavelength": 0.25,
6364
"owner_name": "home_username",
6465
"owner_email": "[email protected]",
6566
"owner_orcid": "home_orcid",

Diff for: tests/test_tools.py

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from diffpy.labpdfproc.labpdfprocapp import get_args
88
from diffpy.labpdfproc.tools import (
9+
_load_wavelength_from_config_file,
910
known_sources,
1011
load_metadata,
1112
load_package_info,
@@ -195,6 +196,28 @@ def test_set_output_directory_bad(user_filesystem):
195196
assert not Path(actual_args.output_directory).is_dir()
196197

197198

199+
def test_load_wavelength_from_config_file_with_home_conf_file():
200+
# C1: args provided, return args
201+
# C2: no args, local config exists, return local config
202+
# C3: no args or local config file, return global config
203+
return True
204+
205+
206+
def test_load_wavelength_from_config_file_with_local_conf_file():
207+
# C1: args provided, return args
208+
# C2: no args, return local config file
209+
# remove global config file to test again
210+
return True
211+
212+
213+
def test_load_wavelength_from_config_file_without_conf_files(user_filesystem):
214+
# C1: args provided, return args
215+
cli_inputs = ["2.5", "data.xy", "-w", "0.25"]
216+
actual_args = get_args(cli_inputs)
217+
actual_args = _load_wavelength_from_config_file(actual_args)
218+
assert actual_args.wavelength == 0.25
219+
220+
198221
@pytest.mark.parametrize(
199222
"inputs, expected",
200223
[

0 commit comments

Comments
 (0)