Package: klein-config
Klein ConfigModule to provide config managementUsagefromklein_configimportget_config# Can be overriden with env variable MY_CONFIG_SETTINGconfig=get_config({"my":{"config":{"setting":"initialised value"}})# Access via `get` accessor with no backup (raises ConfigMissingException if not found).value=config.get("my.config.setting")# Access via `get` accessor method with a backup.backup_value=config.get("not.a.setting","backup value")# Access via `dict` (raises KeyError if not found).same_value=config["my.config.setting"]# Sub-configs are created if the value is another `dict`.intermediate_config=config["my.config"]same_value_again=intermediate_config["setting"]StructureInternally the config object uses the ConfigTree structure that is part of pyhocon. This can be traversed easily with the get method using dot notation as outlined above.Config InitialisationTheget_configfunction looks for :argument--commonor environmental variableKLEIN_COMMONto specify a valid filepath for a common config file (in either JSON or YAML format); andargument--configor environmental variableKLEIN_CONFIGto specify a valid filepath for a config fileN.B. Passing both environmental variablesandarguments for either config or common is ambiguous and is therefore NOT accepted.You can also pass adictinto theget_configfunction.Example configsJSON:{"rabbitmq":{"host":["localhost"],"port":5672,"username":"username","password":"password"},"mongo":{"host":["mongo.domain.com"],"username":"username","password":"password"}}YAML:mongo:host:-mongo.domain.compassword:passwordusername:usernamerabbitmq:host:-localhostpassword:usernameport:5672username:passwordExample config files are also provided injsonandyamlformats.Order precedenceThe configs are applied to the config object in the following order:Common config as identified via argument--commonor environmental variableKLEIN_COMMONConfig that is injected via the Class constructorConfig that is identified via the argument--configor environmental variableKLEIN_CONFIGConfigs will override any previous values as they are applied.Environment AwareThe module is "Environment Aware", i.e. it will look for environment variables in the first instance. If a valid variable exists then this will be used regardless of any config that may have been supplied.The path is transformed by converting the string to uppercase and replacing all dots with underscores.my.config.setting => MY_CONFIG_SETTINGSub-config items are still overriden by the same environment variables as in the root config.DevelopmentThis project usespipenv. To install it, runpip install pipenv.Developmentpipenv install --devTestingpipenvrunpython-mpytestFor test coverage you can run:pipenvshell
pipenvrunpython-mpytest--cov-reportterm--covsrc/tests/Unit testing config in another library.You need to monkey patch the config library and override what the methods do in your test code.
First create your test version of config. Let's say it is in the moduletests.test_config.fromklein_config.configimportEnvironmentAwareConfigconfig_dict={'consumer':{'name':'text_classifier','queue':'text_classifier',},'mongo':{'host':'localhost','port':27017,'doclib_database':'doclib','documents_collection':'documents','text_classification_collection':'text_classification',}}config=EnvironmentAwareConfig(initial=config_dict)defget_config():returnconfigThen in your test code you can monkey patch the config library like this:importsyssys.modules['klein_config']=__import__('tests.test_config')fromtests.test_configimportconfigNow any test code that uses klein_config will use this test version of the config library.TroubleshootingIf you are unable to runpipenv shelland are having permission errors, you can spin up a virtual environment in which to run
thepipenvcommands:pipinstallvirtualenv//installvirtualenvmodule
virtualenvvenv//createyourvirtualenvironment(runcommandfromprojectrootdirectory)sourcevenv/bin/activate//startthevirtualenvironment
pipenvinstall--dev//installdependencies-youshouldnowbeabletorunthetestswiththeabovecommandsKnown issueLibrary does not install on some python versions.This issue has appeared on a few occasions due to a tagged dependency on pyyaml. This library requires certain version of python to install.If this library does not install on pyyaml failure, you can still install and use this library with using --no-deps flag and manually install the dependencies.pip install --no-deps klein_config   # Don't forget to install the dependencies as neededLicenseThis project is licensed under the terms of the Apache 2 license, which can be found in the repository asLICENSE.txt