Package: opinionated-configparser
What is it?This is an opinionated Python2/Python3 thin layer over thePython configparser libraryto deal with:configuration variants (PROD, DEV...) expressed as alternative keys in ini filejinja2as (optional) interpolation method for configuration valuesFor Python2, we usePython3 configparser backport.ConceptsConfiguration variantsBasicWith this example:[group1]debug=0debug[DEV]=1We have two default configuration variant for thedebugkey: theDEVone and the default one.If you don't do anything special, thedebugvalue will be0(standard default value).But if you initialize the library withDEVasconfiguration name, thedebugvalue will be1.Now, if you usePRODasconfiguration name, as there is nodebug[PROD]line/variant,
the debug value will fallback to standard value:0(in this example).InheritanceStill with the same example:[group1]debug=0debug[DEV]=1What about if we useDEV_JOHN_MONDAYasconfiguration namewhen initializing the library?As there is nodebug[DEV_JOHN_MONDAY]line,
one might think that the retained value would be the default one:0.In fact, the retrained value will be1! Why? Because_(underscore) has a special meaning
in configuration names. This is a kind of inheritance mark.SoDEV_JOHN_MONDAYmeans as a configuration name:useDEV_JOHN_MONDAYif there is a variant with this exact name(else) useDEV_JOHN(first level of inheritance) if there is a variant with this name:DEV_JOHN(else) useDEV(second level of inheritance) if there is a variant with this name:DEV(else) use standard/default valueSo with this example:[group1]debug=0debug[DEV]=1debug[DEV_JOHN]=2debug[DEV_PETER]=3debug[DEV_JOHN_MONDAY]=4debug[DEV_JOHN_TUESDAY]=5debug[QA]=6We get this table:Configuration nameselected value fordebugkeycommentFOO0standard value is usedDEV1exact variantDEV_JOHN_MONDAY4exact variantDEV_JOHN_FRIDAY2DEV_JOHNlevel of inheritance is usedDEV_PETER3exact variantDEV_KATE1DEVlevel of inheritance is usedDEV_SMITH_FOO_BAR_11DEVlevel of inheritance is usedDEV_JOHN_QA2DEV_JOHNlevel of inheritance is usedFOO_QA0theQAlevel can be used only if the configuration name begins withQAQA50theQA5variant does not exist and there is no inheritance because there is nounderscoreQA_56QAlevel of inheritancejinja2 usage inside configuration valuesBy default, we usejinja2as interpolation method
for configuration values. The Jinja2 context is initialized with environment variables.So with this example:[group1]key=This is a Jinja2 test value: {% raw %}{{HOME}}{% endraw %}You will get the{% raw %}{{HOME}}{% endraw %}placeholder replaced by the corresponding environment variable value.Missing variables will be replaced by the empty string (without errors).You can define your own Jinja2 context by addinginterpolation=opinionated_configparser.Jinja2Interpolation(jinja2_context)to theOpinionatedConfigParserconstructor call.Usageopinionated_configparseris just a thin layer overPython configparser library. So you use it exactly in the same way.Just an example:fromopinionated_configparserimportOpinionatedConfigParserTEST_DICT={"section1":{"key1":"value1","key1[foo]":"value2","key1[foo_bar]":"value3","key2":"value4"},"section2":{"key3":"value5"}}parser=OpinionatedConfigParser(configuration_name="foo")parser.read_dict(TEST_DICT)# will output: value2print(parser.get("section1","key1"))# [...]# use the parser object exactly as configparser.ConfigParser one# [...]FAQWhat about if I don't want to use Jinja2 as interpolation method?Just passinterpolation=Nonekeyword argument inOpinionatedConfigParserconstructor.Or useinterpolation=configparser.BasicInterpolation()to get the default
interpolation method ofconfigparserAPI.What about if I want to use different jinja2 template options?Just passinterpolation=opinionated_configparser.Jinja2Interpolation(jinja2_context, **jinja2_template_kwargs)keyword argument inOpinionatedConfigParserconstructor.