confini

Parse and merge multiple ini files in python3
git clone git://git.defalsify.org/python-confini.git
Log | Files | Refs | README | LICENSE

commit 6bf4df6d15f449c04326dd821db73179aa7d5138
parent 6c74b0feaaf9e7348b3896324c249206a8a13a3b
Author: nolash <dev@holbrook.no>
Date:   Mon, 12 Oct 2020 20:24:09 +0200

Add prefix string to environment override

Diffstat:
MCHANGELOG | 2++
Mconfini/config.py | 20+++++++++++++++-----
Msetup.py | 2+-
Atest/test_env.py | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 69 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +0.2.0 + - Add optional prefix string for environment overrides 0.1.1 - Add boolean lookup 0.1.0 diff --git a/confini/config.py b/confini/config.py @@ -29,7 +29,7 @@ class Config: parser = configparser.ConfigParser(strict=True) - def __init__(self, config_dir, decrypt=True): + def __init__(self, config_dir, env_prefix=None, decrypt=True): if not os.path.isdir(config_dir): raise OSError('{} is not a directory'.format(config_dir)) self.dir = os.path.realpath(config_dir) @@ -37,6 +37,10 @@ class Config: self.censored = {} self.store = {} self.decrypt = decrypt + self.env_prefix = None + if env_prefix != None: + logg.info('using prefix {} for environment variable override matches'.format(env_prefix)) + self.env_prefix = '{}_'.format(env_prefix) def add(self, value, constant_name): @@ -77,7 +81,15 @@ class Config: for s in self.parser.sections(): for k in self.parser[s]: cn = Config.to_constant_name(k, s) - self.add(os.environ.get(cn, self.parser[s][k]), cn) + cn_env = cn + if self.env_prefix != None: + cn_env = self.env_prefix + cn + val = os.environ.get(cn_env) + if val == None: + val = self.parser[s][k] + else: + logg.info('environment variable {} overrides {}'.format(cn_env, cn)) + self.add(val, cn) def process(self, set_as_current=False): @@ -162,6 +174,4 @@ def config_from_environment(): def config_dir_from_environment(): - return os.environ.get('CIC_CONFIG_DIR') - - + return os.environ.get('CONFINI_DIR') diff --git a/setup.py b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='confini', - version='0.1.1', + version='0.2.0', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no', diff --git a/test/test_env.py b/test/test_env.py @@ -0,0 +1,51 @@ +#!/usr/bin/python + +import os +import unittest +import logging + +from confini import Config + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +class TestEnv(unittest.TestCase): + + wd = os.path.dirname(__file__) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_env_a_override(self): + os.environ['FOO_BAR'] = '43' + inidir = os.path.join(self.wd, 'files') + c = Config(inidir) + c.process() + expect = { + 'FOO_BAR': '43', + 'FOO_BAZ': '029a', + 'BAR_FOO': 'oof', + 'XYZZY_BERT': 'ernie', + } + self.assertDictEqual(expect, c.store) + + + def test_env_b_override(self): + os.environ['ZZZ_FOO_BAR'] = '44' + inidir = os.path.join(self.wd, 'files') + c = Config(inidir, 'ZZZ') + c.process() + expect = { + 'FOO_BAR': '44', + 'FOO_BAZ': '029a', + 'BAR_FOO': 'oof', + 'XYZZY_BERT': 'ernie', + } + self.assertDictEqual(expect, c.store) + + +if __name__ == '__main__': + unittest.main()