updating config library.py
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
# Change MYMODULE
|
# Change MYMODULE
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import sys
|
import sys
|
||||||
from typing import Any, TypeVar, Type, TYPE_CHECKING
|
from typing import Any, TypeVar, Type, TYPE_CHECKING, Union, List
|
||||||
|
import ast
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
import logging
|
import logging
|
||||||
@@ -24,22 +25,54 @@ def set_config(
|
|||||||
key: str,
|
key: str,
|
||||||
default: str = None,
|
default: str = None,
|
||||||
cast: Any = None,
|
cast: Any = None,
|
||||||
|
check: Union[None, List] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Sets the config variable on the instance of a class.
|
"""Sets the config variable on the instance of a class.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
config_inst : Type[config_inst_t]
|
config_inst : Type[config_inst_t]
|
||||||
Instance of the :class:`~MYMODULE.config.Config` class.
|
Instance of the :class:`~netacea_slackbot.config.Config` class.
|
||||||
key : str
|
key : str
|
||||||
The key referencing the config variable.
|
The key referencing the config variable.
|
||||||
default : str, optional
|
default : str, optional
|
||||||
The default value.
|
The default value.
|
||||||
cast : Any, optional
|
cast : Any, optional
|
||||||
The type of the variable.
|
The type of the variable.
|
||||||
|
check : Union[None, List], optional
|
||||||
|
Type of object to check against. This is useful if you want to use TOML
|
||||||
|
to define a list, but want to make sure that a string representation
|
||||||
|
of a list will be loaded properly if it set as an environment variable.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
`config.toml` has the following attribute set:
|
||||||
|
[package.users]
|
||||||
|
auth = ['user1', 'user2']
|
||||||
|
|
||||||
|
If set as an environment variable you can pass this list as a string
|
||||||
|
and set check=list:
|
||||||
|
|
||||||
|
Environment variable:
|
||||||
|
PACKAGE_USERS_AUTH = "['user1', 'user2']"
|
||||||
|
|
||||||
|
Usage in code:
|
||||||
|
```python
|
||||||
|
set_config(CONFIG, 'users.auth', check=list)
|
||||||
|
```
|
||||||
"""
|
"""
|
||||||
config_var = key.lower().replace('.', '_')
|
config_var = key.lower().replace('.', '_')
|
||||||
|
if check is None:
|
||||||
setattr(config_inst, config_var, config_inst.get(key, default, cast))
|
setattr(config_inst, config_var, config_inst.get(key, default, cast))
|
||||||
|
else:
|
||||||
|
if type(config_inst.get(key, default, cast)) is not check:
|
||||||
|
if check is list:
|
||||||
|
var = ast.literal_eval(config_inst.get(key, default, cast))
|
||||||
|
setattr(config_inst, config_var, var)
|
||||||
|
else:
|
||||||
|
setattr(
|
||||||
|
config_inst, config_var, config_inst.get(key, default, cast)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Create function to print cached logged messages and reset
|
# Create function to print cached logged messages and reset
|
||||||
|
|||||||
Reference in New Issue
Block a user