Skip to content

Commit da98d88

Browse files
initial commit on loading username and email
1 parent ea9df03 commit da98d88

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

prevent_commit_to_main.sh

100644100755
File mode changed.

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from diffpy.labpdfproc.functions import apply_corr, compute_cve
55
from diffpy.labpdfproc.tools import (
66
known_sources,
7+
load_user_info,
78
load_user_metadata,
89
set_input_lists,
910
set_output_directory,
@@ -90,12 +91,33 @@ def get_args(override_cli_inputs=None):
9091
"For example, facility='NSLS II', 'facility=NSLS II', beamline=28ID-2, "
9192
"'beamline'='28ID-2', 'favorite color'=blue, are all valid key=value items. ",
9293
)
94+
p.add_argument(
95+
"-n",
96+
"--username",
97+
help="Your username. If not provided here, it can be specified in input "
98+
"or stored in diffpyconfig.json in the current or home directory. "
99+
"Existing config files prevent re-prompting. Values can be overridden here "
100+
"but won't be saved to config files unless it's the first time specifying, "
101+
"which saves to the config file in the home directory.",
102+
default=None,
103+
)
104+
p.add_argument(
105+
"-e",
106+
"--email",
107+
help="Your email. If not provided here, it can be specified in input "
108+
"or stored in diffpyconfig.json in the current or home directory. "
109+
"Existing config files prevent re-prompting. Values can be overridden here "
110+
"but won't be saved to config files unless it's the first time specifying, "
111+
"which saves to the config file in the home directory.",
112+
default=None,
113+
)
93114
args = p.parse_args(override_cli_inputs)
94115
return args
95116

96117

97118
def main():
98119
args = get_args()
120+
args = load_user_info(args)
99121
args = set_input_lists(args)
100122
args.output_directory = set_output_directory(args)
101123
args.wavelength = set_wavelength(args)

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from pathlib import Path
23

34
import pytest
@@ -8,6 +9,10 @@ def user_filesystem(tmp_path):
89
base_dir = Path(tmp_path)
910
input_dir = base_dir / "input_dir"
1011
input_dir.mkdir(parents=True, exist_ok=True)
12+
home_dir = base_dir / "home_dir"
13+
home_dir.mkdir(parents=True, exist_ok=True)
14+
cwd_dir = base_dir / "cwd_dir"
15+
cwd_dir.mkdir(parents=True, exist_ok=True)
1116

1217
chi_data = "dataformat = twotheta\n mode = xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n"
1318
xy_data = "1 2\n 3 4\n 5 6\n 7 8"
@@ -44,4 +49,8 @@ def user_filesystem(tmp_path):
4449
f.write("good_data.xy \n")
4550
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
4651

52+
home_config_data = {"username": "home_username", "email": "[email protected]"}
53+
with open(home_dir / "diffpyconfig.json", "w") as f:
54+
json.dump(home_config_data, f)
55+
4756
yield tmp_path

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import re
34
from pathlib import Path
@@ -7,6 +8,7 @@
78
from diffpy.labpdfproc.labpdfprocapp import get_args
89
from diffpy.labpdfproc.tools import (
910
known_sources,
11+
load_user_info,
1012
load_user_metadata,
1113
set_input_lists,
1214
set_output_directory,
@@ -241,3 +243,119 @@ def test_load_user_metadata_bad(inputs, msg):
241243
actual_args = get_args(cli_inputs)
242244
with pytest.raises(ValueError, match=msg[0]):
243245
actual_args = load_user_metadata(actual_args)
246+
247+
248+
def _setup_dirs(monkeypatch, user_filesystem):
249+
cwd = Path(user_filesystem)
250+
home_dir = cwd / "home_dir"
251+
monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir)
252+
os.chdir(cwd)
253+
return home_dir
254+
255+
256+
def _run_tests(inputs, expected):
257+
expected_username, expected_email = expected
258+
cli_inputs = ["2.5", "data.xy", "--username", inputs[0], "--email", inputs[1]]
259+
actual_args = get_args(cli_inputs)
260+
actual_args = load_user_info(actual_args)
261+
assert actual_args.username == expected_username
262+
assert actual_args.email == expected_email
263+
264+
265+
params_user_info_with_home_conf_file = [
266+
(["", ""], ["home_username", "[email protected]"]),
267+
(["cli_username", ""], ["cli_username", "[email protected]"]),
268+
(["", "[email protected]"], ["home_username", "[email protected]"]),
269+
([None, None], ["home_username", "[email protected]"]),
270+
(["cli_username", None], ["cli_username", "[email protected]"]),
271+
([None, "[email protected]"], ["home_username", "[email protected]"]),
272+
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
273+
]
274+
params_user_info_with_local_conf_file = [
275+
(["", ""], ["cwd_username", "[email protected]"]),
276+
(["cli_username", ""], ["cli_username", "[email protected]"]),
277+
(["", "[email protected]"], ["cwd_username", "[email protected]"]),
278+
([None, None], ["cwd_username", "[email protected]"]),
279+
(["cli_username", None], ["cli_username", "[email protected]"]),
280+
([None, "[email protected]"], ["cwd_username", "[email protected]"]),
281+
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
282+
]
283+
params_user_info_with_no_home_conf_file = [
284+
(
285+
[None, None],
286+
["input_username", "[email protected]"],
287+
["input_username", "[email protected]"],
288+
),
289+
(
290+
["cli_username", None],
291+
292+
["cli_username", "[email protected]"],
293+
),
294+
(
295+
[None, "[email protected]"],
296+
["input_username", ""],
297+
["input_username", "[email protected]"],
298+
),
299+
(
300+
["", ""],
301+
["input_username", "[email protected]"],
302+
["input_username", "[email protected]"],
303+
),
304+
(
305+
["cli_username", ""],
306+
307+
["cli_username", "[email protected]"],
308+
),
309+
(
310+
311+
["input_username", ""],
312+
["input_username", "[email protected]"],
313+
),
314+
(
315+
["cli_username", "[email protected]"],
316+
["input_username", "[email protected]"],
317+
["cli_username", "[email protected]"],
318+
),
319+
]
320+
params_user_info_no_conf_file_no_inputs = [
321+
([None, None], ["", ""], ["", ""]),
322+
]
323+
324+
325+
@pytest.mark.parametrize("inputs, expected", params_user_info_with_home_conf_file)
326+
def test_load_user_info_with_home_conf_file(monkeypatch, inputs, expected, user_filesystem):
327+
_setup_dirs(monkeypatch, user_filesystem)
328+
_run_tests(inputs, expected)
329+
330+
331+
@pytest.mark.parametrize("inputs, expected", params_user_info_with_local_conf_file)
332+
def test_load_user_info_with_local_conf_file(monkeypatch, inputs, expected, user_filesystem):
333+
_setup_dirs(monkeypatch, user_filesystem)
334+
local_config_data = {"username": "cwd_username", "email": "[email protected]"}
335+
with open(Path(user_filesystem) / "diffpyconfig.json", "w") as f:
336+
json.dump(local_config_data, f)
337+
_run_tests(inputs, expected)
338+
os.remove(Path().home() / "diffpyconfig.json")
339+
_run_tests(inputs, expected)
340+
341+
342+
@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file)
343+
def test_load_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem):
344+
_setup_dirs(monkeypatch, user_filesystem)
345+
os.remove(Path().home() / "diffpyconfig.json")
346+
inp_iter = iter(inputsb)
347+
monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
348+
_run_tests(inputsa, expected)
349+
confile = Path().home() / "diffpyconfig.json"
350+
assert confile.is_file()
351+
352+
353+
@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs)
354+
def test_load_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem):
355+
_setup_dirs(monkeypatch, user_filesystem)
356+
os.remove(Path().home() / "diffpyconfig.json")
357+
inp_iter = iter(inputsb)
358+
monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
359+
_run_tests(inputsa, expected)
360+
confile = Path().home() / "diffpyconfig.json"
361+
assert confile.exists() is False

src/diffpy/labpdfproc/tools.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
from diffpy.utils.tools import get_user_info
4+
35
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
46
known_sources = [key for key in WAVELENGTHS.keys()]
57

@@ -171,3 +173,24 @@ def load_user_metadata(args):
171173
setattr(args, key, value)
172174
delattr(args, "user_metadata")
173175
return args
176+
177+
178+
def load_user_info(args):
179+
"""
180+
Update username and email using get_user_info function from diffpy.utils
181+
182+
Parameters
183+
----------
184+
args argparse.Namespace
185+
the arguments from the parser, default is None
186+
187+
Returns
188+
-------
189+
the updated argparse Namespace with username and email inserted
190+
191+
"""
192+
config = {"username": args.username, "email": args.email}
193+
config = get_user_info(config)
194+
args.username = config["username"]
195+
args.email = config["email"]
196+
return args

0 commit comments

Comments
 (0)