Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autoconfiguration-2.1.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

autoconfiguration-2.1.2-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file autoconfiguration-2.1.2.tar.gz.

File metadata

  • Download URL: autoconfiguration-2.1.2.tar.gz
  • Upload date:
  • Size: 8.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.2

File hashes

Hashes for autoconfiguration-2.1.2.tar.gz
Algorithm Hash digest
SHA256 01557e6ffe91d46c3a905bec6eb0c9ee2e1071b66f61d03d1b7660e96a25f619
MD5 8afa33634bd05a60624bcea075ed8d5c
BLAKE2b-256 15eaa5aeaff0bab17a223b54a3d53663fd7aaa184356f01a9e5bab90c8287394

See more details on using hashes here.

File details

Details for the file autoconfiguration-2.1.2-py3-none-any.whl.

File metadata

  • Download URL: autoconfiguration-2.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.5 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.2

File hashes

Hashes for autoconfiguration-2.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c61265fd520e5f6690f940da213b8716c2cae20a085bd69573395fdd2c21b1c7
MD5 5c1877c666558bb5e073449ca6d928ca
BLAKE2b-256 63034904c14a7596b083a0a41d8a4221d0d9b57d464f1691df425bdf90fed81e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page