Confire is a simple but powerful configuration scheme that builds on the configuration parsers of Scapy, elasticsearch, Django and others. The basic scheme is to have a configuration search path that looks for YAML files in standard locations. The search path is hierarchical (meaning that system configurations are overloaded by user configurations, etc). These YAML files are then added to a default, class-based configuration management scheme that allows for easy development.
The easiest and usual way to install confire is to use pip:
pip install confire
To install the package from source, download the latests package tarball, unzip in a temporary directory and run the following command:
python setup.py install
As always, I highly recommend the use of a virtual environment to better manage the software dependencies for your particular code base.
Create a file called “myapp.yaml” and place it in one of the following places:
Create some configuration values inside the file like so:
## Set application environment debug: True testing: False ## A simple database configuration database: name: mydb host: localhost port: 5432 user: postgres
In your code, create a file called “config.py” and add the following:
from confire import Configuration from confire import environ_setting class DatabaseConfiguration(Configuration): host = "localhost" port = 5432 name = "mydb" user = "postgres" password = environ_setting("DATABASE_PASSWORD", required=False) class MyAppConfiguration(Configuration): CONF_PATHS = [ '/etc/myapp.yaml', os.path.expanduser('~/.myapp.yaml'), os.path.abspath('conf/myapp.yaml') ] debug = False testing = True database = DatabaseConfiguration() settings = MyAppConfiguration.load()
Now, everywhere in your code that you would like to access these settings values, simply use as follows:
from config import settings debug = settings.get('DEBUG') or settings['DEBUG']
Voila! A complete configuration system for your application!
Here are a list of topics for more detail about how to use confire in your Python applications.
There are many configuration packages available on PyPI - it seems that everyone has a different way of doing it. However, this is my prefered way, and I found that after I copy and pasted this code into more than 3 projects that it was time to add it as a dependency via PyPI. The configuration builds on what I’ve learned/done in configuring Scapy, elasticsearch, and Django - and builds on these principles:
So there you have it, with these things in mind I wrote confire and I hope you enjoy it!
Confire is open source, and I would be happy to have you contribute! You can contribute in the following ways:
You can contact me on Twitter if needed: @bbengfort
I like cooking, and the thought of preparation in French culinary language appealed to me. The way I got here was to simply change the “g” in config to a “t”. A definition lookup and boom, a name!