Skip to content

Commit 24e8288

Browse files
change tmp_dir to user_filesystem to make tests more readable
2 parents 311bc56 + 1393a45 commit 24e8288

File tree

3 files changed

+118
-5
lines changed

3 files changed

+118
-5
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44

55
from diffpy.labpdfproc.functions import apply_corr, compute_cve
6-
from diffpy.labpdfproc.tools import known_sources, set_output_directory, set_wavelength
6+
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
77
from diffpy.utils.parsers.loaddata import loadData
88
from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES, Diffraction_object
99

@@ -58,6 +58,18 @@ def get_args(override_cli_inputs=None):
5858
action="store_true",
5959
help="Outputs will not overwrite existing file unless --force is specified.",
6060
)
61+
p.add_argument(
62+
"-u",
63+
"--user-metadata",
64+
metavar="KEY=VALUE",
65+
nargs="+",
66+
help="Specify key-value pairs to be loaded into metadata using the format key=value. "
67+
"Separate pairs with whitespace, and ensure no whitespaces before or after the = sign. "
68+
"Avoid using = in keys. If multiple = signs are present, only the first separates the key and value. "
69+
"If a key or value contains whitespace, enclose it in quotes. "
70+
"For example, facility='NSLS II', 'facility=NSLS II', beamline=28ID-2, "
71+
"'beamline'='28ID-2', 'favorite color'=blue, are all valid key=value items. ",
72+
)
6173
args = p.parse_args(override_cli_inputs)
6274
return args
6375

@@ -66,6 +78,7 @@ def main():
6678
args = get_args()
6779
args.output_directory = set_output_directory(args)
6880
args.wavelength = set_wavelength(args)
81+
args = load_user_metadata(args)
6982

7083
filepath = Path(args.input_file)
7184
outfilestem = filepath.stem + "_corrected"

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from diffpy.labpdfproc.labpdfprocapp import get_args
7-
from diffpy.labpdfproc.tools import known_sources, set_output_directory, set_wavelength
7+
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
88

99
params1 = [
1010
([], ["."]),
@@ -16,9 +16,7 @@
1616

1717
@pytest.mark.parametrize("inputs, expected", params1)
1818
def test_set_output_directory(inputs, expected, user_filesystem):
19-
tmp_dir = user_filesystem
20-
expected_output_directory = tmp_dir / expected[0]
21-
19+
expected_output_directory = Path(user_filesystem) / expected[0]
2220
cli_inputs = ["2.5"] + inputs
2321
actual_args = get_args(cli_inputs)
2422
actual_args.output_directory = set_output_directory(actual_args)
@@ -75,3 +73,63 @@ def test_set_wavelength_bad(inputs, msg):
7573
actual_args = get_args(cli_inputs)
7674
with pytest.raises(ValueError, match=re.escape(msg[0])):
7775
actual_args.wavelength = set_wavelength(actual_args)
76+
77+
78+
params5 = [
79+
([], []),
80+
(
81+
["--user-metadata", "facility=NSLS II", "beamline=28ID-2", "favorite color=blue"],
82+
[["facility", "NSLS II"], ["beamline", "28ID-2"], ["favorite color", "blue"]],
83+
),
84+
(["--user-metadata", "x=y=z"], [["x", "y=z"]]),
85+
]
86+
87+
88+
@pytest.mark.parametrize("inputs, expected", params5)
89+
def test_load_user_metadata(inputs, expected):
90+
expected_args = get_args(["2.5"])
91+
for expected_pair in expected:
92+
setattr(expected_args, expected_pair[0], expected_pair[1])
93+
delattr(expected_args, "user_metadata")
94+
95+
cli_inputs = ["2.5"] + inputs
96+
actual_args = get_args(cli_inputs)
97+
actual_args = load_user_metadata(actual_args)
98+
assert actual_args == expected_args
99+
100+
101+
params6 = [
102+
(
103+
["--user-metadata", "facility=", "NSLS II"],
104+
[
105+
"Please provide key-value pairs in the format key=value. "
106+
"For more information, use `labpdfproc --help.`"
107+
],
108+
),
109+
(
110+
["--user-metadata", "favorite", "color=blue"],
111+
"Please provide key-value pairs in the format key=value. "
112+
"For more information, use `labpdfproc --help.`",
113+
),
114+
(
115+
["--user-metadata", "beamline", "=", "28ID-2"],
116+
"Please provide key-value pairs in the format key=value. "
117+
"For more information, use `labpdfproc --help.`",
118+
),
119+
(
120+
["--user-metadata", "facility=NSLS II", "facility=NSLS III"],
121+
"Please do not specify repeated keys: facility. ",
122+
),
123+
(
124+
["--user-metadata", "wavelength=2"],
125+
"wavelength is a reserved name. Please rerun using a different key name. ",
126+
),
127+
]
128+
129+
130+
@pytest.mark.parametrize("inputs, msg", params6)
131+
def test_load_user_metadata_bad(inputs, msg):
132+
cli_inputs = ["2.5"] + inputs
133+
actual_args = get_args(cli_inputs)
134+
with pytest.raises(ValueError, match=msg[0]):
135+
actual_args = load_user_metadata(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,45 @@ def set_wavelength(args):
6060
return WAVELENGTHS[args.anode_type]
6161
else:
6262
return WAVELENGTHS["Mo"]
63+
64+
65+
def _load_key_value_pair(s):
66+
items = s.split("=")
67+
key = items[0].strip()
68+
if len(items) > 1:
69+
value = "=".join(items[1:])
70+
return (key, value)
71+
72+
73+
def load_user_metadata(args):
74+
"""
75+
Load user metadata into the provided argparse Namespace, raise ValueError if in incorrect format
76+
77+
Parameters
78+
----------
79+
args argparse.Namespace
80+
the arguments from the parser
81+
82+
Returns
83+
-------
84+
the updated argparse Namespace with user metadata inserted as key-value pairs
85+
86+
"""
87+
88+
reserved_keys = vars(args).keys()
89+
90+
if args.user_metadata:
91+
for item in args.user_metadata:
92+
if "=" not in item:
93+
raise ValueError(
94+
"Please provide key-value pairs in the format key=value. "
95+
"For more information, use `labpdfproc --help.`"
96+
)
97+
key, value = _load_key_value_pair(item)
98+
if key in reserved_keys:
99+
raise ValueError(f"{key} is a reserved name. Please rerun using a different key name. ")
100+
if hasattr(args, key):
101+
raise ValueError(f"Please do not specify repeated keys: {key}. ")
102+
setattr(args, key, value)
103+
delattr(args, "user_metadata")
104+
return args

0 commit comments

Comments
 (0)