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
(usingConfigParse
).env
(usingpython-dotenv
).yaml/.yml
(usingpyyaml
).toml
(usingtoml
).json
(usingjson
)- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python_simpleconf-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8797d7cfabc200e61557b6d56324450dabe4a01393970a7c43320da962e1bddd |
|
MD5 | 6f95e0fd17766d8a1b22f7d63f4e9a9d |
|
BLAKE2b-256 | 9c7b39e78a220ee6bc98e3e058d7fcf3d055ed9d390161c0c11a975ff13f350f |