diff --git a/doc/source/examples/tools_example.rst b/doc/source/examples/tools_example.rst index 60746e6..268c26d 100644 --- a/doc/source/examples/tools_example.rst +++ b/doc/source/examples/tools_example.rst @@ -41,7 +41,7 @@ The function will return .. code-block:: python - {"owner_email": "janedoe@email.com", "owner_name": "Jane Doe", "owner_orcid": "0000-0000-0000-0000"} + {"owner_name": "Jane Doe", "owner_email": "janedoe@email.com", "owner_orcid": "0000-0000-0000-0000"} Where does ``get_user_info()`` get the user information from? @@ -77,6 +77,7 @@ When building an application where you want to capture data-owner information, w ``check_and_build_global_config()`` first followed by ``get_user_info`` in your app workflow. E.g., .. code-block:: python + from diffpy.utils.tools import check_and_build_global_config, get_user_info from datetime import datetime import json @@ -95,12 +96,30 @@ it will only run once. However, if you want to bypass this behavior, ``check_and_build_global_config()`` takes an optional boolean ``skip_config_creation`` parameter that could be set to ``True`` at runtime to override the config creation. +What happens when you run ``check_and_build_global_config()``? +-------------------------------------------------------------- + +When you set ``skip_config_creation`` to ``False`` and there is no existing global configuration file, +the function will prompt you for inputs (name, email, ORCID). +An example of the prompts you may see is: + +.. code-block:: python + + Please enter the name you would want future work to be credited to: Jane Doe + Please enter your email: janedoe@example.com + Please enter your orcid ID if you know it: 0000-0000-0000-0000 + + +After receiving the inputs, the function will write the information to +the `diffpyconfig.json` file in your home directory. + + I entered the wrong information in my config file so it always loads incorrect information, how do I fix that? -------------------------------------------------------------------------------------------------------------- It is easy to fix this simply by deleting the global and/or local config files, which will allow you to re-enter the information during the ``check_and_build_global_config()`` initialization -workflow. You can also simply editi the ``diffpyconfig.json`` file directly using a text +workflow. You can also simply edit the ``diffpyconfig.json`` file directly using a text editor. Locate the file ``diffpyconfig.json``, in your home directory and open it in an editor :: @@ -111,7 +130,7 @@ Locate the file ``diffpyconfig.json``, in your home directory and open it in an "owner_orcid": "0000-0000-4321-1234" } - Then you can edit the username and email as needed, make sure to save your edits. +Then you can edit the username and email as needed, make sure to save your edits. Automatically Capture Info about a Software Package Being Used ============================================================== diff --git a/doc/source/utilities/tools_utility.rst b/doc/source/utilities/tools_utility.rst index 01685a9..e524a65 100644 --- a/doc/source/utilities/tools_utility.rst +++ b/doc/source/utilities/tools_utility.rst @@ -5,12 +5,17 @@ Tools Utility The ``diffpy.utils.tools`` module provides tool functions for use with diffpy apps. -- ``get_user_info()``: This function is designed for managing and tracking username and email information. - +- ``get_user_info()``: This function is designed for managing and tracking user information (name, email, orcid). Developers can use this function to simplify the process of loading, merging, and saving information consistently and easily. Additionally, it saves the effort of re-entering information, and allows overriding current information by passing parameters. +- ``check_and_build_global_config()``: This function helps create a global configuration file + that can be used by, for example, ``get_user_info()``. + If no existing configuration file is found, this function prompts for information. + The provided inputs are then saved to a global configuration file. + This file can be reused later by ``get_user_info()`` to ensure that the work credits and user information are consistently stored. + - ``get_package_info()``: This function loads package name and version information into a dictionary. It updates the package information under the key "package_info" in the format {"package_name": "version_number"}, resulting in an entry in the passed metadata dictionary that looks like diff --git a/news/user_info_doc.rst b/news/user_info_doc.rst new file mode 100644 index 0000000..43bee6b --- /dev/null +++ b/news/user_info_doc.rst @@ -0,0 +1,23 @@ +**Added:** + +* no news added: simply adding documentation for config update workflow + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/utils/tools.py b/src/diffpy/utils/tools.py index 91505e6..a6c30ed 100644 --- a/src/diffpy/utils/tools.py +++ b/src/diffpy/utils/tools.py @@ -4,29 +4,7 @@ from pathlib import Path -def clean_dict(obj): - """ - Remove keys from the dictionary where the corresponding value is None. - - Parameters - ---------- - obj: dict - The dictionary to clean. If None, initialize as an empty dictionary. - - Returns - ------- - dict: - The cleaned dictionary with keys removed where the value is None. - - """ - obj = obj if obj is not None else {} - for key, value in copy(obj).items(): - if not value: - del obj[key] - return obj - - -def stringify(obj): +def _stringify(obj): """ Convert None to an empty string. @@ -43,7 +21,7 @@ def stringify(obj): return obj if obj is not None else "" -def load_config(file_path): +def _load_config(file_path): """ Load configuration from a .json file. @@ -67,29 +45,6 @@ def load_config(file_path): return {} -def _sorted_merge(*dicts): - merged = {} - for d in dicts: - merged.update(d) - 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 @@ -116,7 +71,7 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None): The name of the user who will show as owner in the metadata that is stored with the data owner_email: string, optional, default is the value stored in the global or local config file. The email of the user/owner - owner_name: string, optional, default is the value stored in the global or local config file. + owner_orcid: string, optional, default is the value stored in the global or local config file. The ORCID id of the user/owner Returns @@ -130,8 +85,8 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None): for key, value in copy(runtime_info).items(): if value is None or value == "": del runtime_info[key] - global_config = load_config(Path().home() / "diffpyconfig.json") - local_config = load_config(Path().cwd() / "diffpyconfig.json") + 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 "