Load configuration files (.ini) automatically
Project description
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
None
if 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]
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_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 = autoconfiguration.init("dev", config_class=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 = autoconfiguration.get()
The instance created by init
is cached. That means that both the init
and the get
function always returns the same instance.
The instance of the config class above looks like:
Config(
section=Section(key="test"),
types=Types(
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],
),
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for autoconfiguration-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6611d49bce482e81ad090c5c99aefebff66855773893897e28ad81fcac3e3b91 |
|
MD5 | 388e7e7f8629b8363f90633b7170fe69 |
|
BLAKE2b-256 | 3adaf2092d57bcc09b803a60f741c84a7a7894fcf8d795b3c44e322a6c431e39 |