Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Python library for loading app configurations from files and/or namespaced environment variables

Project description

Build Status Coverage Status PyPi Status

CLIMATECONTROL

CLIMATECONTROL controls your apps configuration environment. It is a Python library for loading app configurations from files and/or namespaced environment variables.

Install

pip install climatecontrol

Note that versions 0.2.4 and below will probably not work with python 3.5.2 and below. Default 3.5 branch should work fine though.

Usage

Set some environment variables in your shell

export MY_APP_VALUE1=test1
export MY_APP_VALUE2=test2

Then use them in your python modules:

from climatecontrol.settings_parser import Settings
settings_map = Settings(prefix='MY_APP')
print(dict(settings_map))

{
    'value1': 'test1',
    'value2': 'test2'
}

In case you want to update your settings or your environment variables have changed and you want to reload them, the update method will reload your settings:

import os
os.environ['MY_APP_VALUE3'] = 'new_env_data'
settings_map.update()
print(dict(settings_map))

{
    'value1': 'test1',
    'value2': 'test2',
    'value3': 'new_env_data'
}

Now you’ve noticed that you want more complex configurations and need nested settings. For this situation we can delimit sections using a double underscore:

export MY_APP_SECTION1__VALUE1=test1
export MY_APP_SECTION2__VALUE2=test2
export MY_APP_SECTION2__VALUE3=test3
export MY_APP_SECTION2__SUB_SECTION__VALUE4=test4
settings_map = Settings(prefix='MY_APP')
print(dict(settings_map))

{
    'section1': {
        'value1': 'test1'
    },
    'section2': {
        'value2': 'test2',
        'value3': 'test3',
        'sub_section': {
            'value4': 'test4'
        }
    }
}

Finally if you decide that your settings are simpler and you know that your section names do not have underscores, you can use the implicit_depth option, which allows you to add a new section at every single underscore (up to the depth you specify).

export MY_APP_SECTION1_VALUE1=test1
export MY_APP_SECTION2_VALUE2=test2
export MY_APP_SECTION2_VALUE3=test3
export MY_APP_SECTION2_SUBSECTION_VALUE4=test4
settings_map = Settings(prefix='MY_APP', implicit_depth=2)
print(dict(settings_map))

{
    'section1': {
        'value1': 'test1'
    },
    'section2': {
        'value2': 'test2',
        'value3': 'test3',
        'subsection': {
            'value4': 'test4'
        }
    }
}

Settings file support

If you don’t want to use an environment variable for every single setting and want to put your settings in a single file instead you can to this as well. Settings files can be toml files (.toml), yaml files (.yml) or json files (.json).

export MY_APP_SETTINGS_FILE=./my_settings_file.toml

The file could look like this:

[section1]
subsection1 = "test1"

[section2]
subsection2 = "test2"
subsection3 = "test3"

Setting variables whos values are saved in files

Sometimes we don’t want to save values in plain text in environment files or in the settings file itself. Instead we have a file that contains the value of the setting we want. A good example for this behaviour are docker _secrets that store secrets in temporary files.

To read a variable from a file, simply add a “_from_file” to the variable name and give it the path to the file that contains the variable as a value.

Using:

[section1]
subsection1_from_file = /home/myuser/supersecret.txt

or

export MY_APP_SECTION1_SUBSECTION1_FROM_FILE="/home/myuser/supersecret.txt"

will both write the content of the file at “/home/myuser/supersecret.txt” into the variable section1 -> sebsection1.

Command line support using click

The click library is a great tool for creating command line applications. If you don’t want to have to use an environment to set your configuration file. Write your command line application like this:

import click

@click.command()
@settings_map.click_settings_file_option()
def cli():
   print(dict(settings_parser))

save it to a file like “cli.py” and then call it after installing click:

pip install click
python cli.py --settings ./my_settings_file.toml

whithout needing to set any env vars.

Project details


Release history Release notifications

History Node

0.6

This version
History Node

0.5

History Node

0.4.1

History Node

0.4

History Node

0.3.1

History Node

0.3

History Node

0.2.6

History Node

0.2.5

History Node

0.2.4

History Node

0.2.3

History Node

0.2.2

History Node

0.2.1

History Node

0.1.10

History Node

0.1.8

History Node

0.1.7

History Node

0.1.6

History Node

0.1.5

History Node

0.1.4

History Node

0.1.2

History Node

0.1.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
climatecontrol-0.5.tar.gz (21.4 kB) Copy SHA256 hash SHA256 Source None Mar 13, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page