astropy/astropy — Issue #48: Support overriding config options via environment variables
Author: iguananaut
Created: 2011-11-04 00:27:07+00:00
State: open

I first wrote this as a comment in #35, but I'm creating a separate issue for this so it doesn't get forgotten. I will volunteer to implement it. This is slightly higher priority now that I'm getting somewhere with PyFITS implementation, and I would like to hook it into the eventual config system.

It would be good to have the ability to override any config options via environment variables. These environment variables would follow some standard naming convention like `ASTROPY_SUB_PACKAGE_OPTION`. There could also be some way to register shorter aliases for commonly used options, but I don't have any thoughts yet on the specifics of that.
In order to support these environment variables transparently, the easiest approach I see (which I usually end up doing anyways) is to provide a simple wrapper around `ConfigObj` objects. I like to use something simple like `Configuration`. This is a much simpler object than the `ConfigObj` so that fewer details of reading/writing the configuration are exposed to subpackage/affiliated package authors. It would still provide a dictionary-like interface and many of the methods of `ConfigObj`. Though rather than `write()` it has a `save()` method which I think is a little more obvious. If a user wants to update the configuration they simply call `config.save()`. This method would prevent saving any values that were overridden by environment variables.
Using a wrapper can have other eventual benefits, since it allows us to provide some abstractions around the configuration object that aren't specific to any one backend implementation (not that I would suggest we move away from `ConfigObj` or anything--it's just nice to have additional flexibility).