Package: navconfig
Navigator NavConfigNavConfig is a configuration tool for getting variables from environment and other sources.
Is used by Navigator Framework, but is possible to use in other applications as well.NavConfig can load Configuration directives from different sources (can be mixed):Environment files (.env)pyproject.toml filesINI files (using configParser)TOML/YAML filesREDIS variablesMemcached VariablesPython files (using settings.py)The main goal of NavConfig is centralize configuration access in a single and
immutable unique point of truth.NavConfig can be shared across several modules.MotivationAny Application requires too many configuration options, some configuration options need to be secrets, credentials, etc, and also, can depend on the environment where the application runs (development, testing, production, etc.).Instead of creating Python files, we are using python-dotenv + INI files to separate concerns (secrets vs configuration options), NavConfig also supports getting data instead of INI files from YAML or TOML files (for complex types), pyproject.toml files or REDIS variables.Installationpipinstallnavconfigif you're looking for supporting memcache,redis:pipinstallnavconfig[memcache,redis]or adding all features, including logging facility with Logstash Support:pipinstallnavconfig[all]QuickstartFirst of all, let's create a simple configuration environment.Creates a directory for an .INI file and the environment (.env) file.mkdir{env,etc}put a .env file inside of the "env" folder, the first line is the directive to know where the "INI" file lives (even if we can put the . INI file outside of the current dir).the directory tree is very simple:|- myapp/
|  |- __init__.py
|  |- pyproject.toml
|  |- env/
|  |  |- .env
|  |  |- dev/
|  |  |- .env
|  |  |- prod/
|  |  |- .env
|  |- etc/
|  |  |- myconfig.ini
|  | ...# file: .env
CONFIG_FILE=etc/myconfig.ini # CONFIG FILE reference to INI location.
APP_NAME=My AppNavconfig exposes a "config" object to retrieve your environment variables inside your application.fromnavconfigimportconfigAPP_NAME=config.get('APP_NAME')# the result is "My App".but also you can use config as a object:fromnavconfigimportconfigAPP_NAME=config.APP_NAME# the result is "My App".Retrieving valuesOnce an instance ofNavConfighas been installed, values can be retrieved through:>>>config.get("APP_NAME")'My App'there are options available for retrieving values as strings (get), integer (getint()), booleans (getboolean()), but also lists (getlist()) and dictionaries (getdict()).An optional second argument can be provided to anyget*which will be returned as default if
the given config key isn't found:>>>config.get("APP_NAME","My App")'My App'Configuration directoriesBy default,NavConfigwill look for configuration files the base path of project, based on the file type:.env files will be searched on aenv/directory..yml/.toml files will be searched onenv/directory.pyproject.toml file will be searched on root of project..ini files will be searched onetc/directory.Working with EnvironmentsNavConfig can load all environment variables (and the .INI files associated within the .env file) from different directories,
every directory works as a new Environment and you can split your configuration for different environments, like this:env/
.
├── dev
|  |- .env
├── prod
|  |- .env
├── staging
|  |- .env
└── experimental
|  |- .envThen, you can load your application using the "ENV" environment variable:ENV=devpythonapp.pyConfigure LoggingNavConfig has owns logging facility, if you load logging_config from Navconfig, you will get
a logging configuration using the Python dictConfig format.also, if you put an environment variable called "logstash_enabled = True", there is a ready to use Logging facility using Logstash.importloggingfromnavconfig.loggingimport(logdir,loglevel,logging_config)fromlogging.configimportdictConfigdictConfig(logging_config)To use just the logger as expected with logging.getLogger(), e.g.logger=logging.getLogger('MY_APP_NAME')logger.info('Hello World')By default, the current logging configuration make echo to the standard output:[INFO]2022-03-1119:31:39,408navigator:HelloWorldCustom Settingswith Navconfig, users can create a python module called "settings.py" on package "settings" to create new configuration options and fine-tune their configuration.|- myapp/
|  |- settings/
|  |- __init__.py
|  |- settings.pyon "settings.py", we can create new variables using python code:fromnavconfigimportconfig,DEBUGprint('::: LOADING SETTINGS ::: ')# we are in local aiohttp development?LOCAL_DEVELOPMENT=(DEBUGisTrueandsys.argv[0]=='run.py')SEND_NOTIFICATIONS=config.get('SEND_NOTIFICATIONS',fallback=True)And those variables are reachable using "navconfig.conf" module:fromnavconfig.confimportLOCAL_DEVELOPMENTifLOCAL_DEVELOPMENTisTrue:print('We are in a Local instance.')DependenciesPython >= 3.9ConfigParserPython-DotenvpytomlppPyYAMLredispylibmcContribution guidelinesPlease have a look at the Contribution GuideWriting testsCode reviewOther guidelinesWho do I talk to?Repo owner or adminOther community or team contactLicenseNavConfig is released under MIT license.