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.0.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.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: autoconfiguration-2.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 2208bfdabbe0068b43b047ba4a7ea66eee2b70582480f4fa391df09895a028ba
MD5 2b52505d31b65f3eda5a20c2744ba4ef
BLAKE2b-256 0918b9febe2be25c12624c18b9801c0c75fde23e9d0f10e362767a5b9eebfd0f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: autoconfiguration-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f882690b8ec415b04e7b97c6bee620edcff257f7f335a3978969b4fca2ff47f
MD5 0d326f146369d16229c42e920abe4161
BLAKE2b-256 b4c4ed5f1da0ccd6622b31d1e5fcea6e7811d643f48663abf4ffa0f15c4e2030

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