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.

Example

config files: config.ini, config-dev.ini and config-prod.ini (See example/config)

Initialize autoconfiguration:

from autoconfiguration import autoconfiguration

config = autoconfiguration.init("dev")

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()

Auto completion of the Config instance in IDEs


NOTE

This is optional. Use this only if you want auto completion of the sections and keys of your config in IDEs.


If you want auto completion in IDEs, you have to add stubs to your project. First add a package autoconfiguration to your project. Make sure that the file extension of the __init__ file is .pyi (__init__.pyi)! You need to create two files in that package:

File autoconfiguration.pyi:

from autoconfiguration.config import Config

def init(*args: str, config_dir: str = "config") -> Config: ...
def get() -> Config: ...

File config.pyi:

from collections import namedtuple


class Config:
    # TODO: add your sections to this class with a namedtuple annotation containing your keys
    # Example:
    credentials: namedtuple("Credentials", ("username", "password"))

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-1.0.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

autoconfiguration-1.0-py3-none-any.whl (11.4 kB view hashes)

Uploaded Python 3

Supported by

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