Implementing custom ConfigSource classes¶
If you want to get configuration settings from some other sources than
the built-in sources, you should create a class that derives from
ConfigSource and implement a few
The constructor of the class should set up needed resources, such as opening and parsing a configuration file.
It is a good idea to keep whatever connection handles, data access objects, or other resources needed to retrieve the settings, as unprocessed as possible. The methods that actually need the data (
typed()) should use those resources directly instead of reading from cached locally stored copies.
The constructor must call the superclass’
__init__method with all remaining keyword arguments, ie.
For writable sources, whether any parameter value in this source has been changed so that a call to
save()might be needed.
A string identifying this source, primarily used with
Whether or not this source can accept changed configuration settings and store them in the same place as the original setting came from.
The parent of this source, if this represents a nested configuration source, or None
By convention, this should be your main connection handle, data access object, or other resource neededed to retrieve the settings.
This method should return true if the parameter identified by
keyis present in this configuration source. It is up to each configuration source to define the semantics of what exactly “is present” means, but a guideline is that only real values should count as being present. If you only have some sort of placeholder or typing information for
keythis should probably not return True.
Note that it is possible that a configuration source would return True for
typed(some_key)and at the same time return False for
has(some_key), if the source only carries typing information, not real values.
Should return the actual value of the parameter identified by
get(some_key)should always succeed. If the configuration source does not include intrinsic typing information (ie. everything looks like a string) this method should return the string as-is, LayeredConfig is responsible for converting it to the correct type.
Should return True if this source contains typing information for
key, ie information about which data type this parameter should be.
For sources where everything is stored as a string, this should generally return False (no way of distinguishing an actual string from a date formatted as a string).
Should return a list (or other iterator) of subsection keys, ie names that represent subsections of this configuration source. Not all configuration sources need to support subsections. In that case, this should just return an empty list.
Should return the subsection identified by
key, in the form of a new object of the same class, but initialized differently. Exactly how will depend on the source, but as a general rule the same resource handle used as
self.sourceshould be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so that
get()and other methods can use it to look in the correct place.
As a general rule, the constructor should be called with a
parentparameter set to
Should set the parameter identified by
keyto the new value
This method should be prepared for any type of value, ie ints, lists, dates, bools... If the backend cannot handle the given type, it should convert to a str itself.
Note that this does not mean that the changes should be persisted in the backend data, only in the existing objects view of the data (only when
save()is called, the changes should be persisted).
Perform some post-initialization setup. This method will be called by the LayeredConfig constructor after its internal initialization is finished, with itself as argument. Sources may access all properties of the config object in order to eg. find out which parameters have been defined.
The sources will be called in the same order as they were provided to the LayeredConfig constructior, ie. lowest precedence first.
Parameters: config (layeredconfig.LayeredConfig) – The initialized config object that this source is a part of
Persist changed data to the backend. This generally means to update a loaded configuration file with all changed data, or similar.
This method will only ever be called if
writableis True, and only if
dirtyhas been set to True.
If your source is read-only, you don’t have to implement this method.
Given a parameter identified by
keyand an untyped string, convert that string to the type that our version of key has.
If your backend data is exposable as a python dict, you can subclass from this class to avoid implementing
subsections(). You only need to write
__init__()(which should set
self.sourceto that exposed dict), and possibly