Skip to content

Commit f498a07

Browse files
committed
docstring for create global config and have it return a bool
1 parent 552dd32 commit f498a07

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/diffpy/utils/tools.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,40 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
123123

124124

125125
def check_and_build_global_config(skip_config_creation=False):
126+
"""
127+
Checks for a global diffpu config file in user's home directory and creates one if it is missing
128+
129+
The file it looks for is called diffpyconfig.json. This can contain anything in json format, but
130+
minimally contains information about the computer owner. The information is used
131+
when diffpy objects are created and saved to files or databases to retain ownership information
132+
of datasets. For example, it is used by diffpy.utils.tools.get_user_info().
133+
134+
If the function finds no config file in the user's home directory it interrupts execution
135+
and prompts the user for name, email, and orcid information. It then creates the config file
136+
with this information inside it.
137+
138+
The function returns True if the file exists and False otherwise.
139+
140+
If you would like to check for a file but not run the file creation workflow you can set
141+
the optional argument skip_config_creation to True.
142+
143+
Parameters
144+
----------
145+
skip_config_creation: bool, optional, Default is False
146+
The bool that will override the creation workflow even if no config file exists.
147+
148+
Returns
149+
-------
150+
bool: True if the file exists and False otherwise.
151+
152+
"""
153+
config_exists = False
126154
config_path = Path().home() / "diffpyconfig.json"
127-
if skip_config_creation:
128-
return
129155
if config_path.is_file():
130-
return
156+
config_exists = True
157+
return config_exists
158+
if skip_config_creation:
159+
return config_exists
131160
intro_text = (
132161
"No global configuration file was found containing information about the user to "
133162
"associate with the data.\n By following the prompts below you can add your name "
@@ -154,10 +183,11 @@ def check_and_build_global_config(skip_config_creation=False):
154183
f"delete the config file and this workflow will rerun next time you run this "
155184
f"program. Or you may open the config file in a text editor and manually edit the"
156185
f"entries. For more information, see: "
157-
f"https://diffpy.githu.io/diffpy.utils/examples/tools_example.html"
186+
f"https://diffpy.github.io/diffpy.utils/examples/tools_example.html"
158187
)
159188
print(outro_text)
160-
return
189+
config_exists = True
190+
return config_exists
161191

162192

163193
def get_package_info(package_names, metadata=None):

tests/test_tools.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,25 @@ def test_check_and_build_global_config(test_inputs, expected, user_filesystem, m
107107
# remove the config file from home that came with user_filesystem
108108
os.remove(confile)
109109
mocker.patch("builtins.input", side_effect=test_inputs["user_inputs"])
110-
check_and_build_global_config()
110+
actual_bool = check_and_build_global_config()
111111
try:
112112
with open(confile, "r") as f:
113113
actual = json.load(f)
114+
expected_bool = True
114115
except FileNotFoundError:
116+
expected_bool = False
115117
actual = None
116118
assert actual == expected
119+
assert actual_bool == expected_bool
117120

118121

119122
def test_check_and_build_global_config_file_exists(user_filesystem, mocker):
120123
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
121124
os.chdir(user_filesystem[1])
122125
confile = user_filesystem[0] / "diffpyconfig.json"
123126
expected = {"owner_name": "home_ownername", "owner_email": "home@email.com", "owner_orcid": "home_orcid"}
124-
check_and_build_global_config()
127+
actual_bool = check_and_build_global_config()
128+
assert actual_bool is True
125129
with open(confile, "r") as f:
126130
actual = json.load(f)
127131
assert actual == expected
@@ -133,7 +137,8 @@ def test_check_and_build_global_config_skipped(user_filesystem, mocker):
133137
confile = user_filesystem[0] / "diffpyconfig.json"
134138
# remove the config file from home that came with user_filesystem
135139
os.remove(confile)
136-
check_and_build_global_config(skip_config_creation=True)
140+
actual_bool = check_and_build_global_config(skip_config_creation=True)
141+
assert actual_bool is False
137142
assert not confile.exists()
138143

139144

0 commit comments

Comments
 (0)