Skip to main content

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
  • Async loading support

Usage

Loading configurations

import asyncio
from simpleconf import Config


async def main():
  # 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")

  # Async loading
  conf = await Config.a_load('~/xxx.ini')

if __name__ == "__main__":
  asyncio.run(main())

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 by iniconfig).
    • For confiurations without profiles, an ini-like configuration like must have a default (case-insensitive) section.
  • .env (using python-dotenv). A file with environment variables.
  • .yaml/.yml (using pyyaml). A file with YAML data.
  • .toml (using rtoml). A file with TOML data.
  • .json (using json). A file with JSON data.
  • XXX.osenv: System environment variables with prefix XXX_ (case-sensitive) is used.
    • XXX_A=1 will be loaded as conf.A = 1.
  • python dictionary.
  • Strings format of the above formats are also supported.
    • "{'a': 1}" will be loaded as conf.a = 1.

Profile support

Loading configurations

Loading dictionaries
from simpleconf import ProfileConfig

conf = ProfileConfig.load({'default': {'a': 1})
# conf.a == 1

# Asynchronous loading
# conf = await ProfileConfig.a_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


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.8.2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

python_simpleconf-0.8.2-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file python_simpleconf-0.8.2.tar.gz.

File metadata

  • Download URL: python_simpleconf-0.8.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for python_simpleconf-0.8.2.tar.gz
Algorithm Hash digest
SHA256 21f9e68865ed6762a641db26900a300aee86149ab79e3d430f7811d7fcb4c47e
MD5 e87726ec6374f30aa09130b944edc6d7
BLAKE2b-256 47d0706b269aae49898ab3be8a63268b5fe4602f4e6d215950ccfa19bcf090d7

See more details on using hashes here.

File details

Details for the file python_simpleconf-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: python_simpleconf-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for python_simpleconf-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8462229665207389fab5797104003d52f8c1d6d9d75c0447c5182169f80fdf30
MD5 973bade5870f806830e1d42c32bf3138
BLAKE2b-256 771e951fb1fd65a3609c6154a3ccb9000031fe4c64fc9019e2edf848d76feeb2

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