Configuration tool for all Navigator Services Tool for accessing Config info from different sources.
Project description
Navigator NavConfig
NavConfig 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 files
- INI files (using configParser)
- TOML/YAML files
- REDIS variables
- Memcached Variables
- Python 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.
Motivation
Any 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.
Installation
pip install navconfig
if you're looking for supporting memcache,redis:
pip install navconfig[memcache,redis]
or adding all features, including logging facility with Logstash Support:
pip install navconfig[all]
Quickstart
First 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 App
Navconfig exposes a "config" object to retrieve your environment variables inside your application.
from navconfig import config
APP_NAME = config.get('APP_NAME')
# the result is "My App".
but also you can use config as a object:
from navconfig import config
APP_NAME = config.APP_NAME
# the result is "My App".
Retrieving values
Once an instance of NavConfig
has 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 any get*
which will be returned as default if
the given config key isn't found:
>>> config.get("APP_NAME", "My App")
'My App'
Configuration directories
By default, NavConfig
will look for configuration files the base path of project, based on the file type:
- .env files will be searched on a
env/
directory. - .yml/.toml files will be searched on
env/
directory. - pyproject.toml file will be searched on root of project.
- .ini files will be searched on
etc/
directory.
Working with Environments
NavConfig 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
| |- .env
Then, you can load your application using the "ENV" environment variable:
ENV=dev python app.py
Configure Logging
NavConfig 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.
import logging
from navconfig.logging import (
logdir,
loglevel,
logging_config
)
from logging.config import dictConfig
dictConfig(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-11 19:31:39,408 navigator: Hello World
Custom Settings
with 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.py
on "settings.py", we can create new variables using python code:
from navconfig import config, DEBUG
print('::: LOADING SETTINGS ::: ')
# we are in local aiohttp development?
LOCAL_DEVELOPMENT = (DEBUG is True and sys.argv[0] == 'run.py')
SEND_NOTIFICATIONS = config.get('SEND_NOTIFICATIONS', fallback=True)
And those variables are reachable using "navconfig.conf" module:
from navconfig.conf import LOCAL_DEVELOPMENT
if LOCAL_DEVELOPMENT is True:
print('We are in a Local instance.')
Dependencies
- Python >= 3.9
- ConfigParser
- Python-Dotenv
- pytomlpp
- PyYAML
- redis
- pylibmc
Contribution guidelines
Please have a look at the Contribution Guide
- Writing tests
- Code review
- Other guidelines
Who do I talk to?
- Repo owner or admin
- Other community or team contact
License
NavConfig is released under MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for navconfig-1.7.1-pp310-pypy310_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a56c654f963731d1e898bc6ac7e0524ed45b94b0fb48e7c8b5cd001b82ddb583 |
|
MD5 | e785152d51b8a224c9038a6b9f3f548f |
|
BLAKE2b-256 | 06649a951f63230a135ce39d84199d1d06855d020ff62922c4517801d3034b0a |
Hashes for navconfig-1.7.1-pp39-pypy39_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15c2e58ec54ab83f68310a46d27572eda0fd0edc62ae5e0594d84d6dd64ee9d8 |
|
MD5 | 319cb10c91668fd3037bc0f05e70cf76 |
|
BLAKE2b-256 | 7394988829b9b7f102c6fa14f8f430544627b6859f6e0a18934608422dc69912 |
Hashes for navconfig-1.7.1-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff028d96902f59ed5f354077d578c7bc4211e9a69e9928ca31c9cd7e2dd7b501 |
|
MD5 | 546d678bea30739f78ad8a908f76933a |
|
BLAKE2b-256 | 754fee4aec5d48285170fb4a732a7af2f501519d27f2afdbb9a0a13a3a5006b0 |
Hashes for navconfig-1.7.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea5331195fc114f6caec450f1f11e2ced31e35d20e64e5556b776d23b86a4927 |
|
MD5 | 6c47020e0a830b64662384a7a980f770 |
|
BLAKE2b-256 | b9fef6a822f8ba92d24593d303f1cea64015c74e8b782062279d919e8f619368 |
Hashes for navconfig-1.7.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17c36bb6b8c777833ba8e2167b6c3e1fe59e7af187bbc3fccaba37ae7dfd394b |
|
MD5 | 6b84d89c4f0161be23960beba7b87c3b |
|
BLAKE2b-256 | d02692faaa85d40d50aa7681e26657727fbca6c1c3b86badfa60070d0c95788c |
Hashes for navconfig-1.7.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1eae40774e0493f5d5ef1fe1536cade3f81c3b628afbd5c62b2287adf0c3805 |
|
MD5 | 5e2bb80cc584485cea5d4b53a1b6cbb0 |
|
BLAKE2b-256 | 9cbbb146890886d02968adfed41a42b509069052fd5de11988bd8c197e60d8f5 |
Hashes for navconfig-1.7.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c0528f2a166990298cb560de353c1acfed213e97eac13819d3164128ca2801a |
|
MD5 | 4d5b31d21982aa96dab7fb125bd73dca |
|
BLAKE2b-256 | 62545290c420b19119b670a149163b9e983c90d83a9003d38355931aa926917c |
Hashes for navconfig-1.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1adceae480753e07aa27598a3c8c219fabe4084673bea45fde1874afd0931861 |
|
MD5 | 5fd4cbe6bef6d3fb4ed101a8f5488124 |
|
BLAKE2b-256 | 652e0dd86dbaa399983a3f9d6b02ca84613ed3738d8de855f4a597ac7c2e25f6 |
Hashes for navconfig-1.7.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a2d901e0d6907b69694d373d9e76c582072bac6b6c9c10f6ecb678c7296c766 |
|
MD5 | 3115ea87c5f300ca3cc3679a8503e7c4 |
|
BLAKE2b-256 | c170274aa5780de8ab11349f11ee4368e6528792ba3e991a13194528e79cff62 |
Hashes for navconfig-1.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dac1907412ff70acb7b5ecba4b6998be3d77278dade62710634d772ee15ec04 |
|
MD5 | 0bacb4d065e8667d11523b9ebc251e44 |
|
BLAKE2b-256 | 541c170cdca478182ea350bf1c20febca5d1c5acb437060db9c07c276861d770 |