A special note from the author and maintainer...
This project was conceived to make a bunch of poorly factored apps work a little better.
It succeeded at that effort to some extent, but it's fundamentally a crappy way to do config.
If you're using this in or after 2020, that really sucks. Change.
Mandrel provides bootstrapping and configuration tools for consistent, straightforward project config management.
Use Mandrel to:
Separate projects can rely on Mandrel for these purposes, and when they're brought together (as eggs, for instance) to a single application, their configuration can be managed simply in an easily-configurable way.
Suppose you want to be able to look for configuration files across several directories, in order of precedence:
./
~/.whizzies/
/etc/whizzies/
Suppose further that you have one aspect of your app that deals with storage, and another that deals with analysis. You configure them separately.
So at the root of your project, you would add a file named Mandrel.py
,
and in it you would say:
bootstrap.SEARCH_PATHS.extend(['~/.whizzies', '/etc/whizzies'])
Now name your YAML configuration files for the subsections of your app:
storage.yaml
analysis.yaml
You can easily load configuration dictionaries from YAML files if those files exist on the bootstrap.SEARCH_PATH:
analysis_config = mandrel.config.get_configuration('analysis')
storage_config = mandrel.config.get_configuration('storage')
If you want a standard configuration system-wide, put it in /etc/whizzies.
But then if you want a configuration for a particular user, put it in ~user/.whizzies. That will completely override the system-wide one when present.
And of course, the SEARCH_PATH just looks in the current working directory, so the most-specific config can go there, again completely overruling the lower-precedence config.
Similarly, a logging.cfg
file can be placed somewhere in the search
path, and you can use mandrel.bootstrap.get_logger(logger_name)
to
get loggers. The bootstrapper will ensure that logging config is
properly applied.
Take it farther:
class StorageConfig(mandrel.config.Configuration):
NAME = 'storage'
Now the StorageConfig
class easily wraps the lookup of "storage.yaml",
and makes the key/value pairs therein available as attributes on a
StorageConfig
instance.
Add functionality to your class to enforce particular defaults, ensure values are properly formatted, constrained, etc.
You can fetch a logger named "storage" from it, too:
logger = StorageConfig.get_logger()
Mandrel is free software and is released under the terms of the MIT license (http://opensource.org/licenses/mit-license.php), as specified in the accompanying LICENSE.txt file.