Skip to main content

Abstract base class with hierarchical loading

Project description

Easy-to-use abstract base class with hierarchical loading

How to use:

from abconfig import ABConfig


class YourConfig(ABConfig):
    """ My config."""

    # Define sections and default values
    postgres = dict(
        host='127.0.0.1',
        port='5432',
        user='your_user',
        password='your_pass'
    )

    redis = dict(
        host='127.0.0.1',
        port='6379',
        db='0'
    )


config = YourConfig()

# Get section like a dict
dbconfig = config['postgres'])

# Get all:
print(config.get())

File loader

Values from this loader will be overwritten with values from Environment loader

The default is to search for a file called config/config.json/config.yaml in the directory from which the application is started, you can force a file path by adding an attribute filepath to the class

class MainConf(ABConfig):
    filepath = '/etc/main.conf'
    ...

or by define a variable CONFIG_PATH, but the attribute has the highest priority and is preferred.

Environment loader

This loader has the highest priority

The variable name make up of the section name + '_' character and the parameter name (is key in the default values dictionary) in upper case, for example, if exist class:

class LogConfig(ABConfig):
    logging = dict(
        file='/var/log/my.log'
    )
    ...

environment variable will be: LOGGING_FILE.

Disable loaders

To disable loader, add an attribute disabled_loaders to the class with the value of the list containing the loader name as a string.

class DisabledFileLoaderConfig(ABConfig):
    disabled_loaders = ['File']
    ...

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

abconfig-0.1.4.tar.gz (3.9 kB view hashes)

Uploaded Source

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