Config¶
Use libbot.utils.config to import the below functions.
Note
All functions are available in sync and async variants under the same name.
Use await function_name() for async and function_name() for sync invocation.
config_delete(key, *path, missing_ok=False, config_file=DEFAULT_CONFIG_LOCATION)
async
¶
Delete config's key by its path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key to delete. |
required |
*path
|
str
|
Path to the key of the target (pass *[] or don't pass anything at all to delete on the top/root level) |
()
|
missing_ok
|
bool
|
Do not raise an exception if the key is missing. Defaults to False. |
False
|
config_file
|
str | Path
|
Path-like object or path as a string of a location of the config file. Defaults to "config.json". |
DEFAULT_CONFIG_LOCATION
|
Raises:
| Type | Description |
|---|---|
KeyError
|
Key is not found under path provided and |
config_get(key, *path, config_file=DEFAULT_CONFIG_LOCATION)
async
¶
Get a value of the config key by its path provided.
For example, foo.bar.key has a path of "foo", "bar" and the key "key".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key that contains the value |
required |
*path
|
str
|
Path to the key that contains the value (pass *[] or don't pass anything at all to get on the top/root level) |
()
|
config_file
|
str | Path
|
Path-like object or path as a string of a location of the config file. Defaults to |
DEFAULT_CONFIG_LOCATION
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Key's value |
Example
Get the "salary" of "Pete" from this JSON structure: {"users": {"Pete": {"salary": 10.0}}}
This can be easily done with the following code:
import libbot salary: float = libbot.sync.config_get("salary", "users", "Pete")
config_set(key, value, *path, config_file=DEFAULT_CONFIG_LOCATION)
async
¶
Set config's key by its path to the value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key that leads to the value. |
required |
value
|
Any
|
Any JSON-serializable data. |
required |
*path
|
str
|
Path to the key of the target (pass *[] or don't pass anything at all to set on the top/root level). |
()
|
config_file
|
str | Path
|
Path-like object or path as a string of a location of the config file. Defaults to "config.json". |
DEFAULT_CONFIG_LOCATION
|
Raises:
| Type | Description |
|---|---|
KeyError
|
Key was not found under the provided path. |