Blog of Rob Galanakis (@robgalanakis)

Configuration in Host the Docs

With Host the Docs, I chose to eschew configuration files and use a sphinx-style “conf.py” approach (I have previously written about how I like this approach). If a conf.py file is found, it is used for configuration, allowing it to override default configuration options. I also allow configuration through environment variables, where the environment variable name is the same as the conf.py variable but with HTD_ prepended.

So for example, in hostthedocs/getconfig.py, I have code like the following:

try:
    import conf
except ImportError:
    conf = None

def get(attr, default):
    result = os.getenv('HTD_' + attr.upper())
    if result is None:
        result = getattr(conf, attr, None)
    if result is None:
        result = default
    return result

welcome = get('welcome', 'Welcome to Host the Docs!')
# Other configuration values

I was initially going to use a json or yaml file, but always find their contracts a bit unclear, especially when paths are involved in the configuration (if the config uses relative paths, what are the paths relative to?). It also involves more code.

I was really happy with how using Python for configuration worked out in Host the Docs.

One thought on “Configuration in Host the Docs

  1. Daniel Watts says:

    Big fan of using the host language for data/configuration when it makes sense to do so. Yegge’s ancient rant touching on the evil of XML compare to actual languages comes to mind.

    Python’s permissive truthiness almost got me annoyed, except that my initial thought was silly anyway. The entire get function is almost something pleasantly concise: os.getenv(…) or getattr(…) or default, except for the annoying presence of entirely valid False (and, because Python, numeric zero/empty string/empty list etc) values in the config data.

    Collapsing a chain of Maybe/Option-like things is such a common situation that I’m sure there are elegant Pythonic solutions, but then I’m getting into silly overengineering territory for only two such values.

Leave a Reply