Skip to content

Commit 658e1ce

Browse files
reformat and add news
1 parent ca0459b commit 658e1ce

File tree

3 files changed

+79
-56
lines changed

3 files changed

+79
-56
lines changed

news/skip_creation_config.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* functionality to run `get_user_info` without execution interruption
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/tools.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"computer and your name will be automatically associated with subsequent diffpy data by default.\n"
1111
"This is not recommended on a shared or public computer. "
1212
"You will only have to do that once.\n"
13-
"For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html"
13+
"For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html."
1414
)
1515

1616

@@ -36,7 +36,7 @@ def _clean_dict(obj):
3636
return obj
3737

3838

39-
def stringify(obj):
39+
def _stringify(obj):
4040
"""
4141
Convert None to an empty string.
4242
@@ -53,7 +53,7 @@ def stringify(obj):
5353
return obj if obj is not None else ""
5454

5555

56-
def load_config(file_path):
56+
def _load_config(file_path):
5757
"""
5858
Load configuration from a .json file.
5959
@@ -77,7 +77,7 @@ def load_config(file_path):
7777
return None
7878

7979

80-
def _merge_sorted_configs(*dicts):
80+
def _sorted_merge(*dicts):
8181
merged = {}
8282
for d in dicts:
8383
d = _clean_dict(d)
@@ -93,28 +93,29 @@ def _create_global_config(user_info):
9393
email = input(f"Please enter the your email " f"[{user_info.get('email', '')}]: ").strip() or user_info.get(
9494
"email", ""
9595
)
96-
config = {"username": stringify(username), "email": stringify(email)}
96+
config = {"username": _stringify(username), "email": _stringify(email)}
9797
if username and email:
9898
with open(Path().home() / "diffpyconfig.json", "w") as f:
9999
f.write(json.dumps(config))
100-
print(
101-
f"You can manually edit the config file at {Path().home() / 'diffpyconfig.json'} using any text editor.\n"
102-
f"Or you can update the config file by passing new values to get_user_info(), "
103-
f"see examples here: https://www.diffpy.org/diffpy.utils/examples/toolsexample.html"
104-
)
100+
print(
101+
f"You can manually edit the config file at "
102+
f"{Path().home() / 'diffpyconfig.json'} using any text editor.\n"
103+
f"Or you can update the config file by passing new values to get_user_info(), "
104+
f"see examples here: https://www.diffpy.org/diffpy.utils/examples/toolsexample.html."
105+
)
105106
return config
106107

107108

108109
def get_user_info(user_info=None, skip_config_creation=False):
109110
"""
110111
Get username and email configuration.
111112
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.
117-
Removes invalid global config file if creation is needed, replacing it with empty username and email.
113+
Workflow:
114+
1. First attempts to load config file from global and local paths.
115+
2. If config files exist, prioritizes values from user_info, then local config, then global config.
116+
3. Otherwise, if user wants to skip config creation, uses user_info as the final config, even if it's empty.
117+
4. Otherwise, if no config files exist and user does not want to skip config creation, prompts for user inputs.
118+
5. If no config files exist, a global config file will only be created if both username and email are valid.
118119
119120
Parameters
120121
----------
@@ -126,14 +127,14 @@ def get_user_info(user_info=None, skip_config_creation=False):
126127
dict or None:
127128
The dictionary containing username and email with corresponding values.
128129
"""
129-
global_config = load_config(Path().home() / "diffpyconfig.json")
130-
local_config = load_config(Path().cwd() / "diffpyconfig.json")
130+
global_config = _load_config(Path().home() / "diffpyconfig.json")
131+
local_config = _load_config(Path().cwd() / "diffpyconfig.json")
131132
if global_config or local_config:
132-
return _merge_sorted_configs(global_config, local_config, user_info)
133+
return _sorted_merge(global_config, local_config, user_info)
133134
if skip_config_creation:
134135
return {
135-
"username": stringify(user_info.get("username", "")),
136-
"email": stringify(user_info.get("email", "")),
136+
"username": _stringify(user_info.get("username", "")),
137+
"email": _stringify(user_info.get("email", "")),
137138
}
138139
print(user_info_imsg)
139140
return _create_global_config(user_info)

tests/test_tools.py

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,152 +27,151 @@ def _run_tests(cli_inputs, expected):
2727

2828
params_user_info_with_home_conf_file = [
2929
(
30-
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
30+
{"cli_username": None, "cli_email": None, "skip_config_creation": False},
3131
{"expected_username": "home_username", "expected_email": "home@email.com"},
3232
),
3333
(
34-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
34+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": False},
3535
{"expected_username": "cli_username", "expected_email": "home@email.com"},
3636
),
3737
(
38-
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
38+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": False},
3939
{"expected_username": "home_username", "expected_email": "cli@email.com"},
4040
),
4141
(
42-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
42+
{"cli_username": "cli_username", "cli_email": "cli@email.com", "skip_config_creation": False},
4343
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
4444
),
4545
(
46-
{"skip_config_creation": True, "cli_username": None, "cli_email": None},
46+
{"cli_username": None, "cli_email": None, "skip_config_creation": True},
4747
{"expected_username": "home_username", "expected_email": "home@email.com"},
4848
),
4949
(
50-
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": None},
50+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": True},
5151
{"expected_username": "cli_username", "expected_email": "home@email.com"},
5252
),
5353
(
54-
{"skip_config_creation": True, "cli_username": None, "cli_email": "cli@email.com"},
54+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": True},
5555
{"expected_username": "home_username", "expected_email": "cli@email.com"},
5656
),
5757
(
58-
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": "cli@email.com"},
58+
{"cli_username": "cli_username", "cli_email": "cli@email.com", "skip_config_creation": True},
5959
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
6060
),
6161
]
6262
params_user_info_with_local_conf_file = [
6363
(
64-
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
64+
{"cli_username": None, "cli_email": None, "skip_config_creation": False},
6565
{"expected_username": "cwd_username", "expected_email": "cwd@email.com"},
6666
),
6767
(
68-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
68+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": False},
6969
{"expected_username": "cli_username", "expected_email": "cwd@email.com"},
7070
),
7171
(
72-
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
72+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": False},
7373
{"expected_username": "cwd_username", "expected_email": "cli@email.com"},
7474
),
7575
(
76-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
76+
{"cli_username": "cli_username", "cli_email": "cli@email.com", "skip_config_creation": False},
7777
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
7878
),
7979
(
80-
{"skip_config_creation": True, "cli_username": None, "cli_email": None},
80+
{"cli_username": None, "cli_email": None, "skip_config_creation": True},
8181
{"expected_username": "cwd_username", "expected_email": "cwd@email.com"},
8282
),
8383
(
84-
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": None},
84+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": True},
8585
{"expected_username": "cli_username", "expected_email": "cwd@email.com"},
8686
),
8787
(
88-
{"skip_config_creation": True, "cli_username": None, "cli_email": "cli@email.com"},
88+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": True},
8989
{"expected_username": "cwd_username", "expected_email": "cli@email.com"},
9090
),
9191
(
92-
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": "cli@email.com"},
92+
{"cli_username": "cli_username", "cli_email": "cli@email.com", "skip_config_creation": True},
9393
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
9494
),
9595
]
9696
params_user_info_with_no_conf_file = [
97-
# Case 1: no inputs, do not create config files
97+
# Case 1: None or only one of username or email is provided, config file should not be created
9898
(
99-
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
99+
{"cli_username": None, "cli_email": None, "skip_config_creation": False},
100100
{"input_username": "", "input_email": ""},
101101
{"expected_username": "", "expected_email": "", "config_file_exists": False},
102102
),
103-
# Case 2: One input (username / email) and the other is empty, do not create config file
104103
(
105-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
104+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": False},
106105
{"input_username": "", "input_email": ""},
107106
{"expected_username": "cli_username", "expected_email": "", "config_file_exists": False},
108107
),
109108
(
110-
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
109+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": False},
111110
{"input_username": "", "input_email": ""},
112111
{"expected_username": "", "expected_email": "cli@email.com", "config_file_exists": False},
113112
),
114113
(
115-
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
114+
{"cli_username": None, "cli_email": None, "skip_config_creation": False},
116115
{"input_username": "input_username", "input_email": ""},
117116
{"expected_username": "input_username", "expected_email": "", "config_file_exists": False},
118117
),
119118
(
120-
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
119+
{"cli_username": None, "cli_email": None, "skip_config_creation": False},
121120
{"input_username": "", "input_email": "input@email.com"},
122121
{"expected_username": "", "expected_email": "input@email.com", "config_file_exists": False},
123122
),
124123
(
125-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
124+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": False},
126125
{"input_username": "input_username", "input_email": ""},
127126
{"expected_username": "input_username", "expected_email": "", "config_file_exists": False},
128127
),
129128
(
130-
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
129+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": False},
131130
{"input_username": "", "input_email": "input@email.com"},
132131
{"expected_username": "", "expected_email": "input@email.com", "config_file_exists": False},
133132
),
134-
# Case 2: Both inputs, create global config file
133+
# Case 2: Both username and email are provided, config file should be created
135134
(
136-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
135+
{"cli_username": "cli_username", "cli_email": "cli@email.com", "skip_config_creation": False},
137136
{"input_username": "", "input_email": ""},
138137
{"expected_username": "cli_username", "expected_email": "cli@email.com", "config_file_exists": True},
139138
),
140139
(
141-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": None},
140+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": False},
142141
{"input_username": "", "input_email": "input@email.com"},
143142
{"expected_username": "cli_username", "expected_email": "input@email.com", "config_file_exists": True},
144143
),
145144
(
146-
{"skip_config_creation": False, "cli_username": None, "cli_email": "cli@email.com"},
145+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": False},
147146
{"input_username": "input_username", "input_email": ""},
148147
{"expected_username": "input_username", "expected_email": "cli@email.com", "config_file_exists": True},
149148
),
150149
(
151-
{"skip_config_creation": False, "cli_username": None, "cli_email": None},
150+
{"cli_username": None, "cli_email": None, "skip_config_creation": False},
152151
{"input_username": "input_username", "input_email": "input@email.com"},
153152
{"expected_username": "input_username", "expected_email": "input@email.com", "config_file_exists": True},
154153
),
155154
(
156-
{"skip_config_creation": False, "cli_username": "cli_username", "cli_email": "cli@email.com"},
155+
{"cli_username": "cli_username", "cli_email": "cli@email.com", "skip_config_creation": False},
157156
{"input_username": "input_username", "input_email": "input@email.com"},
158157
{"expected_username": "input_username", "expected_email": "input@email.com", "config_file_exists": True},
159158
),
160159
]
161160
params_user_info_no_conf_file_no_inputs = [
162161
(
163-
{"skip_config_creation": True, "cli_username": None, "cli_email": None},
162+
{"cli_username": None, "cli_email": None, "skip_config_creation": True},
164163
{"expected_username": "", "expected_email": ""},
165164
),
166165
(
167-
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": None},
166+
{"cli_username": "cli_username", "cli_email": None, "skip_config_creation": True},
168167
{"expected_username": "cli_username", "expected_email": ""},
169168
),
170169
(
171-
{"skip_config_creation": True, "cli_username": None, "cli_email": "cli@email.com"},
170+
{"cli_username": None, "cli_email": "cli@email.com", "skip_config_creation": True},
172171
{"expected_username": "", "expected_email": "cli@email.com"},
173172
),
174173
(
175-
{"skip_config_creation": True, "cli_username": "cli_username", "cli_email": "cli@email.com"},
174+
{"cli_username": "cli_username", "cli_email": "cli@email.com", "skip_config_creation": True},
176175
{"expected_username": "cli_username", "expected_email": "cli@email.com"},
177176
),
178177
]

0 commit comments

Comments
 (0)