Skip to content

Commit ad05e37

Browse files
committed
check_and_build_global_config added wih tests
1 parent 6a52f33 commit ad05e37

File tree

2 files changed

+74
-62
lines changed

2 files changed

+74
-62
lines changed

src/diffpy/utils/tools.py

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,6 @@ def _sorted_merge(*dicts):
7474
return merged
7575

7676

77-
def _create_global_config(args):
78-
username = input(
79-
f"Please enter the name you would want future work to be credited to " f"[{args.get('username', '')}]: "
80-
).strip() or args.get("username", "")
81-
email = input(f"Please enter the your email " f"[{args.get('email', '')}]: ").strip() or args.get("email", "")
82-
return_bool = False if username is None or email is None else True
83-
with open(Path().home() / "diffpyconfig.json", "w") as f:
84-
f.write(json.dumps({"username": stringify(username), "email": stringify(email)}))
85-
print(
86-
f"You can manually edit the config file at {Path().home() / 'diffpyconfig.json'} using any text editor.\n"
87-
f"Or you can update the config file by passing new values to get_user_info(), "
88-
f"see examples here: https://www.diffpy.org/diffpy.utils/examples/toolsexample.html"
89-
)
90-
return return_bool
91-
92-
9377
def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
9478
"""
9579
Get name, email and orcid of the owner/user from various sources and return it as a metadata dictionary
@@ -107,7 +91,7 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
10791
"owner_email": "<your_associated_email>>@email.com",
10892
"owner_orcid": "<your_associated_orcid if you would like this stored with your data>>"
10993
}
110-
You may also store any other gloabl-level information that you would like associated with your
94+
You may also store any other global-level information that you would like associated with your
11195
diffraction data in this file
11296
11397
Parameters
@@ -132,24 +116,52 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
132116
del runtime_info[key]
133117
global_config = load_config(Path().home() / "diffpyconfig.json")
134118
local_config = load_config(Path().cwd() / "diffpyconfig.json")
135-
# if global_config is None and local_config is None:
136-
# print(
137-
# "No global configuration file was found containing "
138-
# "information about the user to associate with the data.\n"
139-
# "By following the prompts below you can add your name and email to this file on the current "
140-
# "computer and your name will be automatically associated with subsequent diffpy data by default.\n"
141-
# "This is not recommended on a shared or public computer. "
142-
# "You will only have to do that once.\n"
143-
# "For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html"
144-
# )
145119
user_info = global_config
146120
user_info.update(local_config)
147121
user_info.update(runtime_info)
148122
return user_info
149123

150124

151-
def check_and_build_global_config():
152-
return
125+
def _get_value(mystring):
126+
return mystring.strip()
127+
128+
129+
def check_and_build_global_config(skip_config_creation=False):
130+
config_path = Path().home() / "diffpyconfig.json"
131+
if skip_config_creation:
132+
return
133+
if config_path.is_file():
134+
return
135+
intro_text = (
136+
"No global configuration file was found containing information about the user to "
137+
"associate with the data.\n By following the prompts below you can add your name "
138+
"and email to this file on the current "
139+
"computer and your name will be automatically associated with subsequent diffpy data by default.\n"
140+
"This is not recommended on a shared or public computer. "
141+
"You will only have to do that once.\n"
142+
"For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html"
143+
)
144+
print(intro_text)
145+
username = input("Please enter the name you would want future work to be credited to: ").strip()
146+
email = input("Please enter your email: ").strip()
147+
orcid = input("Please enter your orcid ID if you know it: ").strip()
148+
config = {"owner_name": stringify(username), "owner_email": stringify(email), "owner_orcid": stringify(orcid)}
149+
if email != "" or orcid != "" or username != "":
150+
config["owner_orcid"] = stringify(orcid)
151+
with open(config_path, "w") as f:
152+
f.write(json.dumps(config))
153+
outro_text = (
154+
f"The config file at {Path().home() / 'diffpyconfig.json'} has been created. "
155+
f"The values {config} were entered.\n"
156+
f"These values will be inserted as metadata with your data in apps that use "
157+
f"diffpy.get_user_info(). If you would like to update these values, either "
158+
f"delete the config file and this workflow will rerun next time you run this "
159+
f"program. Or you may open the config file in a text editor and manually edit the"
160+
f"entries. For more information, see: "
161+
f"https://diffpy.githu.io/diffpy.utils/examples/tools_example.html"
162+
)
163+
print(outro_text)
164+
return
153165

154166

155167
def get_package_info(package_names, metadata=None):

tests/test_tools.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -149,59 +149,59 @@ def test_get_user_info_with_local_conf_file(runtime_inputs, expected, user_files
149149
assert actual == expected
150150

151151

152-
# @pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file)
153-
# def test_get_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem):
154-
# _setup_dirs(monkeypatch, user_filesystem)
155-
# os.remove(Path().home() / "diffpyconfig.json")
156-
# inp_iter = iter(inputsb)
157-
# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
158-
# _run_tests(inputsa, expected)
159-
# confile = Path().home() / "diffpyconfig.json"
160-
# assert confile.is_file()
161-
#
162-
#
163-
# @pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs)
164-
# def test_get_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem):
165-
# _setup_dirs(monkeypatch, user_filesystem)
166-
# os.remove(Path().home() / "diffpyconfig.json")
167-
# inp_iter = iter(inputsb)
168-
# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
169-
# _run_tests(inputsa, expected)
170-
# confile = Path().home() / "diffpyconfig.json"
171-
# assert confile.exists() is False
172-
#
173-
174-
175152
@pytest.mark.parametrize(
176153
"test_inputs,expected",
177154
[ # Check check_and_build_global_config() builds correct config when config is found missing
178155
( # C1: user inputs valid name, email and orcid
179156
{"user_inputs": ["input_name", "input@email.com", "input_orcid"]},
180157
{"owner_email": "input@email.com", "owner_orcid": "input_orcid", "owner_name": "input_name"},
181158
),
182-
# ( # C2: empty strings passed in, expect uname, email, orcid from home_config
183-
# {"owner_name": "", "owner_email": "", "owner_orcid": ""},
184-
# {"owner_name": "home_ownername", "owner_email": "home@email.com", "owner_orcid": "home_orcid"},
185-
# ),
159+
({"user_inputs": ["", "", ""]}, None), # C2: empty strings passed in, expect no config file created
160+
( # C3: just username input, expect config file but with some empty values
161+
{"user_inputs": ["input_name", "", ""]},
162+
{"owner_email": "", "owner_orcid": "", "owner_name": "input_name"},
163+
),
186164
],
187165
)
188166
def test_check_and_build_global_config(test_inputs, expected, user_filesystem, mocker):
189167
# user_filesystem[0] is tmp_dir/home_dir with the global config file in it, user_filesystem[1]
190168
# is tmp_dir/cwd_dir
191169
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
192170
os.chdir(user_filesystem[1])
171+
confile = user_filesystem[0] / "diffpyconfig.json"
193172
# remove the config file from home that came with user_filesystem
194-
old_confile = user_filesystem[0] / "diffpyconfig.json"
195-
os.remove(old_confile)
173+
os.remove(confile)
174+
mocker.patch("builtins.input", side_effect=test_inputs["user_inputs"])
196175
check_and_build_global_config()
197-
inp_iter = iter(test_inputs["user_inputs"])
198-
mocker.patch("builtins.input", lambda _: next(inp_iter))
199-
with open(old_confile, "r") as f:
176+
try:
177+
with open(confile, "r") as f:
178+
actual = json.load(f)
179+
except FileNotFoundError:
180+
actual = None
181+
assert actual == expected
182+
183+
184+
def test_check_and_build_global_config_file_exists(user_filesystem, mocker):
185+
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
186+
os.chdir(user_filesystem[1])
187+
confile = user_filesystem[0] / "diffpyconfig.json"
188+
expected = {"owner_name": "home_ownername", "owner_email": "home@email.com", "owner_orcid": "home_orcid"}
189+
check_and_build_global_config()
190+
with open(confile, "r") as f:
200191
actual = json.load(f)
201-
print(actual)
202192
assert actual == expected
203193

204194

195+
def test_check_and_build_global_config_skipped(user_filesystem, mocker):
196+
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
197+
os.chdir(user_filesystem[1])
198+
confile = user_filesystem[0] / "diffpyconfig.json"
199+
# remove the config file from home that came with user_filesystem
200+
os.remove(confile)
201+
check_and_build_global_config(skip_config_creation=True)
202+
assert not confile.exists()
203+
204+
205205
params_package_info = [
206206
(["diffpy.utils", None], {"package_info": {"diffpy.utils": "3.3.0"}}),
207207
(["package1", None], {"package_info": {"package1": "1.2.3", "diffpy.utils": "3.3.0"}}),

0 commit comments

Comments
 (0)