Skip to main content

A declarative and powerful configuration APIfor application configuration and documentation

Project description

moduconfig ⚙️

A declarative and powerful configuration API for application configuration and documentation.

Basic Usage 🤷‍♂️

Create a file that you can import across your application. This can be a python or JSON file. This example uses a python file called configuration.py within a config module in our source (src) module. If our entrypoint for our application is main.py, we will be able to import the configuration.py module and it's content like so from src.config.configuration import * anywhere in the application.

src/
    config/
        __init__.py
        configuration.py
        # file with production variables
        production.py
        # file with development variables
        development.py
    __init__.py
main.py

configuration.py will have our configuration definition. You can also choose to do this in a JSON file as stated above which moduconfig will load and use.

Here for example we are configuring an application that will connect to a database

# configuration.py

APP_CONFIGS = {
    # the name of your application, used to prefix the names of configuration variables for their environment variables 
    # {applicationName}_{variableName} 
    "applicationName": "MY_AWESOME_APP",

    # we define our modes for our application, usually production and development but can be anything you like
    # the key is the mode name and the value is the description of what the mode does 
    "modes": {
        "testing": "Mode for integration tests to ensure application is functioning correctly",
        "development": "Mode for developing the application, should only be used to develop locally and not to serve clients",
        "production": "Mode for running the application to serve clients" 
    }
    "variables":{
        "DATABASE_HOST":{
            # error will be thrown if a value cannot be obtained 
            "required": True,
            "default":{
                # default only in the selected modes 
                "development": "localhost",
                "testing": "localhost"
            },

            # NOTE: if this is a JSON file, simply wrap the type around qoutations
            "type": str,

            # An optional description to the variable 
            "description": "The host name of the database without the port"
        },
        "DATABASE_PORT": {
            "required": True,
            # default across all modes
            "default": 5432,

            # environment variables are always strings, moduconfig will attempt to cast the value to the specified type if it's not a str
            "type": int
        },

        "DATABASE_USER":{
            "required": True,

            "default":{
                "development": "postgres",
                "testing": "postgres"
            },

            # configuring where to load the variable from 
            "configOpts": {

                # Only allow loading from an environment variable, we might want to do this for security reasons for example 
                "environmentVariable": True
            },

            "type": str
        },

        "DATABASE_NAME":{
            "required": True,
            "default": "myawesomedb",
            "type": str
        },
        "DATABASE_PASSWORD":{
            "required": True,
            "default":{
                "development": "postgres",
                "testing": "postgres"
            },
            "configOpts": {
                "environmentVariable": True
            },
            "type" : str
        },


        "DATABASE_URI": {
            # downstream configuration variable built on defined variables 🔥
            "dependsOn": [
                "DATABASE_HOST",
                "DATABASE_NAME",
                "DATABASE_PORT",
                "DATABASE_PASSWORD",
                "DATABASE_USER"
            ]

            # build variable based upon depended on variables.
            "format":"postgresql+psycopg2://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}"
        }
    }
}

Now we use moduconfig to load variables into a simple python dict based on our specifications.

# main.py
import os
from moduconfig import load_configuration_variables
from src.config.configuration import APP_CONFIGS
from src.config import production, development

app_env = os.environ.get("APP_ENV") or "production"

variables = load_configuration_variables(
    spec = APP_CONFIGS,
    environment=app_env,

    # we have specified our production and development configuration files to be python modules however, this can be a python dictionary, a path to a JSON file, or a path to an .env file
    # if no file is defined for a specific mode then configuration variables will only be loaded from environment variables
    # you can also specify a single file to be used across all modes e.g.
    # configuration_modules=default
    # this can be a python module, python dictionary, path to JSON file or path to .env file
    configuration_modules={
        "development": development,
        "production": production
    }
)

Variables will be loaded into a python dict and returned from load_configuration_variables. Here are some configuration variables returned when our mode is development

{
    "DATABASE_NAME": "mydatabase",
    "DATABASE_HOST": "localhost",
    "DATABASE_PORT": 5432,
    "DATABASE_USER": "postgres",
    "DATABASE_PASSWORD": "postgres"
    "DATABASE_URI": "postgresql+psycopg2://postgres:postgres@localhost:5432/mydatabase"
}

we have specified a value other than the default by specifying an environment variable

$ export MY_AWESOME_APP_DATABASE_NAME="mydatabase"

if you look closely you can see this is a concatination of the applicationName setting and the variable name. This ensures environment variables are unique to this application and we don't load a value from another application with a conflicting environment variable name.

You can now use these values to configure your application.

It doesn't stop there however ! We can generate documentation for our configuration using the moduconfig CLI !

# make sure you activate your virtualenv if you installed moduconfig there 

$ ls
main.py src/
$ moduconfig -c src/config/configuration.py:APP_CONFIGS -o HOW_TO_CONFIGURE.md

we specify our configuration file using the -c argument. If it is a python module this will be the file path ( src/config/configuration.py in our case) followed by the variable to access ( APP_CONFIG ) seperated by a colon. If it is a JSON file this will just be the path to the JSON file

We specify the output by the -o argument. Here we define the filename and path to the markdown file we want to generate. In this case we want to generate a markdown file at the root of our project called HOW_TO_CONFIGURE.

As your application grows it's simple to add configuration variables, simply go back to configuration.py and add the variable definition to you variables dictionary. Then call the moduconfig cli to regenerate your documentation! No figuring out, where you need to modify repeated code, add more boilerplate or add to the documentation.

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

moduconfig-0.0.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

moduconfig-0.0.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file moduconfig-0.0.1.tar.gz.

File metadata

  • Download URL: moduconfig-0.0.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for moduconfig-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3f56e8d746216e17ddac4ed3f0abcc8e8428c0771d21e11b43d02ecadeb8fd06
MD5 2c088eaefce438a7671a2f7cac1377b9
BLAKE2b-256 4081f34bbcd8c84e0d65fedad3a297db3364ddf0db38b11c57b172732b20d534

See more details on using hashes here.

File details

Details for the file moduconfig-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: moduconfig-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for moduconfig-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e3a236c77acbd6a4f3722a6c66624411e3ffb7a441857655be64d8bd8e77f0e3
MD5 0caa7e9eaff0e7c9bd350f9748dc8e48
BLAKE2b-256 e2e58da81804cd945edcb99519314bc428a0e9fcc14b380935f3ec4a83a8d2dd

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