Skip to content

Commit 2f3b5f0

Browse files
authored
Merge pull request diffpy#83 from yucongalicechen/pkg_docs
added package docs and corrected import in tools
2 parents 4efb6c2 + 10f110b commit 2f3b5f0

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

doc/manual/source/examples/toolsexample.rst

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Tools Example
66
#############
77

8-
This example will demonstrate how diffpy.utils allows us to load and manage username and email information.
8+
This example will demonstrate how diffpy.utils allows us to conveniently load and manage user and package information.
99
Using the tools module, we can efficiently get them in terms of a dictionary.
1010

1111
1) We have the function ``get_user_info`` that neatly returns a dictionary containing the username and email.
@@ -14,23 +14,22 @@ Using the tools module, we can efficiently get them in terms of a dictionary.
1414
from diffpy.utils.tools import get_user_info
1515
user_info = get_user_info()
1616

17-
This function will first attempt to load configuration files
18-
from both the current working directory and the home directory.
19-
If no configuration files exist, it prompts for user input and creates a configuration file in the home directory.
20-
It prioritizes prompted user inputs, then current working directory, and finally home directory.
21-
If no configuration files or inputs are found, this function creates a configuration in the home directory
22-
with empty values for username and email stored as a dictionary.
17+
This function will first attempt to load the information from configuration files looking first in
18+
the current working directory and then in the user's home directory.
19+
If no configuration files exist, it prompts for user input and creates a configuration file in the home directory
20+
so that the next time the program is run it will no longer have to prompt the user.
21+
It can be passed user information which overrides looking in config files, and so could be passed
22+
information that is entered through a gui or command line interface to override default information at runtime.
23+
It prioritizes prompted user inputs, then current working directory config file, and finally home directory config file.
2324

24-
2) You can also override existing values by passing a dictionary to the function. ::
25+
The function returns a dictionary containing the username and email information.
26+
27+
2) You can also override existing values by passing a dictionary to the function with the keys `"username"` and `"email"` ::
2528

2629
new_args = {"username": "new_username", "email": "[email protected]"}
2730
new_user_info = get_user_info(new_args)
2831

29-
Here, the function returns a dictionary containing the new arguments.
30-
If no configuration files exist, it prompts for inputs again. The arguments passed here also override input values.
31-
The updated arguments will not be saved in files.
32-
33-
You can update only the username or email individually, for example ::
32+
3) You can update only the username or email individually, for example ::
3433

3534
new_username = {"username": new_username}
3635
new_user_info = get_user_info(new_username)
@@ -43,5 +42,18 @@ Using the tools module, we can efficiently get them in terms of a dictionary.
4342

4443
This updates the email to "[email protected]" while fetching the username from inputs or the configuration files.
4544

45+
3) We also have the function ``get_package_info``, which inserts or updates package names and versions
46+
in the given metadata dictionary under the key "package_info".
47+
It stores the package information as {"package_info": {"package_name": "version_number"}}.
48+
This function can be used as follows. ::
49+
50+
from diffpy.utils.tools import get_user_info
51+
package_metadata = get_package_info("my_package")
52+
53+
You can also specify an existing dictionary to be updated with the information. ::
54+
55+
existing_dict = {"key": "value"}
56+
updated_dict = get_package_info("my_package", metadata=existing_dict))
4657

47-
By using this function, we ensure that user information is correctly loaded, merged, and saved.
58+
note that `"diffpy.utils"` is automatically included in the package info since the `get_user_info` function is
59+
part of diffpy.utils.

doc/manual/source/utilities/toolsutility.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
Tools Utility
66
#############
77

8-
The ``diffpy.utils.tools`` module contains tool functions for use with diffpy apps.
9-
One of these functions, ``get_user_info``, is designed for managing and tracking username and email information.
10-
This function helps storing this data in a ``diffpyconfig.json`` file.
8+
The ``diffpy.utils.tools`` module provides tool functions for use with diffpy apps.
119

12-
Users can use the module to simplify the process of loading, merging, and saving information consistently and easily.
10+
1) ``get_uder_info``: This function is designed for managing and tracking username and email information.
11+
12+
Developers can use this function to simplify the process of loading, merging, and saving information consistently and easily.
1313
Additionally, it saves the effort of re-entering information, and allows overriding current information by
1414
passing parameters.
15+
16+
2) ``get_package_info``: This function loads package name and version information into a dictionary.
17+
It updates the package information under the key "package_info" in the format {"package_name": "version_number"},
18+
resulting in an entry in the passed metadata dictionary that looks like
19+
`{"package_info": {"package1": "version_number1", "package2": "version_number2"} if the function is called more than
20+
once.
21+
22+
Users can use these functions to track and manage versions of packages that can later be stored, for example, in an output
23+
file header.

src/diffpy/utils/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import importlib
1+
import importlib.metadata
22
import json
33
import os
44
from copy import copy

0 commit comments

Comments
 (0)