python-autoconfiguration
Load configuration files (.ini) automatically.
Usage
The init function of the autoconfiguration package has to be called first to initialize the configuration. Pass an arbitrary amount of configuration files to this function. All passed files will be loaded. Additionally the global configuration file (config.ini) will always be loaded by default. The name of the global configuration file has to be config.ini. All other files must start with config- and end with .ini. You don't have to use the full file names for the init function. You can just use the name between config- and .ini.
The init function expects a second parameter config_class. This should be a dataclass containing all sections of the configuration files. The types of the fields should be dataclasses too. These dataclasses should contain the keys of the respective sections.
Supported data types for keys in the dataclasses:
- str
- int
- float
- complex
- bool
- List
- Tuple
- Dict
- Optional (value will be set to
Noneif the key could not be found in the configuration)
Default values are supported too which will be set if the respective key could not be found in the configuration.
The third parameter of the init function is the optional config_dir parameter. This should be a path to the directory containing the configuration files. Absolute paths are supported. The default is config. This works if the name of the directory is config and it exists in the directory where the application was executed from.
Example
config files:
config.ini:
[section]
key=test
config-dev.ini:
[test]
# % needs to be escaped with another %, so a str containing %% will contain only %
test-complex-str=test %%d 1
# Lines after the first line have to be indented deeper than the first line
2 \n\ta
test-int=123
test-bool=False
test-float=0.987
test-complex=1j
test-list=["abc", 123]
test-tuple=(123, "abc")
test-dict={"test": 123, 2: "abc"}
dataclasses:
from dataclasses import dataclass, field
from typing import List, Tuple, Dict, Any, Optional
@dataclass
class Section:
key: str
@dataclass
class Test:
test_complex_str: str
test_int: int
test_bool: bool
test_float: float
test_complex: complex
test_list: List[Any]
test_tuple: Tuple[int, str]
test_dict: Dict
optional: Optional[str]
default_int: int = 987
default_list: List[str] = field(default_factory=lambda: [1, 2, 3])
@dataclass
class Config:
section: Section
test: Test
Initialize autoconfiguration:
from autoconfiguration import autoconfiguration
config: Config = autoconfiguration.init("dev", config_class=Config)
You can enable auto completion in IDEs by specifying the type of the variable (config: Config).
After the autoconfiguration was initialized you can get the configuration from anywhere in your code by calling the get function:
from autoconfiguration import autoconfiguration
config: Config = autoconfiguration.get()
The instance created by init is cached. That means that both the init and the get function always returns the same instance for a specific config class.
The instance of the config class above looks like:
Config(
section=Section(key="test"),
types=Types(
test_complex_str="test %d 1\n2 \\n\\ta",
test_int=123,
test_bool=False,
test_float=0.987,
test_complex=1j,
test_list=["abc", 123],
test_tuple=(123, "abc"),
test_dict={"test": 123, 2: "abc"},
optional=None,
default_int=987,
default_list=[1, 2, 3],
),
)
Multiple instances
If init is called with another config class, autoconfiguration creates and returns a new instance and caches this instance too.
If get is called without the config class parameter, it always returns the first cached instance. Pass the config class to get to get another instance:
from autoconfiguration import autoconfiguration
config: SecondConfig = autoconfiguration.get(SecondConfig)
See example/main.py for a complete example.
Metadata
Release files for autoconfiguration 2.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| autoconfiguration-2.3.3.tar.gz | 13.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| autoconfiguration-2.3.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.8 kB
Release files / autoconfiguration-2.3.3.tar.gz
| Download URL | autoconfiguration-2.3.3.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2025e70198492a706ad43d3adf17e747eb5e550eb12a4dc6ad87c2f188086b80
|
|
BLAKE2b-256 checksum How to use checksums |
9882434eac6eef4b7f1adaee01b98f5440c2ebc2a56de57b87ab711682db16f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
|
Release files / autoconfiguration-2.3.3-py3-none-any.whl
| Download URL | autoconfiguration-2.3.3-py3-none-any.whl |
|---|---|
| Size | 16.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f640bbeaa768a24c1df0c3ffb064885b2747ad623ad3017c2dd4fbc70e565573
|
|
BLAKE2b-256 checksum How to use checksums |
bd7c1ae5af4cb1acfca86853a15862327b4da6bb2f1d89509e11c4e248c93dd6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
|