Skip to main content

Layered configuration library

Project description

StingConf

Build Status codecov

This is a configuration library through envrionment variables, command line arguments, configuration file and default value.

Installation

pip install stingconf

Examples

define args with functions

  • example.py
import stingconf

parser = stingconf.Parser('Example module')
parser.env_prefix('SC')
parser.conf_file('config.yml')
parser.add('conf-a', short='a', type=int, default=0)
parser.add('conf-b', short='b', type=float, default=0.0)
parser.add('conf-c', short='c', type=str, default='conf-c')
parser.add('conf-d', short='d', default='conf-d', help='4th config')
config = parser.parse()

print('CONF_A:', config.CONF_A)
print('CONF_B:', config.CONF_B)
print('CONF_C:', config.CONF_C)
print('CONF_D:', config.CONF_D)
  • config.yml
conf_a: 100
conf_b: 1.0
conf_c: file-conf-c
  • run example
$ export SC_CONF_A=300
$ python example.py --conf-a 200 -b 20.0
CONF_A: 300
CONF_B: 20.0
CONF_C: file-conf-c
CONF_D: conf-d

define args with object

  • example.py
import stingconf

definitions = {
    'env_prefix': 'SC',
    'conf_file': 'config.yml',
    'items': {
        'conf-a': {
            'arg': {'short': 'a'},
            'type': 'int',
            'default': 0,
        },
        'conf-b': {
            'arg': {'short': 'b'},
            'type': 'float',
            'default': 0.0,
        },
        'conf-c': {
            'arg': {'short': 'c'},
            'type': 'str',
            'default': 'conf-c',
        },
        'conf-d': {
            'arg': {'short': 'd'},
            'default': 'conf-d',
            'help': '4th config',
        },
    },
}
parser = stingconf.Parser('Example module', definitions)
config = parser.parse()

define args with file

  • definitions.yml
env_prefix: SC
conf_file: config.yml
order:
  - env
  - arg
  - file
  - default
items:
  user:
    help: 'login user'
    arg:
      short: u
  password:
    help: 'login passowrd'
    arg:
      short: p
  enable:
    type: bool
    default: false
    help: 'enable something'
    arg:
      long_prefix: '-'
  http-proxy:
    env:
      no_prefix: true
      ignorecase: true
  https-proxy:
    env:
      no_prefix: true
      ignorecase: true
  • example.py
import yaml
import stingconf

with open('definitions.yml') as f:
	definitions = yaml.safe_load(f)
parser = stingconf.Parser('Example module', definitions)
config = parser.parse()

Naming convention

Environment

Environment variables' name should be set LIKE_THIS.

If desired, you can set the prefix of name by Parser.env_prefix().

Argument

Arguments' name should be set like-this.

If desired, you can use short version argument by passing short to Parser.add()

File

Variables' name in the configuration file should be set like_this

Currently, the supported formats are json and yaml, and the content must be non-nested dictionary.

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

stingconf-0.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

stingconf-0.0.3-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file stingconf-0.0.3.tar.gz.

File metadata

  • Download URL: stingconf-0.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for stingconf-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e1df04fd6f0d7588efe7c192838631ebe2ce88de8b8641ba93dbef1c7aad87a4
MD5 cb179866918ac3387ca3f3c87faa3eff
BLAKE2b-256 7b994ef390375a7850fc22e376a585527d3a77437c40e4173beff1a424ea9f49

See more details on using hashes here.

File details

Details for the file stingconf-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: stingconf-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for stingconf-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e96eacf0a695f7b2ba1a7056db7a269db5b2a5abd225b176966090ef5f1fad31
MD5 42a8b8a5edfb46d6800deddb72b73d31
BLAKE2b-256 3099e5d40c51f3fbeb2e521b0d5e360ba5b9e17266ffd2ddec2c34d40f6a2f1a

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