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
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]
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: 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_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.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autoconfiguration-2.3.1.tar.gz.
File metadata
- Download URL: autoconfiguration-2.3.1.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b811c2962097aa72a9802383dd20a0ac3ed4cdff12534c0b4a3aadcd2deafdd
|
|
| MD5 |
c025b7fec1600b788e959e55bfc3ecb6
|
|
| BLAKE2b-256 |
c578b08ccba66cbdad7a3e3e06b78b171b027fec04ae0c9e6c0632deff75f3ec
|
File details
Details for the file autoconfiguration-2.3.1-py3-none-any.whl.
File metadata
- Download URL: autoconfiguration-2.3.1-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
216505446b4b56857b9e81e3dc78389184d1a0d10acb0cd90d547520b34c407b
|
|
| MD5 |
20fbb3a253fa2a48d00d9e41468c5ecd
|
|
| BLAKE2b-256 |
e54a1b6d0eb2032d2af51dda3eac00c389491a248cd4f5eb777dafa8ea653885
|