Skip to content

Commit bf1a542

Browse files
authored
Merge pull request #77 from yucongalicechen/user2
loading username and email using function in diffpyutils
2 parents 00ffee6 + 5b5046c commit bf1a542

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

prevent_commit_to_main.sh

100644100755
File mode changed.

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 16 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,27 @@ 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="Username will be loaded from config files. Specify here "
98+
"only if you want to override that behavior at runtime. ",
99+
default=None,
100+
)
101+
p.add_argument(
102+
"-e",
103+
"--email",
104+
help="Email will be loaded from config files. Specify here "
105+
"only if you want to override that behavior at runtime. ",
106+
default=None,
107+
)
93108
args = p.parse_args(override_cli_inputs)
94109
return args
95110

96111

97112
def main():
98113
args = get_args()
114+
args = load_user_info(args)
99115
args = set_input_lists(args)
100116
args.output_directory = set_output_directory(args)
101117
args.wavelength = set_wavelength(args)

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 7 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,8 @@ 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)
1114

1215
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"
1316
xy_data = "1 2\n 3 4\n 5 6\n 7 8"
@@ -44,4 +47,8 @@ def user_filesystem(tmp_path):
4447
f.write("good_data.xy \n")
4548
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
4649

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

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from diffpy.labpdfproc.labpdfprocapp import get_args
88
from diffpy.labpdfproc.tools import (
99
known_sources,
10+
load_user_info,
1011
load_user_metadata,
1112
set_input_lists,
1213
set_output_directory,
@@ -241,3 +242,26 @@ def test_load_user_metadata_bad(inputs, msg):
241242
actual_args = get_args(cli_inputs)
242243
with pytest.raises(ValueError, match=msg[0]):
243244
actual_args = load_user_metadata(actual_args)
245+
246+
247+
params_user_info = [
248+
([None, None], ["home_username", "[email protected]"]),
249+
(["cli_username", None], ["cli_username", "[email protected]"]),
250+
([None, "[email protected]"], ["home_username", "[email protected]"]),
251+
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
252+
]
253+
254+
255+
@pytest.mark.parametrize("inputs, expected", params_user_info)
256+
def test_load_user_info(monkeypatch, inputs, expected, user_filesystem):
257+
cwd = Path(user_filesystem)
258+
home_dir = cwd / "home_dir"
259+
monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir)
260+
os.chdir(cwd)
261+
262+
expected_username, expected_email = expected
263+
cli_inputs = ["2.5", "data.xy", "--username", inputs[0], "--email", inputs[1]]
264+
actual_args = get_args(cli_inputs)
265+
actual_args = load_user_info(actual_args)
266+
assert actual_args.username == expected_username
267+
assert actual_args.email == expected_email

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)