layeredconfig package¶
Submodules¶
layeredconfig.commandline module¶
-
class
layeredconfig.commandline.
Commandline
(commandline=None, parser=None, sectionsep='-', add_help=True, **kwargs)[source]¶ Bases:
layeredconfig.configsource.ConfigSource
Load configuration from command line options. Any long-style parameters are turned into configuration values, and parameters containing the section separator (by default
"-"
) are turned into nested config objects (i.e.--module-parameter=foo
results inself.module.parameter == "foo"
.If an initialized ArgumentParser object is provided, the defined parameters in that object is used for supporting short form options (eg.
'-f'
instead of'--force'
), typing information and help text. The standards argparse feature of printing a helpful message when the ‘-h’ option is given is retained.Parameters: - commandline (list) – Command line arguments, in list form like
sys.argv
. If not provided, uses the realsys.argv
. - parser (argparse.ArgumentParser) – An initialized/configured argparse object
- sectionsep (str) – An alternate section separator instead of
-
. - add_help (bool) – Same as for ArgumentParser()
-
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
has
(key)[source]¶ This method should return true if the parameter identified by
key
is 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 forkey
this 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 forhas(some_key)
, if the source only carries typing information, not real values.
-
rest
= []¶ The remainder of the command line, containing all parameters that couldn’t be turned into configuration settings.
-
set
(key, value)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
setup
(config)[source]¶ 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
-
subsection
(key)[source]¶ 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 asself.source
should be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so thatget()
and other methods can use it to look in the correct place.As a general rule, the constructor should be called with a
parent
parameter set toself
.
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
- commandline (list) – Command line arguments, in list form like
layeredconfig.configsource module¶
-
class
layeredconfig.configsource.
ConfigSource
(**kwargs)[source]¶ Bases:
object
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 (
has()
,get()
,subsection()
,subsections()
and possiblytyped()
) 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.super(MySource, self).__init__(**kwargs)
.-
dirty
= False¶ For writable sources, whether any parameter value in this source has been changed so that a call to
save()
might be needed.
-
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
has
(key)[source]¶ This method should return true if the parameter identified by
key
is 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 forkey
this 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 forhas(some_key)
, if the source only carries typing information, not real values.
-
identifier
= None¶ A string identifying this source, primarily used with
LayeredConfig.set()
.
-
parent
= None¶ The parent of this source, if this represents a nested configuration source, or None
-
save
()[source]¶ 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
writable
is True, and only ifdirty
has been set to True.If your source is read-only, you don’t have to implement this method.
-
set
(key, value)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
setup
(config)[source]¶ 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
-
source
= None¶ By convention, this should be your main connection handle, data access object, or other resource neededed to retrieve the settings.
-
subsection
(key)[source]¶ 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 asself.source
should be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so thatget()
and other methods can use it to look in the correct place.As a general rule, the constructor should be called with a
parent
parameter set toself
.
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
-
typevalue
(key, value)[source]¶ Given a parameter identified by
key
and an untyped string, convert that string to the type that our version of key has.
-
writable
= False¶ Whether or not this source can accept changed configuration settings and store them in the same place as the original setting came from.
-
layeredconfig.defaults module¶
-
class
layeredconfig.defaults.
Defaults
(defaults=None, **kwargs)[source]¶ Bases:
layeredconfig.dictsource.DictSource
This source is initialized with a dict.
Parameters: defaults (dict) – A dict with configuration keys and values. If any values are dicts, these are turned into nested config objects.
layeredconfig.dictsource module¶
-
class
layeredconfig.dictsource.
DictSource
(**kwargs)[source]¶ Bases:
layeredconfig.configsource.ConfigSource
If your backend data is exposable as a python dict, you can subclass from this class to avoid implementing
has()
,get()
,keys()
,subsection()
andsubsections()
. You only need to write__init__()
(which should setself.source
to that exposed dict), and possiblytyped()
andsave()
.-
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
has
(key)[source]¶ This method should return true if the parameter identified by
key
is 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 forkey
this 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 forhas(some_key)
, if the source only carries typing information, not real values.
-
set
(key, value)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
subsection
(key)[source]¶ 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 asself.source
should be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so thatget()
and other methods can use it to look in the correct place.As a general rule, the constructor should be called with a
parent
parameter set toself
.
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
-
layeredconfig.environment module¶
-
class
layeredconfig.environment.
Environment
(environ=None, prefix=None, lower=True, sectionsep='_', **kwargs)[source]¶ Bases:
layeredconfig.configsource.ConfigSource
Loads settings from environment variables. If
prefix
is set toMYAPP_
, the value of the environment variableMYAPP_HOME
will be available as the configuration settinghome
.Parameters: - environ (dict) – Environment variables, in dict form like
os.environ
. If not provided, uses the realos.environ
. - prefix (str) – Since the entire environment is not suitable to use as a configuration, only variables starting with this prefix are used.
- lower (True) – If true, lowercase the name of environment variables (since these typically uses uppercase)
- sectionsep (str) – An alternate section separator instead of
-
.
-
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
has
(key)[source]¶ This method should return true if the parameter identified by
key
is 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 forkey
this 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 forhas(some_key)
, if the source only carries typing information, not real values.
-
set
(key, val)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
subsection
(key)[source]¶ 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 asself.source
should be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so thatget()
and other methods can use it to look in the correct place.As a general rule, the constructor should be called with a
parent
parameter set toself
.
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
- environ (dict) – Environment variables, in dict form like
layeredconfig.etcdstore module¶
-
class
layeredconfig.etcdstore.
EtcdStore
(baseurl='http://127.0.0.1:2379/v2/', **kwargs)[source]¶ Bases:
layeredconfig.configsource.ConfigSource
Loads configuration from a etcd store.
Parameters: baseurl – The main endpoint of the etcd store etcd
has no concept of typed values, so all data from this source are returned as strings.-
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
has
(key)[source]¶ This method should return true if the parameter identified by
key
is 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 forkey
this 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 forhas(some_key)
, if the source only carries typing information, not real values.
-
save
()[source]¶ 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
writable
is True, and only ifdirty
has been set to True.If your source is read-only, you don’t have to implement this method.
-
set
(key=None, value=None)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
subsection
(key)[source]¶ 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 asself.source
should be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so thatget()
and other methods can use it to look in the correct place.As a general rule, the constructor should be called with a
parent
parameter set toself
.
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
-
layeredconfig.inifile module¶
-
class
layeredconfig.inifile.
INIFile
(inifilename=None, rootsection='__root__', sectionsep='.', writable=True, **kwargs)[source]¶ Bases:
layeredconfig.configsource.ConfigSource
Loads and optionally saves configuration files in INI format, as handled by
configparser
.Parameters: - inifile (str) – The name of a ini-style configuration
file. The file should have a top-level
section, by default named
__root__
, whose keys are turned into top-level configuration parameters. Any other sections in this file are turned into nested config objects. - rootsection (str) – An alternative name for the top-level section. See note below.
- sectionsep (str) – separator to use in section names to separate nested subsections. See note below.
- writable (bool) – Whether changes to the LayeredConfig object that has this INIFile object amongst its sources should be saved in the INI file.
Note
Nested subsections is possible, but since the INI format does not natively support nesting, this is accomplished through specially-formatted section names, eg the config value mymodule.mysection.example would be expressed in the ini file as:
[mymodule.mysection] example = value
Since this source uses
configparser
, and since that module handles sections named[DEFAULT]
differently, this module will have a sort-of automatic cascading feature for subsections ifDEFAULT
is used asrootsection
-
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
has
(key)[source]¶ This method should return true if the parameter identified by
key
is 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 forkey
this 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 forhas(some_key)
, if the source only carries typing information, not real values.
-
save
()[source]¶ 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
writable
is True, and only ifdirty
has been set to True.If your source is read-only, you don’t have to implement this method.
-
set
(key, value)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
subsection
(key)[source]¶ 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 asself.source
should be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so thatget()
and other methods can use it to look in the correct place.As a general rule, the constructor should be called with a
parent
parameter set toself
.
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
- inifile (str) – The name of a ini-style configuration
file. The file should have a top-level
section, by default named
layeredconfig.jsonfile module¶
-
class
layeredconfig.jsonfile.
JSONFile
(jsonfilename=None, writable=True, **kwargs)[source]¶ Bases:
layeredconfig.dictsource.DictSource
Loads and optionally saves configuration files in JSON format. Since JSON has some support for typed values (supports numbers, lists, bools, but not dates or datetimes), data from this source are sometimes typed, sometimes only available as strings.
Parameters: -
save
()[source]¶ 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
writable
is True, and only ifdirty
has been set to True.If your source is read-only, you don’t have to implement this method.
-
set
(key, value)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
typed
(key)[source]¶ 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).
-
layeredconfig.layeredconfig module¶
-
class
layeredconfig.layeredconfig.
LayeredConfig
(*sources, **kwargs)[source]¶ Bases:
object
Creates a config object from one or more sources and provides unified access to a nested set of configuration parameters. The source of these parameters a config file (using .ini-file style syntax), command line parameters, and default settings embedded in code. Command line parameters override configuration file parameters, which in turn override default settings in code (hence Layered Config).
Configuration parameters are accessed as regular object attributes, not dict-style key/value pairs. Configuration parameter names should therefore be regular python identifiers, and preferrably avoid upper-case and “_” as well (i.e. only consist of the characters a-z and 0-9)
Configuration parameter values can be typed (strings, integers, booleans, dates, lists…). Even though some sources lack typing information (eg in INI files, command-line parameters and enviroment variables, everything is a string), LayeredConfig will attempt to find typing information in other sources and convert data.
Parameters: - *sources – Initialized ConfigSource-derived objects
- cascade (bool) – If an attempt to get a non-existing parameter
on a sub (nested) configuration object should
attempt to get the parameter on the parent
config object.
False
by default, - writable (bool) – Whether configuration values should be mutable.
True
by default. This does not affectset()
.
-
static
boolconvert
(value)[source]¶ Convert the string value to a boolean.
"True"
is converted toTrue
and"False"
is converted toFalse
.Note
If value is neither “True” nor “False”, it’s returned unchanged.
-
static
dateconvert
(value)[source]¶ Convert the string value to a
date
object. value is assumed to be on the form “YYYY-MM-DD”.
-
static
datetimeconvert
(value)[source]¶ Convert the string value to a
datetime
object. value is assumed to be on the form “YYYY-MM-DD HH:MM:SS” (optionally ending with fractions of a second).
-
static
dump
(config)[source]¶ Returns the entire content of the config object in a way that can be easily examined, compared or dumped to a string or file.
Parameters: config – The configuration object to dump Return type: dict
-
static
get
(config, key, default=None)[source]¶ Gets a value from the config object, or return a default value if the parameter does not exist, like
dict.get()
does.
-
static
set
(config, key, value, sourceid='defaults')[source]¶ Sets a value in this config object without marking any source dirty, and with exact control of exactly where to set the value. This is mostly useful for low-level trickery with config objects.
Parameters: - config – The configuration object to set values on
- key – The parameter name
- value – The new value
- sourceid – The identifier for the underlying source that the value should be set on.
-
static
write
(config)[source]¶ Commits any pending modifications, ie save a configuration file if it has been marked “dirty” as a result of an normal assignment. The modifications are written to the first writable source in this config object.
Note
This is a static method, ie not a method on any object instance. This is because all attribute access on a LayeredConfig object is meant to retrieve configuration settings.
Parameters: config (layeredconfig.LayeredConfig) – The configuration object to save
layeredconfig.plistfile module¶
-
class
layeredconfig.plistfile.
PListFile
(plistfilename=None, writable=True, **kwargs)[source]¶ Bases:
layeredconfig.dictsource.DictSource
Loads and optionally saves configuration files in PList format. Since PList has some support for typed values (supports numbers, lists, bools, datetimes but not dates), data from this source are sometimes typed, sometimes only available as strings.
Parameters: -
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
save
()[source]¶ 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
writable
is True, and only ifdirty
has been set to True.If your source is read-only, you don’t have to implement this method.
-
set
(key, value)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
-
layeredconfig.pyfile module¶
-
class
layeredconfig.pyfile.
PyFile
(pyfilename=None, **kwargs)[source]¶ Bases:
layeredconfig.configsource.ConfigSource
Loads configuration from a python source file. Any variables defined in that file will be interpreted as configuration keys. The class
Subsection
is automatically imported into the context when the file is executed, and represents a subsection of the configuration. Any attribute set on such an object is treated as a configuration parameter on that subsection.Note
The python source file is loaded and interpreted once, when creating the PyFile object. If a value is set by eg. calling a function, that function will only be called at load time, not when accessing the parameter.
Parameters: pyfile (str) – The name of a file containing valid python code. -
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
has
(key)[source]¶ This method should return true if the parameter identified by
key
is 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 forkey
this 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 forhas(some_key)
, if the source only carries typing information, not real values.
-
set
(key, value)[source]¶ Should set the parameter identified by
key
to the new valuevalue
.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).
-
subsection
(key)[source]¶ 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 asself.source
should be passed to the new object. Often, the subsection key will need to be provided to the new object as well, so thatget()
and other methods can use it to look in the correct place.As a general rule, the constructor should be called with a
parent
parameter set toself
.
-
subsections
()[source]¶ 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.
-
typed
(key)[source]¶ 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).
-
layeredconfig.yamlfile module¶
-
class
layeredconfig.yamlfile.
YAMLFile
(yamlfilename=None, writable=True, **kwargs)[source]¶ Bases:
layeredconfig.dictsource.DictSource
Loads and optionally saves configuration files in YAML format. Since YAML (and the library implementing the support, PyYAML) has automatic support for typed values, data from this source are typed.
Parameters: -
get
(key)[source]¶ Should return the actual value of the parameter identified by
key
. Ifhas(some_key)
returns True,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.
-
save
()[source]¶ 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
writable
is True, and only ifdirty
has been set to True.If your source is read-only, you don’t have to implement this method.
-