A simple python3 config library based ongolang's spf13/viper
Project description
Gila
Gila is a python3 layered configuration library based very heavily on the viper config library for Go. It is designed to facilitate making 12 factor apps as easy as possible using python3.
Motivation
After having used the Viper config library in Go, I became very used to the ease of use and flexibility that it offered. After looking for a library to use in my python projects I was unable to find one that combined all of the very useful features that Viper supports.
Features
- Allow default values to be set for each config key
- Automatically find config files on multiple paths
- Load in environment variables automatically that have a specific prefix
- Support most popular config languages:
yaml, toml, json, properties files, hcl, dotenv
- Singleton pattern available for ease of use in most applications
- Hierarchical loading for non-ambiguous results.
Installation
Requires python 3.6+
pip install gila
Simple Example
More examples can be found in the examples directory
import gila
from os import environ
# Setting default values for keys
gila.set_default('host', 'localhost:8080')
gila.set_default('allow_insecure', False)
# This will tell Gila to automatically load in ENV vars that start with GILA
gila.set_env_prefix('GILA')
gila.automatic_env()
# This would normally be done outside of program
# eg. in docker compose or k8s manifest
environ['GILA_HOST'] = 'localhost:9999'
# This is the preferred method of grabbing
# the configurations. Other methods exist (all_config, debug)
# but this method is the most performant for single
# value calls.
host = gila.get('host')
allow_insecure = gila.get('allow_insecure')
"""
Output:
Host: localhost:9999
Insecure allowed: false
"""
print(f'Host: {host}')
print(f'Insecure allowed: {allow_insecure}')
Supported Config Filetypes
- yaml (
.yaml, .yml
) - json (
.json
) - toml (
.toml
) - hcl (
.hcl
) - environment variables
- Values will be cast to strings
- Keys are cast to lowercase: ENV_VAR ->
gila.get('env_var')
- properties file (
.properties, .prop, .props
)- Values will be cast to strings
- dotenv (
.env
)- Values will be cast to strings
- No nested values supported
Additional Help
Check out our documentation on ReadTheDocs or if you prefer minimum working examples, checkout the directory </examples/> which house a plethora of use cases from simple to more complex configurations.
Credits
Steve Francia spf13 for creating the Viper library
Authors of pyyaml, pyhcl, and toml packages, as Gila would have been much harder to create and maintain without them
Major differences from Viper
- Case sensitivity - all keys in Gila are case sensitive, this allows for proper json, yaml, and toml support. ENV keys are not case sensitive unless manually bound.
- No support for remote config - while I think that supporting consul and etcd is a very nice feature, I think at this moment in time that it is a feature that belongs in a seperate library.
- No support for command flags - Viper relies pretty heavily on Cobra companionship for command flags. You should choose your own flavour of CLI parser (docopts, argparse, etc) and pair it with this library (check out our examples).
- Watching for changes in config files - I would very much so like to add this feature, but it is not a feature that I felt was necessary at
v1.0.0
. If you would like this feature you can thumbs up the issue, or open a PR with an implementation.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.