Skip to main content

getconf, a versatile configuration lib for Python projects

Project description

The getconf project provides simple configuration helpers for Python programs.

It provides a simple API to read from various configuration files and environment variables:

import getconf
config = getconf.ConfigGetter('myproj', ['/etc/myproj.conf'])
db_host = config.get('db.host', 'localhost')
db_port = config.getint('db.port', 5432)

Beyond this API, getconf aims at unifying configuration setup across development and production systems, respecting the standard procedures in each system:

  • Allow userspace configuration on development systems

  • Allow multiple different configurations for continuous integration systems

  • Use standard configuration space in /etc on traditional production servers

  • Handle environment-based configuration for cloud-based platforms

getconf supports Python 2.6, 2.7, 3.3, 3.4 and is distributed under the two-clause BSD license.

Installation

Intall the package from PyPI, using pip:

pip install getconf

Or from GitHub:

git clone git://github.com/Polyconseil/getconf

getconf has no external dependancy beyond Python.

Introduction

All configuration values are accessed through the getconf.ConfigGetter object:

import getconf
config = getconf.ConfigGetter('myproj', ['/etc/myproj/settings.ini', './local_settings.ini'])

The above line declares:

  • Use the myproj namespace (explained later; this is mostly used for environment-based configuration, as a prefix for environment variables)

  • Look, in turn, at /etc/myproj/settings.ini (for production) and ./local_settings.ini (for development); the latter overriding the former.

Once the getconf.ConfigGetter has been configured, it can be used to retrieve settings:

debug = config.getbool('debug', False)
db_host = config.get('db.host', 'localhost')
db_port = config.getint('db.port', 5432)
allowed_hosts = config.getlist('django.allowed_hosts', ['*'])

All settings have a type (default is text), and accept a default value. They use namespaces (think ‘sections’) for easier reading.

With the above setup, getconf will try to provide db.host by inspecting the following options in order (it stops at the first defined value):

  • From the environment variable MYPROJ_DB_HOST, if defined

  • From the host key in the [db] section of ./local_settings.ini

  • From the host key in the [db] section of /etc/myproj/settings.ini

  • From the default provided value, 'localhost'

Features

Env-based configuration files

An extra configuration file/directory/glob can be provided through MYPROJ_CONFIG; it takes precedence over other files

Default options

An extra dictionary can be provided as ConfigGetter(defaults=some_dict); it is used after configuration files and environment variables.

It should be a dict mapping a section name to a dict of key => value:

>>> config = ConfigGetter('myproj', defaults={'db': {'host': 'localhost'}})
>>> config.get('db.host')
'localhost'
Typed getters

getconf can convert options into a few standard types:

config.getbool('db.enabled', False)
config.getint('db.port', 5432)
config.getlist('db.tables')  # Expects a comma-separated list
config.getfloat('db.auto_vacuum_scale_factor', 0.2)

Concepts

getconf relies on a few key concepts:

namespace

Each ConfigGetter works within a specific namespace (its first argument).

Its goal is to avoid mistakes while reading the environment: with ConfigGetter(namespace='myproj'), only environment variables beginning with MYPROJ_ will be read

Sections

The configuration options for a project often grow quite a lot; to restrict complexity, getconf splits values into sections, similar to Python’s configparser module.

Section are handled differently depending on the actual configuration source:

  • section.key is mapped to MYPROJ_SECTION_KEY for environment variables

  • section.key is mapped to [section] key = in configuration files

  • section.key is mapped to defaults['section']['key'] in the defaults dict.

Default section

Some settings are actually “globals” for a projet. This is handled by unset section names:

  • key is mapped to MYPROJ_KEY for environment variables

  • key is mapped to [DEFAULT] key = in configuration files

  • key is mapped to defaults['DEFAULT']['key'] in the defaults dict.

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

getconf-1.4.0.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

getconf-1.4.0-py2.py3-none-any.whl (14.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file getconf-1.4.0.tar.gz.

File metadata

  • Download URL: getconf-1.4.0.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/57.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.6

File hashes

Hashes for getconf-1.4.0.tar.gz
Algorithm Hash digest
SHA256 9b81db068e66c798d8f5a87bc14e2ab5fa935ef3f4eb95fc2dcc5fc3d63e0c7c
MD5 58f6b4499defb1bdf1e3860e673b8ebb
BLAKE2b-256 befce45125cf214d0a5020696ca88fb1f4150ffaff6e946c436576005a2e9743

See more details on using hashes here.

File details

Details for the file getconf-1.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: getconf-1.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/57.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.6

File hashes

Hashes for getconf-1.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a10997d3053a4e59bf95da632941ca47e61be57852dfcb1d50df8a151eaf9031
MD5 aa9e4f5c98a65b89b86248e4faf1edb9
BLAKE2b-256 e851df3004be64528076ff497c7fca36d59c3f07b4be70c47ad97392c3e0fbd9

See more details on using hashes here.

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