Appconfig

Python module to simplify the usage of config files.

Download as .zip Download as .tar.gz View on GitHub

appconfig

appconfig is a python module that simplifies the usage of ini based config files. It uses the Python ConfigParser module. The advantage of the appconfig module it that it allows defining default values for all config parameters and to provide a description of each parameter. While writing the config file back it will add comments above all of the config values and since we have default values we never need to check if a value is defined.

Features

The appconfig module offers the following features:

Installation

pip install appconfig

Dependencies

The following packages are required for using the library:

To run the unit test the following packages are required:

Examples

Example Config File

The config files that the user will use for your application will have the ini format. For the application it self it is necessary to create a config file with json content that allows to set the default values and the documentation for all keys.

Example JSON File

{
    "application_name": "appconfig_test",
    "application_author": "python",
    "application_version": "1.0",
    "appconfig": {
        "first": {
            "default": 1,
            "description": "the first parameter",
            "type": "int"
        }
    }
}

This will lead to the following user config file:

Example ini config File

[appconfig]
# the first paramter 
# Type: [int]
# first = 1
fisrt = 10

Example Usage

With the application and the user config from above a simple python application might look like these:

#!/bin/env python

from appconfig import AppConfig


def main():
    """ The main method to be run.
    """
    conf = AppConfig()
    conf.init_default_config('example.json')
    conf.load('example.conf')

    value = conf.get_s('appconfig', 'first')
    print value
    print type(value)


if __name__ == '__main__':
    main()

Application Config Format

The application config format is a simple config file with json syntax it requires at least the application_name to be defined and one json object (config section) with at least one parameter defined.

{
    "application_name": "appconfig_test",
    "application_author": "python",
    "application_version": "1.0",
    "appconfig": {
        "first": {
            "default": 1,
            "description": "the first parameter",
            "type": "int"
        }
    }
}

A section consists out of at least one parameter. Each parameter must be a json object with the following three parameter defined:

Config File Location

The load_default function tries to load config files from different locations. The first location it tries is the global config directory:

Mac OS X

/Library/Application Support/APPNAME/APPNAME.conf

Linux

/etc/xdg/APPNAME/APPNAME.conf

Windows 7

?

The second location is the users home directory where the local config files are store.

Mac OS X

/Users/USERNAME/Library/Application Support/APPNAME/APPNAME.conf

Linux

/home/USERSNAME/.config/APPNAME/APPNAME.conf

Windows

C:\Users\USERNAME\AppData\Local\AUTHOR\APPNAME\APPNAME.conf

If both file exist they will be loaded in this order and the user config file will override values from the global config file.

If the application version is specified then the application version will be added to the path after the application name. This allows a per-version isolation of the config files.

The save saves a config file if no config file is specified it will try to use the user default location or the location configured within the section APPNAME config value config_file.

Suported Types

Currently the following types are supported for a config value:

All the types will be converted with python functions. The only exception is the bool type. The boolean type uses the RawConfigParser.getboolean function. This translates the following string to a true (1, yes, true, on)