Simple configuration management with python.
Project description
simpleconf
Simple configuration management for python
Installation
# released version
pip install python-simpleconf
# Install support for ini
pip install python-simpleconf[ini]
# Install support for dotenv
pip install python-simpleconf[dotenv]
# Install support for yaml
pip install python-simpleconf[yaml]
# Install support for toml
pip install python-simpleconf[toml]
# Install support for all supported formats
pip install python-simpleconf[all]
Features
- Multiple formats supported
- Type casting
- Profile support
- Simple APIs
Usage
Loading configurations
from simpleconf import Config
# Load a single file
conf = Config.load('~/xxx.ini')
# load multiple files, later files override previous ones
conf = Config.load(
'~/xxx.ini', '~/xxx.env', '~/xxx.yaml', '~/xxx.toml',
'~/xxx.json', 'simpleconf.osenv', {'a': 3}
)
# Load a single file with a different loader
conf = Config.load('~/xxx.ini', loader="toml")
Accessing configuration values
from simpleconf import Config
conf = Config.load({'a': 1, 'b': {'c': 2}})
# conf.a == 1
# conf.b.c == 2
Supported formats
.ini/.cfg/.config
(parsed byiniconfig
).- For confiurations without profiles, an ini-like configuration like must have a
default
(case-insensitive) section.
- For confiurations without profiles, an ini-like configuration like must have a
.env
(usingpython-dotenv
). A file with environment variables..yaml/.yml
(usingpyyaml
). A file with YAML data..toml
(usingrtoml
). A file with TOML data..json
(usingjson
). A file with JSON data.XXX.osenv
: System environment variables with prefixXXX_
(case-sensitive) is used.XXX_A=1
will be loaded asconf.A = 1
.
- python dictionary.
Profile support
Loading configurations
Loading dictionaries
from simpleconf import ProfileConfig
conf = ProfileConfig.load({'default': {'a': 1})
# conf.a == 1
Loading a .env
file
config.env
# config.env
default_a=1
from simpleconf import ProfileConfig
conf = ProfileConfig.load('config.env')
# conf.a == 1
Loading ini-like configuration files
# config.ini
[default]
a = 1
from simpleconf import ProfileConfig
conf = ProfileConfig.load('config.ini')
# conf.a == 1
Loading JSON files
config.json
{
"default": {
"a": 1
}
}
from simpleconf import ProfileConfig
conf = ProfileConfig.load('config.json')
# conf.a == 1
Loading system environment variables
from os import environ
from simpleconf import ProfileConfig
environ['XXX_DEFAULT_A'] = '1'
conf = ProfileConfig.load('XXX.osenv')
# conf.a == 1
Loading TOML files
# config.toml
[default]
a = 1
from simpleconf import ProfileConfig
conf = ProfileConfig.load('config.toml')
# conf.a == 1
Loading YAML files
# config.yaml
default:
a: 1
from simpleconf import ProfileConfig
conf = ProfileConfig.load('config.yaml')
# conf.a == 1
Switching profile
from simpleconf import ProfileConfig
conf = ProfileConfig.load(
{'default': {'a': 1, 'b': 2}, 'dev': {'a': 3}, 'prod': {'a': 4}}
)
# conf.a == 1; conf.b == 2
# ProfileConfig.profiles(conf) == ['default', 'dev', 'prod']
# ProfileConfig.pool(conf) == {'default': {'a': 1, 'b': 2}, 'dev': {'a': 3}, 'prod': {'a': 4}}
# ProfileConfig.current_profile(conf) == 'default'
# ProfileConfig.base_profile(conf) == 'default'
ProfileConfig.use_profile(conf, 'dev')
# conf.a == 3; conf.b == 2
# ProfileConfig.current_profile(conf) == 'dev'
# ProfileConfig.base_profile(conf) == 'default'
# use a different base profile
ProfileConfig.use_profile(conf, 'prod', base='dev')
# conf.a == 4 # No 'b' in conf
# ProfileConfig.current_profile(conf) == 'prod'
# ProfileConfig.base_profile(conf) == 'dev'
# Copy configuration instead of inplace modification
conf2 = ProfileConfig.use_profile(conf, 'dev', copy=True)
# conf2 is not conf
# conf2.a == 3; conf2.b == 2
# Use a context manager
with ProfileConfig.use_profile(conf2, 'default'):
conf2.a == 3
conf2.b == 2
# conf2.a == 3; conf2.b == 2
Type casting
For configuration formats with type support, including dictionary, no type casting is done by this library, except that for TOML files.
TOML does not support None
value in python. We use rtoml
library to parse TOML files, which dumps None
as "null"
. So a null_caster
is used to cast "null"
to None
.
A none_caster
is also enabled for TOML files, a pure string of "@none"
is casted to None
.
For other formats, following casters are supported:
Int caster
from os import environ
from simpleconf import Config
environ['XXX_A'] = '@int:1'
conf = Config.load('XXX.osenv')
# conf.a == 1 # int
Float caster
@float:1.0
-> 1.0
Bool caster
@bool:true
-> True
@bool:false
-> False
Python caster
Values are casted by ast.literal_eval()
.
"@python:1" => 1 # or
"@py:1" => 1
"@py:1.0` -> `1.0`
"@py:[1, 2, 3]" => [1, 2, 3]
JSON caster
@json:{"a": 1}
-> {"a": 1}
TOML caster
@toml:a = 1
-> {"a": 1}
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
File details
Details for the file python_simpleconf-0.6.0.tar.gz
.
File metadata
- Download URL: python_simpleconf-0.6.0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/5.15.0-1040-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06c29e0c08e22a41024028c46cd845bdfe3cbba402730fa65fb007fa89315801 |
|
MD5 | 1d44b92491c25d6464fd4168ab87f4c6 |
|
BLAKE2b-256 | e3ef52466817a11ab2313e759e464c9c23207dda08170e9edf2285dff4b13546 |
File details
Details for the file python_simpleconf-0.6.0-py3-none-any.whl
.
File metadata
- Download URL: python_simpleconf-0.6.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/5.15.0-1040-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40e73fbd60d84ff45d22fcd8f7e2ade5fcca65559f8d9865e7750d8bcfadd209 |
|
MD5 | bbe7ef7b0764d88b573572f6da9ca98f |
|
BLAKE2b-256 | 5a60804a0e78edf338a6f1cf6922f7a8700da4eecca64ef5706b2db8d41346f2 |