Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/configupdate.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* no news added: covered in the news from the get_user_info work

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
66 changes: 39 additions & 27 deletions src/diffpy/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,6 @@ def _sorted_merge(*dicts):
return merged


def _create_global_config(args):
username = input(
f"Please enter the name you would want future work to be credited to " f"[{args.get('username', '')}]: "
).strip() or args.get("username", "")
email = input(f"Please enter the your email " f"[{args.get('email', '')}]: ").strip() or args.get("email", "")
return_bool = False if username is None or email is None else True
with open(Path().home() / "diffpyconfig.json", "w") as f:
f.write(json.dumps({"username": stringify(username), "email": stringify(email)}))
print(
f"You can manually edit the config file at {Path().home() / 'diffpyconfig.json'} using any text editor.\n"
f"Or you can update the config file by passing new values to get_user_info(), "
f"see examples here: https://www.diffpy.org/diffpy.utils/examples/toolsexample.html"
)
return return_bool


def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
"""
Get name, email and orcid of the owner/user from various sources and return it as a metadata dictionary
Expand All @@ -107,7 +91,7 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
"owner_email": "<your_associated_email>>@email.com",
"owner_orcid": "<your_associated_orcid if you would like this stored with your data>>"
}
You may also store any other gloabl-level information that you would like associated with your
You may also store any other global-level information that you would like associated with your
diffraction data in this file

Parameters
Expand All @@ -132,22 +116,50 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
del runtime_info[key]
global_config = load_config(Path().home() / "diffpyconfig.json")
local_config = load_config(Path().cwd() / "diffpyconfig.json")
# if global_config is None and local_config is None:
# print(
# "No global configuration file was found containing "
# "information about the user to associate with the data.\n"
# "By following the prompts below you can add your name and email to this file on the current "
# "computer and your name will be automatically associated with subsequent diffpy data by default.\n"
# "This is not recommended on a shared or public computer. "
# "You will only have to do that once.\n"
# "For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html"
# )
user_info = global_config
user_info.update(local_config)
user_info.update(runtime_info)
return user_info


def check_and_build_global_config(skip_config_creation=False):
config_path = Path().home() / "diffpyconfig.json"
if skip_config_creation:
return
if config_path.is_file():
return
intro_text = (
"No global configuration file was found containing information about the user to "
"associate with the data.\n By following the prompts below you can add your name "
"and email to this file on the current "
"computer and your name will be automatically associated with subsequent diffpy data by default.\n"
"This is not recommended on a shared or public computer. "
"You will only have to do that once.\n"
"For more information, please refer to www.diffpy.org/diffpy.utils/examples/toolsexample.html"
)
print(intro_text)
username = input("Please enter the name you would want future work to be credited to: ").strip()
email = input("Please enter your email: ").strip()
orcid = input("Please enter your orcid ID if you know it: ").strip()
config = {"owner_name": stringify(username), "owner_email": stringify(email), "owner_orcid": stringify(orcid)}
if email != "" or orcid != "" or username != "":
config["owner_orcid"] = stringify(orcid)
with open(config_path, "w") as f:
f.write(json.dumps(config))
outro_text = (
f"The config file at {Path().home() / 'diffpyconfig.json'} has been created. "
f"The values {config} were entered.\n"
f"These values will be inserted as metadata with your data in apps that use "
f"diffpy.get_user_info(). If you would like to update these values, either "
f"delete the config file and this workflow will rerun next time you run this "
f"program. Or you may open the config file in a text editor and manually edit the"
f"entries. For more information, see: "
f"https://diffpy.githu.io/diffpy.utils/examples/tools_example.html"
)
print(outro_text)
return


def get_package_info(package_names, metadata=None):
"""
Fetches package version and updates it into (given) metadata.
Expand Down
140 changes: 53 additions & 87 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,7 @@

import pytest

from diffpy.utils.tools import get_package_info, get_user_info

# def _setup_dirs(monkeypatch, user_filesystem):
# home_dir, cwd_dir = user_filesystem.home_dir, user_filesystem.cwd_dir
# os.chdir(cwd_dir)
# return home_dir
#


def _run_tests(inputs, expected):
args = {"username": inputs[0], "email": inputs[1]}
expected_username, expected_email = expected
config = get_user_info(args)
assert config.get("username") == expected_username
assert config.get("email") == expected_email


params_user_info_with_local_conf_file = [
(["", ""], ["cwd_username", "[email protected]"]),
(["cli_username", ""], ["cli_username", "[email protected]"]),
(["", "[email protected]"], ["cwd_username", "[email protected]"]),
([None, None], ["cwd_username", "[email protected]"]),
(["cli_username", None], ["cli_username", "[email protected]"]),
([None, "[email protected]"], ["cwd_username", "[email protected]"]),
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
]
params_user_info_with_no_home_conf_file = [
(
[None, None],
["input_username", "[email protected]"],
["input_username", "[email protected]"],
),
(
["cli_username", None],
["", "[email protected]"],
["cli_username", "[email protected]"],
),
(
[None, "[email protected]"],
["input_username", ""],
["input_username", "[email protected]"],
),
(
["", ""],
["input_username", "[email protected]"],
["input_username", "[email protected]"],
),
(
["cli_username", ""],
["", "[email protected]"],
["cli_username", "[email protected]"],
),
(
["", "[email protected]"],
["input_username", ""],
["input_username", "[email protected]"],
),
(
["cli_username", "[email protected]"],
["input_username", "[email protected]"],
["cli_username", "[email protected]"],
),
]
params_user_info_no_conf_file_no_inputs = [
([None, None], ["", ""], ["", ""]),
]
from diffpy.utils.tools import check_and_build_global_config, get_package_info, get_user_info


@pytest.mark.parametrize(
Expand Down Expand Up @@ -149,27 +84,58 @@ def test_get_user_info_with_local_conf_file(runtime_inputs, expected, user_files
assert actual == expected


# @pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file)
# def test_get_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem):
# _setup_dirs(monkeypatch, user_filesystem)
# os.remove(Path().home() / "diffpyconfig.json")
# inp_iter = iter(inputsb)
# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
# _run_tests(inputsa, expected)
# confile = Path().home() / "diffpyconfig.json"
# assert confile.is_file()
#
#
# @pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs)
# def test_get_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem):
# _setup_dirs(monkeypatch, user_filesystem)
# os.remove(Path().home() / "diffpyconfig.json")
# inp_iter = iter(inputsb)
# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
# _run_tests(inputsa, expected)
# confile = Path().home() / "diffpyconfig.json"
# assert confile.exists() is False
#
@pytest.mark.parametrize(
"test_inputs,expected",
[ # Check check_and_build_global_config() builds correct config when config is found missing
( # C1: user inputs valid name, email and orcid
{"user_inputs": ["input_name", "[email protected]", "input_orcid"]},
{"owner_email": "[email protected]", "owner_orcid": "input_orcid", "owner_name": "input_name"},
),
({"user_inputs": ["", "", ""]}, None), # C2: empty strings passed in, expect no config file created
( # C3: just username input, expect config file but with some empty values
{"user_inputs": ["input_name", "", ""]},
{"owner_email": "", "owner_orcid": "", "owner_name": "input_name"},
),
],
)
def test_check_and_build_global_config(test_inputs, expected, user_filesystem, mocker):
# user_filesystem[0] is tmp_dir/home_dir with the global config file in it, user_filesystem[1]
# is tmp_dir/cwd_dir
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
confile = user_filesystem[0] / "diffpyconfig.json"
# remove the config file from home that came with user_filesystem
os.remove(confile)
mocker.patch("builtins.input", side_effect=test_inputs["user_inputs"])
check_and_build_global_config()
try:
with open(confile, "r") as f:
actual = json.load(f)
except FileNotFoundError:
actual = None
assert actual == expected


def test_check_and_build_global_config_file_exists(user_filesystem, mocker):
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
confile = user_filesystem[0] / "diffpyconfig.json"
expected = {"owner_name": "home_ownername", "owner_email": "[email protected]", "owner_orcid": "home_orcid"}
check_and_build_global_config()
with open(confile, "r") as f:
actual = json.load(f)
assert actual == expected


def test_check_and_build_global_config_skipped(user_filesystem, mocker):
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
confile = user_filesystem[0] / "diffpyconfig.json"
# remove the config file from home that came with user_filesystem
os.remove(confile)
check_and_build_global_config(skip_config_creation=True)
assert not confile.exists()


params_package_info = [
(["diffpy.utils", None], {"package_info": {"diffpy.utils": "3.3.0"}}),
Expand Down
Loading