Configuration Loading¶
Load configuration values from different file formats and python structures. Nested dictionaries are flattened using dotted notation.
These flattened keys and values are merged into a
staticconf.config.ConfigNamespace
.
Examples¶
Basic example:
staticconf.YamlConfiguration('config.yaml')
Multiple loaders can be used to override values from previous loaders.
import staticconf
# Start by loading some values from a defaults file
staticconf.YamlConfiguration('defaults.yaml')
# Override with some user specified options
staticconf.YamlConfiguration('user.yaml', optional=True)
# Further override with some command line options
staticconf.ListConfiguration(opts.config_values)
For configuration reloading see staticconf.config.ConfigFacade
.
Arguments¶
Configuration loaders accept the following kwargs:
- error_on_unknown
raises a
staticconf.errors.ConfigurationError
if there are keys in the config that have not been defined by a getter or a schema.- optional
if True only warns on failure to load configuration (Default False)
- namespace
load the configuration values into a namespace. Defaults to the DEFAULT namespace.
- flatten
flatten nested structures into a mapping with depth of 1 (Default True)
Added in version 0.7.0: flatten was added as a kwarg to all loaders
Custom Loader¶
You can create your own loaders for other formats by using
build_loader()
. It takes a single argument, a function, which
can accept any arguments, but must return a dictionary of
configuration values.
from staticconf import loader
def load_from_db(table_name, conn):
...
return dict((row.field, row.value) for row in cursor.fetchall())
DBConfiguration = loader.build_loader(load_from_db)
# Now lets use it
DBConfiguration('config_table', conn, namespace='special')
- class staticconf.loader.CompositeConfiguration(loaders: List[Any] | None = None)[source]¶
Store a list of configuration loaders and their params, so they can be reloaded in the correct order.
- staticconf.loader.AutoConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Deprecated since version v0.7.0: Do not use. It will be removed in future versions.
- staticconf.loader.DictConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from a
dict
.- Parameters:
dict – a dictionary
- staticconf.loader.INIConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from a .ini file
- Parameters:
filename – path to the ini file
- staticconf.loader.JSONConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from a json file.
- Parameters:
filename – path to a json file
- staticconf.loader.ListConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from a list of strings in the form key=value.
- Parameters:
seq – a sequence of strings
- staticconf.loader.ObjectConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from any object. Attributes are keys and the attribute value are values.
- Parameters:
object – an object
- staticconf.loader.PropertiesConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from a properties file
- Parameters:
filename – path to the properties file
- staticconf.loader.PythonConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from a python module.
- Parameters:
module – python path to a module as you would pass it to
__import__()
- staticconf.loader.XMLConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from an XML file.
- Parameters:
filename – path to the XML file
- staticconf.loader.YamlConfiguration(*args: Any, **kwargs: Any) Dict[str, Any] ¶
Load configuration from a yaml file.
- Parameters:
filename – path to a yaml file