Package: python-simpleconf
simpleconfSimple configuration management for pythonInstallation# released versionpipinstallpython-simpleconf# Install support for inipipinstallpython-simpleconf[ini]# Install support for dotenvpipinstallpython-simpleconf[dotenv]# Install support for yamlpipinstallpython-simpleconf[yaml]# Install support for tomlpipinstallpython-simpleconf[toml]# Install support for all supported formatspipinstallpython-simpleconf[all]FeaturesMultiple formats supportedType castingProfile supportSimple APIsUsageLoading configurationsfromsimpleconfimportConfig# Load a single fileconf=Config.load('~/xxx.ini')# load multiple files, later files override previous onesconf=Config.load('~/xxx.ini','~/xxx.env','~/xxx.yaml','~/xxx.toml','~/xxx.json','simpleconf.osenv',{'a':3})# Load a single file with a different loaderconf=Config.load('~/xxx.ini',loader="toml")Accessing configuration valuesfromsimpleconfimportConfigconf=Config.load({'a':1,'b':{'c':2}})# conf.a == 1# conf.b.c == 2Supported formats.ini/.cfg/.config(parsed byiniconfig).For confiurations without profiles, an ini-like configuration like must have adefault(case-insensitive) section..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=1will be loaded asconf.A = 1.python dictionary.Profile supportLoading configurationsLoading dictionariesfromsimpleconfimportProfileConfigconf=ProfileConfig.load({'default':{'a':1})# conf.a == 1Loading a.envfileconfig.env# config.env
default_a=1fromsimpleconfimportProfileConfigconf=ProfileConfig.load('config.env')# conf.a == 1Loading ini-like configuration files# config.ini[default]a=1fromsimpleconfimportProfileConfigconf=ProfileConfig.load('config.ini')# conf.a == 1Loading JSON filesconfig.json{"default":{"a":1}}fromsimpleconfimportProfileConfigconf=ProfileConfig.load('config.json')# conf.a == 1Loading system environment variablesfromosimportenvironfromsimpleconfimportProfileConfigenviron['XXX_DEFAULT_A']='1'conf=ProfileConfig.load('XXX.osenv')# conf.a == 1Loading TOML files# config.toml[default]a=1fromsimpleconfimportProfileConfigconf=ProfileConfig.load('config.toml')# conf.a == 1Loading YAML files# config.yamldefault:a:1fromsimpleconfimportProfileConfigconf=ProfileConfig.load('config.yaml')# conf.a == 1Switching profilefromsimpleconfimportProfileConfigconf=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 profileProfileConfig.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 modificationconf2=ProfileConfig.use_profile(conf,'dev',copy=True)# conf2 is not conf# conf2.a == 3; conf2.b == 2# Use a context managerwithProfileConfig.use_profile(conf2,'default'):conf2.a==3conf2.b==2# conf2.a == 3; conf2.b == 2Type castingFor configuration formats with type support, including dictionary, no type casting is done by this library, except that for TOML files.TOML does not supportNonevalue in python. We usertomllibrary to parse TOML files, which dumpsNoneas"null". So anull_casteris used to cast"null"toNone.Anone_casteris also enabled for TOML files, a pure string of"@none"is casted toNone.For other formats, following casters are supported:Int casterfromosimportenvironfromsimpleconfimportConfigenviron['XXX_A']='@int:1'conf=Config.load('XXX.osenv')# conf.a == 1 # intFloat caster@float:1.0->1.0Bool caster@bool:true->True@bool:false->FalsePython casterValues are casted byast.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}