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)
New 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=None)[source]¶ Store a list of configuration loaders and their params, so they can be reloaded in the correct order.
-
staticconf.loader.
YamlConfiguration
(*args, **kwargs)¶ Load configuration from a yaml file.
Parameters: filename – path to a yaml file
-
staticconf.loader.
JSONConfiguration
(*args, **kwargs)¶ Load configuration from a json file.
Parameters: filename – path to a json file
-
staticconf.loader.
ListConfiguration
(*args, **kwargs)¶ Load configuration from a list of strings in the form key=value.
Parameters: seq – a sequence of strings
-
staticconf.loader.
DictConfiguration
(*args, **kwargs)¶ Load configuration from a
dict
.Parameters: dict – a dictionary
-
staticconf.loader.
AutoConfiguration
(*args, **kwargs)¶ Deprecated since version v0.7.0: Do not use. It will be removed in future versions.
-
staticconf.loader.
PythonConfiguration
(*args, **kwargs)¶ Load configuration from a python module.
Parameters: module – python path to a module as you would pass it to __import__()
-
staticconf.loader.
INIConfiguration
(*args, **kwargs)¶ Load configuration from a .ini file
Parameters: filename – path to the ini file
-
staticconf.loader.
XMLConfiguration
(*args, **kwargs)¶ Load configuration from an XML file.
Parameters: filename – path to the XML file
-
staticconf.loader.
PropertiesConfiguration
(*args, **kwargs)¶ Load configuration from a properties file
Parameters: filename – path to the properties file
-
staticconf.loader.
ObjectConfiguration
(*args, **kwargs)¶ Load configuration from any object. Attributes are keys and the attribute value are values.
Parameters: object – an object