Skip to content

Commit 193fc82

Browse files
added more examples to help message, added reserve keys, added get_args for tests
1 parent 4d28a1b commit 193fc82

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES, Diffraction_object
99

1010

11-
def get_args():
11+
def get_args(override_cli_inputs=None):
1212
p = ArgumentParser()
1313
p.add_argument("mud", help="Value of mu*D for your " "sample. Required.", type=float)
1414
p.add_argument("-i", "--input-file", help="The filename of the " "datafile to load.")
@@ -66,10 +66,11 @@ def get_args():
6666
help="Specify key-value pairs to be loaded into metadata using the format key=value. "
6767
"Separate pairs with whitespace, and ensure no whitespaces before or after the = sign. "
6868
"Avoid using = in keys. If multiple = signs are present, only the first separates the key and value. "
69-
"Please do not repeat key names. If a key or value contains whitespace, enclose it in quotes. "
70-
'For example, you can specify -u "facility=NSLS II" beamline=28ID-2 "favorite color"=blue',
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. ",
7172
)
72-
args = p.parse_args()
73+
args = p.parse_args(override_cli_inputs)
7374
return args
7475

7576

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import argparse
22
import os
33
import re
4-
from argparse import ArgumentParser
54
from pathlib import Path
65

76
import pytest
87

8+
from diffpy.labpdfproc.labpdfprocapp import get_args
99
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
1010

1111
params1 = [
@@ -80,57 +80,54 @@ def test_set_wavelength_bad(inputs, msg):
8080

8181

8282
params5 = [
83-
([None], []),
83+
([], []),
8484
(
85-
[["facility=NSLS II", "beamline=28ID-2", "favorite color=blue"]],
85+
["-u", "facility=NSLS II", "beamline=28ID-2", "favorite color=blue"],
8686
[["facility", "NSLS II"], ["beamline", "28ID-2"], ["favorite color", "blue"]],
8787
),
88-
([["x=y=z"]], [["x", "y=z"]]),
88+
(["-u", "x=y=z"], [["x", "y=z"]]),
8989
]
9090

9191

9292
@pytest.mark.parametrize("inputs, expected", params5)
9393
def test_load_user_metadata(inputs, expected):
94-
expected_parser = ArgumentParser()
95-
expected_args = expected_parser.parse_args([])
94+
expected_args = get_args(["2.5"])
9695
for expected_pair in expected:
9796
setattr(expected_args, expected_pair[0], expected_pair[1])
97+
delattr(expected_args, "user_metadata")
9898

99-
actual_parser = ArgumentParser()
100-
actual_parser.add_argument("--user-metadata")
101-
actual_args = actual_parser.parse_args(["--user-metadata", inputs[0]])
99+
cli_inputs = ["2.5"] + inputs
100+
actual_args = get_args(cli_inputs)
102101
actual_args = load_user_metadata(actual_args)
103102
assert actual_args == expected_args
104103

105104

106105
params6 = [
107106
(
108-
[["facility=NSLS", "II"]],
107+
["-u", "facility=", "NSLS II"],
109108
[
110109
"Please provide key-value pairs in the format key=value. "
111110
"For more information, use `labpdfproc --help.`"
112111
],
113112
),
114113
(
115-
[["favorite", "color=blue"]],
114+
["-u", "favorite", "color=blue"],
116115
"Please provide key-value pairs in the format key=value. "
117116
"For more information, use `labpdfproc --help.`",
118117
),
119118
(
120-
[["beamline", "=", "28ID-2"]],
119+
["-u", "beamline", "=", "28ID-2"],
121120
"Please provide key-value pairs in the format key=value. "
122121
"For more information, use `labpdfproc --help.`",
123122
),
124-
([["facility=NSLS II", "facility=NSLS III"]], ["Please do not specify repeated keys: facility. "]),
125-
([["wavelength=2"]], ["Please do not specify repeated keys: wavelength. "]),
123+
(["-u", "facility=NSLS II", "facility=NSLS III"], "Please do not specify repeated keys: facility. "),
124+
(["-u", "wavelength=2"], "wavelength is a reserved name. Please rerun using a different key name. "),
126125
]
127126

128127

129128
@pytest.mark.parametrize("inputs, msg", params6)
130129
def test_load_user_metadata_bad(inputs, msg):
131-
actual_parser = ArgumentParser()
132-
actual_parser.add_argument("--wavelength")
133-
actual_parser.add_argument("--user-metadata")
134-
actual_args = actual_parser.parse_args(["--user-metadata", inputs[0]])
130+
cli_inputs = ["2.5"] + inputs
131+
actual_args = get_args(cli_inputs)
135132
with pytest.raises(ValueError, match=msg[0]):
136133
actual_args = load_user_metadata(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def load_user_metadata(args):
8585
8686
"""
8787

88+
reserved_keys = vars(args).keys()
89+
8890
if args.user_metadata:
8991
for item in args.user_metadata:
9092
if "=" not in item:
@@ -93,6 +95,8 @@ def load_user_metadata(args):
9395
"For more information, use `labpdfproc --help.`"
9496
)
9597
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. ")
96100
if hasattr(args, key):
97101
raise ValueError(f"Please do not specify repeated keys: {key}. ")
98102
setattr(args, key, value)

0 commit comments

Comments
 (0)