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 within a config module in our source (src) module. If our entrypoint for our application is, we will be able to import the module and it's content like so from src.config.configuration import * anywhere in the application.

        # file with production variables
        # file with development variables 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


    # 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" 
            # error will be thrown if a value cannot be obtained 
            "required": True,
                # 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

            "required": True,

                "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

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

        "DATABASE_URI": {
            # downstream configuration variable built on defined variables 🔥
            "dependsOn": [

            # build variable based upon depended on variables.

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

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,

    # 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
        "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 src/
$ moduconfig -c src/config/ -o

we specify our configuration file using the -c argument. If it is a python module this will be the file path ( src/config/ 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 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 hashes)

Uploaded source

Built Distribution

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

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page