Package: config42
Config42Config42  is a complete configuration reader and manager. It aims to read the configuration from different sources: a memory Dict object, an external file ( YAML, JSON, INI, PYTHON Object ), an SQL database (Postgres, MySQL, Oracle)
alternatively, Key-value data store ( Etcd )It is designed to be extensible. Different handlers could support another data store.
All PR are welcome.Related blog postsEasy configuration management with config42 for Python applicationsInstallFrom sources:(Bleeding edge)pip install git+https://github.com/yurilaaziz/config42From PyPi:(Stable)pip install config42Getting startedConfig42 abstract loading configuration complexity. Let config42 manage your configuration for you.Using environment variablesMost of the containerised applications change behaviour from environment variables to change their behaviour. config42 handle it easily.Here a working sampleexamples/dockerfromconfig42importConfigManagerenv_config=ConfigManager(prefix="MYAPP")# Access to configuration via the ConfigManager getterprint("username :{}".format(env_config.get('username')))print("nested key  :{}".format(env_config.get('secret.one')))Export variables to system environmentexportMYAPP_USERNAME=yuriexportMYAPP_SECRET_ONE=password
pythonapp.pyExport variables to process environmentMYAPP_USERNAME=yuri2pythonapp.pyOnce you build you docker image, you may run the application by export variables into the container environmentdockerrun-eMYAPP_USERNAME=yuri-eMYAPP_SECRET_ONE=secrettestconfig42:latestUsing Etcd  HandlerTo load configuration from Etcd data store, you need to specify thekeyspacewhere the configuration is located, Etcd host(s) and port(s).fromconfig42importConfigManagerfromconfig42.handlersimportEtcdconfig=ConfigManager(handler=Etcd,keyspace='/config')# config = ConfigManager(handler=Etcd, keyspace='/config', port=4001)# config = ConfigManager(handler=Etcd, keyspace='/config', host='127.0.0.1', port=4001)# config = ConfigManager(handler=Etcd, keyspace='/config', host=(('127.0.0.1', 4001), ('127.0.0.1', 4002), ('127.0.0.1', 4003)))Note : Etcd handler usepython-etcdclient
All args after keyspace are passed to Etcd.Client class.Using Filehandler, Load configuration from filefrompprintimportpprintfromconfig42importConfigManagerfromconfig42.handlersimportFileHandler# Yaml filesconfig=ConfigManager(handler=FileHandler,path='files/config1.yml')#config = ConfigManager(handler=FileHandler, path='files/config1.yaml')# Json file#config = ConfigManager(handler=FileHandler, path='files/config1.json')#INI structure support only one level of nesting (Sections = { key: value })#config = ConfigManager(handler=FileHandler, path='files/config.ini')CONFIG=config.as_dict()print("Configuration has been loaded")pprint(CONFIG)# Access to configuration via the ConfigManager getterprint("application_name :{}".format(config.get('application_name')))print("nested key :{}".format(config.get('nested.nestedkey.key2')))# Access to configuration via the as dict utility; it will dump configuration file to data store if updatedprint("user :{}".format(config.as_dict()['user']))# Access to configuration via the classic CONFIG global variableprint("application_name :{}".format(CONFIG['application_name']))print("nested key :{}".format(CONFIG['nested']['nestedkey']['key2']))Using argparse, Load configuration from commandline parameters (sys.argv)fromconfig42importConfigManagerfromconfig42.handlers.argparseimportArgparseschema=[dict(key="user",description="username"),dict(key="verbosity",description="verbosity level",choices=["debug","info"]),]config=ConfigManager(handler=Argparse,schema=schema)Please take a look to theexample using Argparse's handlerReal use caseBelow is a real use from Instabot-Py project that uses this library as a configuration manager.config42 handles 4 sources of configuration data in order of priority:Default configuration from Dict ObjectEnvironment variables prefixed by INSTABOTLocal file where value located in config.file (INSTABOT_CONFIG_FILE)Etcd data-storeref :https://github.com/instabot-py/instabot.pyimportlogging.configimportosfromconfig42importConfigManagerfrominstabot_py.default_configimportDEFAULT_CONFIGenv_config=ConfigManager(prefix="INSTABOT")logging.basicConfig(level=logging.DEBUGifenv_config.get("debug")elselogging.INFO)LOGGER=logging.getLogger(__name__)config=ConfigManager()config.set_many(DEFAULT_CONFIG)config.set_many(env_config.as_dict())config_file=config.get("config.file")config_etcd=config.get("config.etcd")ifconfig_file:ifconfig_file.startswith("/"):config_path=config_fileelse:cwd=os.getcwd()config_path=cwd+"/"+config_fileconfig.set_many(ConfigManager(path=config_path.replace('//','/')).as_dict())LOGGER.info("Setting configuration from{}: OK".format(config_file))ifconfig_etcd:ifnotconfig_etcd.get("keyspace"):raiseException("etcd Keyspace is mandatory")try:config.set_many(ConfigManager(**config_etcd).as_dict())LOGGER.info("Setting external configuration from{}: OK".format(config_file))exceptExceptionasexc:LOGGER.error("Setting external configuration from ({}) : NOT OK".format(",".join({key+"="+valueforkey,valueinconfig_etcd.items()or{}})))LOGGER.exception(exc)raiseexclogging.config.dictConfig(config.get("logging"))RequirementsYaml configuration filespipinstallPyamlEtcd data storepipinstallpython-etcdDEVThe following packages are needed to run tests and coveragepipinstalltoxpytest-covpytestflake8orpipinstall-rrequirements/ci.txt
pipinstall-rrequirements/tests.txtUsage examplesfrom-etcd.pyfrom-yaml.pyTODOreadthedoc with sphinxReleases0.3.1Add set default variables feature0.3Add Environment Handler0.2Add Etcd HandlerAdd Ini Yaml, Json Handlers