Skip to main content

Simple configuration management with python.

Project description

simpleconf

Simple configuration management with python

Installation

# released version
pip install python-simpleconf
# lastest version
pip install git+https://github.com/pwwang/simpleconf

Features

  • Simple! Simple! Simple!
  • Profile switching
  • Supported formats:
    • .ini/.cfg/.config (using ConfigParse)
    • .env (using python-dotenv)
    • .yaml/.yml (using pyyaml)
    • .toml (using toml)
    • .json (using json)
    • systme environment variables
    • python dictionaries
  • Value casting

Usage

Loading configurations

from simpleconf import config

# load a single file
config._load('~/xxx.ini')
# load multiple files
config._load(
   '~/xxx.ini', '~/xxx.env', '~/xxx.yaml', '~/xxx.toml', 
   '~/xxx.json', 'simpleconf.osenv', {'default': {'a': 3}}
)

For .env configurations, variable name uses the profile name as prefix. For example:

default_a=1
default_b=py:1
test_a=2
config._load('xxx.env')
config.a == '1'
config.b == 1
config._use('test')
config.a == '2'
config._revert()
config.a == '1'

Use with to temporarily switch profile:

config._load('xxx.env')
config.a == '1'
config.b == 1
with config._with('test') as cfg
   config.a == '2'
config.a == '1'

For .osenv configurations, for example simpleconf.osenv, only variables with names start with SIMPLECONF_ will be loaded, then the upper-cased profile name should follow.

os.environ['SIMPLECONF_DEFAULT_A'] = 1
os.environ['SIMPLECONF_test_A'] = 2
config._load('simpleconf.osenv')
config.A == 1
config._use('test')
config.A == 2

Priority is decided by the order that configurations being loaded.
In the above example, config.A is 3 anyway no matter whatever value is assigned in prior configurations.

Hint: to get system environment variables always have the highest priority, they should be always loaded last.

Switching profiles

Like ConfigParse, the default profile (section) will be loaded first.

[default]
a = 1
b = 2

[test]
a = 3
config._load('xxx.ini')

config.a == 1
config.b == 2

config._use('test')
config.a == 3
config.b == 2

Note that simpleconf profiles are case-insensitive, and we use uppercase names for the first-layer configurations:

default:
   complicated_conf:
      a = 9
config._load('xxx.yaml')
config.complicated_conf.a == 9

Getting configuration values

simpleconf.config is an instance of ConfigBox from python-box. All methods supported by ConfigBox is applicable with simpleconf.config.
Additionally, we also extended get method to allow user-defined cast method:

config._load('xxx.ini')
config.int('A') == 1
config.float('A') == 1.0

def version(x):
	return '%s.0.0' % x

config.get('A', cast = version) == '1.0.0'

None-profile mode

a: 1
b: 2
from simpleconf import Config
config = Config(with_profile = False)
config._load('xxx.yaml')
config.A == 1
config.B == 2

Note that in .ini configuration file, you still have to use the section name [DEFAULT]

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

python-simpleconf-0.1.7.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

python_simpleconf-0.1.7-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

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