Skip to content

Commit ca0459b

Browse files
intermediate commit
1 parent f1350e5 commit ca0459b

File tree

2 files changed

+184
-89
lines changed

2 files changed

+184
-89
lines changed

src/diffpy/utils/tools.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import importlib.metadata
22
import json
3-
import os
43
from copy import copy
54
from pathlib import Path
65

6+
user_info_imsg = (
7+
"No global configuration file was found containing "
8+
"information about the user to associate with the data.\n"
9+
"By following the prompts below you can add your name and email to this file on the current "
10+
"computer and your name will be automatically associated with subsequent diffpy data by default.\n"
11+
"This is not recommended on a shared or public computer. "
12+
"You will only have to do that once.\n"
13+
"For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html"
14+
)
715

8-
def clean_dict(obj):
16+
17+
def _clean_dict(obj):
918
"""
1019
Remove keys from the dictionary where the corresponding value is None.
1120
@@ -68,9 +77,10 @@ def load_config(file_path):
6877
return None
6978

7079

71-
def _sorted_merge(*dicts):
80+
def _merge_sorted_configs(*dicts):
7281
merged = {}
7382
for d in dicts:
83+
d = _clean_dict(d)
7484
merged.update(d)
7585
return merged
7686

@@ -83,24 +93,27 @@ def _create_global_config(user_info):
8393
email = input(f"Please enter the your email " f"[{user_info.get('email', '')}]: ").strip() or user_info.get(
8494
"email", ""
8595
)
86-
return_bool = False if username is None or email is None else True
87-
with open(Path().home() / "diffpyconfig.json", "w") as f:
88-
f.write(json.dumps({"username": stringify(username), "email": stringify(email)}))
96+
config = {"username": stringify(username), "email": stringify(email)}
97+
if username and email:
98+
with open(Path().home() / "diffpyconfig.json", "w") as f:
99+
f.write(json.dumps(config))
89100
print(
90101
f"You can manually edit the config file at {Path().home() / 'diffpyconfig.json'} using any text editor.\n"
91102
f"Or you can update the config file by passing new values to get_user_info(), "
92103
f"see examples here: https://www.diffpy.org/diffpy.utils/examples/toolsexample.html"
93104
)
94-
return return_bool
105+
return config
95106

96107

97-
def get_user_info(user_info=None):
108+
def get_user_info(user_info=None, skip_config_creation=False):
98109
"""
99110
Get username and email configuration.
100111
101-
First attempts to load config file from global and local paths.
102-
If neither exists, creates a global config file.
103-
It prioritizes values from user_info, then local, then global.
112+
The workflow is following:
113+
We first attempt to load config file from global and local paths.
114+
If any exists, it prioritizes values from user_info, then local, then global.
115+
Otherwise, if user wants to skip config creation, it uses user_info as the final info, even if it's empty.
116+
Otherwise, prompt for user inputs, and create a global config file.
104117
Removes invalid global config file if creation is needed, replacing it with empty username and email.
105118
106119
Parameters
@@ -112,29 +125,18 @@ def get_user_info(user_info=None):
112125
-------
113126
dict or None:
114127
The dictionary containing username and email with corresponding values.
115-
116128
"""
117-
config_bool = True
118129
global_config = load_config(Path().home() / "diffpyconfig.json")
119130
local_config = load_config(Path().cwd() / "diffpyconfig.json")
120-
if global_config is None and local_config is None:
121-
print(
122-
"No global configuration file was found containing "
123-
"information about the user to associate with the data.\n"
124-
"By following the prompts below you can add your name and email to this file on the current "
125-
"computer and your name will be automatically associated with subsequent diffpy data by default.\n"
126-
"This is not recommended on a shared or public computer. "
127-
"You will only have to do that once.\n"
128-
"For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html"
129-
)
130-
config_bool = _create_global_config(user_info)
131-
global_config = load_config(Path().home() / "diffpyconfig.json")
132-
config = _sorted_merge(clean_dict(global_config), clean_dict(local_config), clean_dict(user_info))
133-
if config_bool is False:
134-
os.remove(Path().home() / "diffpyconfig.json")
135-
config = {"username": "", "email": ""}
136-
137-
return config
131+
if global_config or local_config:
132+
return _merge_sorted_configs(global_config, local_config, user_info)
133+
if skip_config_creation:
134+
return {
135+
"username": stringify(user_info.get("username", "")),
136+
"email": stringify(user_info.get("email", "")),
137+
}
138+
print(user_info_imsg)
139+
return _create_global_config(user_info)
138140

139141

140142
def get_package_info(package_names, metadata=None):

tests/test_tools.py

Lines changed: 151 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,109 +16,202 @@ def _setup_dirs(monkeypatch, user_filesystem):
1616
return home_dir
1717

1818

19-
def _run_tests(inputs, expected):
20-
args = {"username": inputs[0], "email": inputs[1]}
21-
expected_username, expected_email = expected
22-
config = get_user_info(args)
19+
def _run_tests(cli_inputs, expected):
20+
user_info = {"username": cli_inputs["cli_username"], "email": cli_inputs["cli_email"]}
21+
config = get_user_info(user_info=user_info, skip_config_creation=cli_inputs["skip_config_creation"])
22+
expected_username = expected["expected_username"]
23+
expected_email = expected["expected_email"]
2324
assert config.get("username") == expected_username
2425
assert config.get("email") == expected_email
2526

2627

2728
params_user_info_with_home_conf_file = [
28-
(["", ""], ["home_username", "home@email.com"]),
29-
(["cli_username", ""], ["cli_username", "home@email.com"]),
30-
(["", "cli@email.com"], ["home_username", "cli@email.com"]),
31-
([None, None], ["home_username", "home@email.com"]),
32-
(["cli_username", None], ["cli_username", "home@email.com"]),
33-
([None, "cli@email.com"], ["home_username", "cli@email.com"]),
34-
(["cli_username", "cli@email.com"], ["cli_username", "cli@email.com"]),
29+
(
30+
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
31+
{"expected_username": "home_username", "expected_email": "home@email.com"},
32+
),
33+
(
34+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
35+
{"expected_username": "cli_username", "expected_email": "home@email.com"},
36+
),
37+
(
38+
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
39+
{"expected_username": "home_username", "expected_email": "cli@email.com"},
40+
),
41+
(
42+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
43+
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
44+
),
45+
(
46+
{"skip_config_creation": True, "cli_username": None, "cli_email": None},
47+
{"expected_username": "home_username", "expected_email": "home@email.com"},
48+
),
49+
(
50+
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": None},
51+
{"expected_username": "cli_username", "expected_email": "home@email.com"},
52+
),
53+
(
54+
{"skip_config_creation": True, "cli_username": None, "cli_email": "cli@email.com"},
55+
{"expected_username": "home_username", "expected_email": "cli@email.com"},
56+
),
57+
(
58+
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": "cli@email.com"},
59+
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
60+
),
3561
]
3662
params_user_info_with_local_conf_file = [
37-
(["", ""], ["cwd_username", "cwd@email.com"]),
38-
(["cli_username", ""], ["cli_username", "cwd@email.com"]),
39-
(["", "cli@email.com"], ["cwd_username", "cli@email.com"]),
40-
([None, None], ["cwd_username", "cwd@email.com"]),
41-
(["cli_username", None], ["cli_username", "cwd@email.com"]),
42-
([None, "cli@email.com"], ["cwd_username", "cli@email.com"]),
43-
(["cli_username", "cli@email.com"], ["cli_username", "cli@email.com"]),
63+
(
64+
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
65+
{"expected_username": "cwd_username", "expected_email": "cwd@email.com"},
66+
),
67+
(
68+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
69+
{"expected_username": "cli_username", "expected_email": "cwd@email.com"},
70+
),
71+
(
72+
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
73+
{"expected_username": "cwd_username", "expected_email": "cli@email.com"},
74+
),
75+
(
76+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
77+
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
78+
),
79+
(
80+
{"skip_config_creation": True, "cli_username": None, "cli_email": None},
81+
{"expected_username": "cwd_username", "expected_email": "cwd@email.com"},
82+
),
83+
(
84+
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": None},
85+
{"expected_username": "cli_username", "expected_email": "cwd@email.com"},
86+
),
87+
(
88+
{"skip_config_creation": True, "cli_username": None, "cli_email": "cli@email.com"},
89+
{"expected_username": "cwd_username", "expected_email": "cli@email.com"},
90+
),
91+
(
92+
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": "cli@email.com"},
93+
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
94+
),
4495
]
45-
params_user_info_with_no_home_conf_file = [
96+
params_user_info_with_no_conf_file = [
97+
# Case 1: no inputs, do not create config files
4698
(
47-
[None, None],
48-
["input_username", "input@email.com"],
49-
["input_username", "input@email.com"],
99+
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
100+
{"input_username": "", "input_email": ""},
101+
{"expected_username": "", "expected_email": "", "config_file_exists": False},
50102
),
103+
# Case 2: One input (username / email) and the other is empty, do not create config file
51104
(
52-
["cli_username", None],
53-
["", "input@email.com"],
54-
["cli_username", "input@email.com"],
105+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
106+
{"input_username": "", "input_email": ""},
107+
{"expected_username": "cli_username", "expected_email": "", "config_file_exists": False},
55108
),
56109
(
57-
[None, "cli@email.com"],
58-
["input_username", ""],
59-
["input_username", "cli@email.com"],
110+
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
111+
{"input_username": "", "input_email": ""},
112+
{"expected_username": "", "expected_email": "cli@email.com", "config_file_exists": False},
60113
),
61114
(
62-
["", ""],
63-
["input_username", "input@email.com"],
64-
["input_username", "input@email.com"],
115+
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
116+
{"input_username": "input_username", "input_email": ""},
117+
{"expected_username": "input_username", "expected_email": "", "config_file_exists": False},
65118
),
66119
(
67-
["cli_username", ""],
68-
["", "input@email.com"],
69-
["cli_username", "input@email.com"],
120+
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
121+
{"input_username": "", "input_email": "input@email.com"},
122+
{"expected_username": "", "expected_email": "input@email.com", "config_file_exists": False},
70123
),
71124
(
72-
["", "cli@email.com"],
73-
["input_username", ""],
74-
["input_username", "cli@email.com"],
125+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
126+
{"input_username": "input_username", "input_email": ""},
127+
{"expected_username": "input_username", "expected_email": "", "config_file_exists": False},
75128
),
76129
(
77-
["cli_username", "cli@email.com"],
78-
["input_username", "input@email.com"],
79-
["cli_username", "cli@email.com"],
130+
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
131+
{"input_username": "", "input_email": "input@email.com"},
132+
{"expected_username": "", "expected_email": "input@email.com", "config_file_exists": False},
133+
),
134+
# Case 2: Both inputs, create global config file
135+
(
136+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
137+
{"input_username": "", "input_email": ""},
138+
{"expected_username": "cli_username", "expected_email": "cli@email.com", "config_file_exists": True},
139+
),
140+
(
141+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
142+
{"input_username": "", "input_email": "input@email.com"},
143+
{"expected_username": "cli_username", "expected_email": "input@email.com", "config_file_exists": True},
144+
),
145+
(
146+
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
147+
{"input_username": "input_username", "input_email": ""},
148+
{"expected_username": "input_username", "expected_email": "cli@email.com", "config_file_exists": True},
149+
),
150+
(
151+
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
152+
{"input_username": "input_username", "input_email": "input@email.com"},
153+
{"expected_username": "input_username", "expected_email": "input@email.com", "config_file_exists": True},
154+
),
155+
(
156+
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
157+
{"input_username": "input_username", "input_email": "input@email.com"},
158+
{"expected_username": "input_username", "expected_email": "input@email.com", "config_file_exists": True},
80159
),
81160
]
82161
params_user_info_no_conf_file_no_inputs = [
83-
([None, None], ["", ""], ["", ""]),
162+
(
163+
{"skip_config_creation": True, "cli_username": None, "cli_email": None},
164+
{"expected_username": "", "expected_email": ""},
165+
),
166+
(
167+
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": None},
168+
{"expected_username": "cli_username", "expected_email": ""},
169+
),
170+
(
171+
{"skip_config_creation": True, "cli_username": None, "cli_email": "cli@email.com"},
172+
{"expected_username": "", "expected_email": "cli@email.com"},
173+
),
174+
(
175+
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": "cli@email.com"},
176+
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
177+
),
84178
]
85179

86180

87-
@pytest.mark.parametrize("inputs, expected", params_user_info_with_home_conf_file)
88-
def test_get_user_info_with_home_conf_file(monkeypatch, inputs, expected, user_filesystem):
181+
@pytest.mark.parametrize("cli_inputs, expected", params_user_info_with_home_conf_file)
182+
def test_get_user_info_with_home_conf_file(monkeypatch, cli_inputs, expected, user_filesystem):
89183
_setup_dirs(monkeypatch, user_filesystem)
90-
_run_tests(inputs, expected)
184+
_run_tests(cli_inputs, expected)
91185

92186

93-
@pytest.mark.parametrize("inputs, expected", params_user_info_with_local_conf_file)
94-
def test_get_user_info_with_local_conf_file(monkeypatch, inputs, expected, user_filesystem):
187+
@pytest.mark.parametrize("cli_inputs, expected", params_user_info_with_local_conf_file)
188+
def test_get_user_info_with_local_conf_file(monkeypatch, cli_inputs, expected, user_filesystem):
95189
_setup_dirs(monkeypatch, user_filesystem)
96190
local_config_data = {"username": "cwd_username", "email": "cwd@email.com"}
97191
with open(Path(user_filesystem) / "diffpyconfig.json", "w") as f:
98192
json.dump(local_config_data, f)
99-
_run_tests(inputs, expected)
193+
_run_tests(cli_inputs, expected)
194+
# Run tests again without global config, results should be the same
100195
os.remove(Path().home() / "diffpyconfig.json")
101-
_run_tests(inputs, expected)
196+
_run_tests(cli_inputs, expected)
102197

103198

104-
@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file)
105-
def test_get_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem):
199+
@pytest.mark.parametrize("cli_inputs, inputs, expected", params_user_info_with_no_conf_file)
200+
def test_get_user_info_with_no_conf_file(monkeypatch, cli_inputs, inputs, expected, user_filesystem):
106201
_setup_dirs(monkeypatch, user_filesystem)
107202
os.remove(Path().home() / "diffpyconfig.json")
108-
inp_iter = iter(inputsb)
203+
inp_iter = iter([inputs["input_username"], inputs["input_email"]])
109204
monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
110-
_run_tests(inputsa, expected)
205+
_run_tests(cli_inputs, expected)
111206
confile = Path().home() / "diffpyconfig.json"
112-
assert confile.is_file()
207+
assert confile.is_file() == expected["config_file_exists"]
113208

114209

115-
@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs)
116-
def test_get_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem):
210+
@pytest.mark.parametrize("cli_inputs, expected", params_user_info_no_conf_file_no_inputs)
211+
def test_get_user_info_no_conf_file_no_inputs(monkeypatch, cli_inputs, expected, user_filesystem):
117212
_setup_dirs(monkeypatch, user_filesystem)
118213
os.remove(Path().home() / "diffpyconfig.json")
119-
inp_iter = iter(inputsb)
120-
monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
121-
_run_tests(inputsa, expected)
214+
_run_tests(cli_inputs, expected)
122215
confile = Path().home() / "diffpyconfig.json"
123216
assert confile.exists() is False
124217

0 commit comments

Comments
 (0)