API Documentation
This part of the documentation covers the interfaces used to develop with confuse
.
Core
Worry-free YAML configuration files.
- class confuse.core.ConfigView[source]
Bases:
object
A configuration “view” is a query into a program’s configuration data. A view represents a hypothetical location in the configuration tree; to extract the data from the location, a client typically calls the
view.get()
method. The client can access children in the tree (subviews) by subscripting the parent view (i.e.,view[key]
).- add(value)[source]
Set the default value for this configuration view. The specified value is added as the lowest-priority configuration data source.
- all_contents()[source]
Iterates over all subviews from collections at this view from all sources. If the object for this view in any source is not iterable, then a ConfigTypeError is raised. This method is intended to be used when the view indicates a list; this method will concatenate the contents of the list from all sources.
- as_choice(choices)[source]
Get the value from a list of choices. Equivalent to get(Choice(choices)).
Sequences, dictionaries and
Enum
types are supported, seeconfuse.templates.Choice
for more details.
- as_pairs(default_value=None)[source]
Get the value as a sequence of pairs of two strings. Equivalent to get(Pairs(default_value=default_value)).
- as_str()[source]
Get the value as a (Unicode) string. Equivalent to get(unicode) on Python 2 and get(str) on Python 3.
- as_str_expanded()[source]
Get the value as a (Unicode) string, with env vars expanded by os.path.expandvars().
- as_str_seq(split=True)[source]
Get the value as a sequence of strings. Equivalent to get(StrSeq(split=split)).
- first()[source]
Return a (value, source) pair for the first object found for this view. This amounts to the first element returned by resolve. If no values are available, a NotFoundError is raised.
- flatten(redact=False)[source]
Create a hierarchy of OrderedDicts containing the data from this view, recursively reifying all views to get their represented values.
If redact is set, then sensitive values are replaced with the string “REDACTED”.
- get(template=<object object>)[source]
Retrieve the value for this view according to the template.
The template against which the values are checked can be anything convertible to a Template using as_template. This means you can pass in a default integer or string value, for example, or a type to just check that something matches the type you expect.
May raise a ConfigValueError (or its subclass, ConfigTypeError) or a NotFoundError when the configuration doesn’t satisfy the template.
- items()[source]
Iterates over (key, subview) pairs contained in dictionaries from all sources at this view. If the object for this view in any source is not a dict, then a ConfigTypeError is raised.
- keys()[source]
Returns a list containing all the keys available as subviews of the current views. This enumerates all the keys in all dictionaries matching the current view, in contrast to
view.get(dict).keys()
, which gets all the keys for the first dict matching the view. If the object for this view in any source is not a dict, then a ConfigTypeError is raised. The keys are ordered according to how they appear in each source.
- name = None
The name of the view, depicting the path taken through the configuration in Python-like syntax (e.g.,
foo['bar'][42]
).
- property redact
Whether the view contains sensitive information and should be redacted from output.
- resolve()[source]
The core (internal) data retrieval method. Generates (value, source) pairs for each source that contains a value for this view. May raise ConfigTypeError if a type error occurs while traversing a source.
- sequence()[source]
Iterates over the subviews contained in lists from the first source at this view. If the object for this view in the first source is not a list or tuple, then a ConfigTypeError is raised.
- set(value)[source]
Override the value for this configuration view. The specified value is added as the highest-priority configuration data source.
- set_args(namespace, dots=False)[source]
Overlay parsed command-line arguments, generated by a library like argparse or optparse, onto this view’s value.
- Parameters:
namespace (dict or Namespace) – Dictionary or Namespace to overlay this config with. Supports nested Dictionaries and Namespaces.
dots (bool) –
If True, any properties on namespace that contain dots (.) will be broken down into child dictionaries. :Example:
{‘foo.bar’: ‘car’} # Will be turned into {‘foo’: {‘bar’: ‘car’}}
- class confuse.core.Configuration(appname, modname=None, read=True, loader=<class 'confuse.yaml_util.Loader'>)[source]
Bases:
RootView
- _add_default_source()[source]
Add the package’s default configuration settings. This looks for a YAML file located inside the package for the module modname if it was given.
- _add_user_source()[source]
Add the configuration options from the YAML file in the user’s configuration directory (given by config_dir) if it exists.
- config_dir()[source]
Get the path to the user configuration directory. The directory is guaranteed to exist as a postcondition (one may be created if none exist).
If the application’s
...DIR
environment variable is set, it is used as the configuration directory. Otherwise, platform-specific standard configuration locations are searched for aconfig.yaml
file. If no configuration file is found, a fallback path is used.
- dump(full=True, redact=False)[source]
Dump the Configuration object to a YAML file.
The order of the keys is determined from the default configuration file. All keys not in the default configuration will be appended to the end of the file.
- Parameters:
full – Dump settings that don’t differ from the defaults as well
redact – Remove sensitive information (views with the redact flag set) from the output
- read(user=True, defaults=True)[source]
Find and read the files for this configuration and set them as the sources for this configuration. To disable either discovered user configuration files or the in-package defaults, set user or defaults to False.
- reload()[source]
Reload all sources from the file system.
This only affects sources that come from files (i.e., YamlSource objects); other sources, such as dictionaries inserted with add or set, will remain unchanged.
- set_env(prefix=None, sep='__')[source]
Create a configuration overlay at the highest priority from environment variables.
After prefix matching and removal, environment variable names will be converted to lowercase for use as keys within the configuration. If there are nested keys, list-like dicts (ie, {0: ‘a’, 1: ‘b’}) will be converted into corresponding lists (ie, [‘a’, ‘b’]). The values of all environment variables will be parsed as YAML scalars using the self.loader Loader class to ensure type conversion is consistent with YAML file sources. Use the EnvSource class directly to load environment variables using non-default behavior and to enable full YAML parsing of values.
- Parameters:
prefix – The prefix to identify the environment variables to use. Defaults to uppercased self.appname followed by an underscore.
sep – Separator within variable names to define nested keys.
- set_file(filename, base_for_paths=False)[source]
Parses the file as YAML and inserts it into the configuration sources with highest priority.
- Parameters:
filename – Filename of the YAML file to load.
base_for_paths – Indicates whether the directory containing the YAML file will be used as the base directory for resolving relative path values stored in the YAML file. Otherwise, by default, the directory returned by config_dir() will be used as the base.
- class confuse.core.LazyConfig(appname, modname=None)[source]
Bases:
Configuration
A Configuration at reads files on demand when it is first accessed. This is appropriate for using as a global config object at the module level.
- add(value)[source]
Set the default value for this configuration view. The specified value is added as the lowest-priority configuration data source.
- read(user=True, defaults=True)[source]
Find and read the files for this configuration and set them as the sources for this configuration. To disable either discovered user configuration files or the in-package defaults, set user or defaults to False.
- class confuse.core.RootView(sources)[source]
Bases:
ConfigView
The base of a view hierarchy. This view keeps track of the sources that may be accessed by subviews.
- add(obj)[source]
Set the default value for this configuration view. The specified value is added as the lowest-priority configuration data source.
- resolve()[source]
The core (internal) data retrieval method. Generates (value, source) pairs for each source that contains a value for this view. May raise ConfigTypeError if a type error occurs while traversing a source.
- class confuse.core.Subview(parent, key)[source]
Bases:
ConfigView
A subview accessed via a subscript of a parent view.
- add(value)[source]
Set the default value for this configuration view. The specified value is added as the lowest-priority configuration data source.
- resolve()[source]
The core (internal) data retrieval method. Generates (value, source) pairs for each source that contains a value for this view. May raise ConfigTypeError if a type error occurs while traversing a source.
Exceptions
- exception confuse.exceptions.ConfigError[source]
Bases:
Exception
Base class for exceptions raised when querying a configuration.
- exception confuse.exceptions.ConfigReadError(name, reason=None)[source]
Bases:
ConfigError
A configuration source could not be read.
- exception confuse.exceptions.ConfigTemplateError[source]
Bases:
ConfigError
Base class for exceptions raised because of an invalid template.
- exception confuse.exceptions.ConfigTypeError[source]
Bases:
ConfigValueError
The value in the configuration did not match the expected type.
- exception confuse.exceptions.ConfigValueError[source]
Bases:
ConfigError
The value in the configuration is illegal.
- exception confuse.exceptions.NotFoundError[source]
Bases:
ConfigError
A requested value could not be found in the configuration trees.
Sources
- class confuse.sources.ConfigSource(value, filename=None, default=False, base_for_paths=False)[source]
Bases:
dict
A dictionary augmented with metadata about the source of the configuration.
- class confuse.sources.EnvSource(prefix, sep='__', lower=True, handle_lists=True, parse_yaml_docs=False, loader=<class 'confuse.yaml_util.Loader'>)[source]
Bases:
ConfigSource
A configuration data source loaded from environment variables.
- class confuse.sources.YamlSource(filename=None, default=False, base_for_paths=False, optional=False, loader=<class 'confuse.yaml_util.Loader'>)[source]
Bases:
ConfigSource
A configuration data source that reads from a YAML file.
Templates
- class confuse.templates.AttrDict[source]
Bases:
dict
A dict subclass that can be accessed via attributes (dot notation) for convenience.
- class confuse.templates.Choice(choices, default=<object object>)[source]
Bases:
Template
A template that permits values from a sequence of choices.
Sequences, dictionaries and
Enum
types are supported, see__init__()
for usage.- __init__(choices, default=<object object>)[source]
Create a template that validates any of the values from the iterable choices.
If choices is a map, then the corresponding value is emitted. Otherwise, the value itself is emitted.
If choices is a Enum, then the enum entry with the value is emitted.
- class confuse.templates.Filename(default=<object object>, cwd=None, relative_to=None, in_app_dir=False, in_source_dir=False)[source]
Bases:
Template
A template that validates strings as filenames.
Filenames are returned as absolute, tilde-free paths.
Relative paths are relative to the template’s cwd argument when it is specified. Otherwise, if the paths come from a file, they will be relative to the configuration directory (see the config_dir method) by default or to the base directory of the config file if either the source has base_for_paths set to True or the template has in_source_dir set to True. Paths from sources without a file are relative to the current working directory. This helps attain the expected behavior when using command-line options.
- __init__(default=<object object>, cwd=None, relative_to=None, in_app_dir=False, in_source_dir=False)[source]
relative_to is the name of a sibling value that is being validated at the same time.
in_app_dir indicates whether the path should be resolved inside the application’s config directory (even when the setting does not come from a file).
in_source_dir indicates whether the path should be resolved relative to the directory containing the source file, if there is one, taking precedence over the application’s config directory.
- class confuse.templates.Integer(default=<object object>)[source]
Bases:
Template
An integer configuration value template.
- class confuse.templates.MappingTemplate(mapping)[source]
Bases:
Template
A template that uses a dictionary to specify other types for the values for a set of keys and produce a validated AttrDict.
- class confuse.templates.MappingValues(subtemplate)[source]
Bases:
Template
A template used to validate mappings of similar items, based on a given subtemplate applied to the values.
All keys in the mapping are considered valid, but values must pass validation by the subtemplate. Similar to the Sequence template but for mappings.
- class confuse.templates.Number(default=<object object>)[source]
Bases:
Template
A numeric type: either an integer or a floating-point number.
- class confuse.templates.OneOf(allowed, default=<object object>)[source]
Bases:
Template
A template that permits values complying to one of the given templates.
- class confuse.templates.Optional(subtemplate, default=None, allow_missing=True)[source]
Bases:
Template
A template that makes a subtemplate optional.
If the value is present and not null, it must validate against the subtemplate. However, if the value is null or missing, the template will still validate, returning a default value. If allow_missing is False, the template will not allow missing values while still permitting null.
- class confuse.templates.Pairs(default_value=None)[source]
Bases:
StrSeq
A template for ordered key-value pairs.
This can either be given with the same syntax as for StrSeq (i.e. without values), or as a list of strings and/or single-element mappings such as:
- key: value - [key, value] - key
The result is a list of two-element tuples. If no value is provided, the default_value will be returned as the second element.
- class confuse.templates.Path(default=<object object>, cwd=None, relative_to=None, in_app_dir=False, in_source_dir=False)[source]
Bases:
Filename
A template that validates strings as pathlib.Path objects.
Filenames are parsed equivalent to the Filename template and then converted to pathlib.Path objects.
- confuse.templates.REQUIRED = <object object>
A sentinel indicating that there is no default value and an exception should be raised when the value is missing.
- class confuse.templates.Sequence(subtemplate)[source]
Bases:
Template
A template used to validate lists of similar items, based on a given subtemplate.
- class confuse.templates.StrSeq(split=True, default=<object object>)[source]
Bases:
Template
A template for values that are lists of strings.
Validates both actual YAML string lists and single strings. Strings can optionally be split on whitespace.
- class confuse.templates.String(default=<object object>, pattern=None, expand_vars=False)[source]
Bases:
Template
A string configuration value template.
- class confuse.templates.Template(default=<object object>)[source]
Bases:
object
A value template for configuration fields.
The template works like a type and instructs Confuse about how to interpret a deserialized YAML value. This includes type conversions, providing a default value, and validating for errors. For example, a filepath type might expand tildes and check that the file exists.
- __init__(default=<object object>)[source]
Create a template with a given default value.
If default is the sentinel REQUIRED (as it is by default), then an error will be raised when a value is missing. Otherwise, missing values will instead return default.
- convert(value, view)[source]
Convert the YAML-deserialized value to a value of the desired type.
Subclasses should override this to provide useful conversions. May raise a ConfigValueError when the configuration is wrong.
- fail(message, view, type_error=False)[source]
Raise an exception indicating that a value cannot be accepted.
type_error indicates whether the error is due to a type mismatch rather than a malformed value. In this case, a more specific exception is raised.
Utility
- confuse.util.build_dict(obj, sep='', keep_none=False)[source]
Recursively builds a dictionary from an argparse.Namespace, optparse.Values, or dict object.
Additionally, if sep is a non-empty string, the keys will be split by sep and expanded into a nested dict. Keys with a None value are dropped by default to avoid unsetting options but can be kept by setting keep_none to True.
- Parameters:
obj (argparse.Namespace or optparse.Values or dict or *) – Namespace, Values, or dict to iterate over. Other values will simply be returned.
sep (str) – Separator to use for splitting properties/keys of obj for expansion into nested dictionaries.
keep_none (bool) – Whether to keep keys whose value is None.
- Returns:
A new dictionary or the value passed if obj was not a dict, Namespace, or Values.
- Return type:
dict or *
- confuse.util.config_dirs()[source]
Return a platform-specific list of candidates for user configuration directories on the system.
The candidates are in order of priority, from highest to lowest. The last element is the “fallback” location to be used when no higher-priority config file exists.
- confuse.util.find_package_path(name)[source]
Returns the path to the package containing the named module or None if the path could not be identified (e.g., if
name == "__main__"
).
- confuse.util.iter_first(sequence)[source]
Get the first element from an iterable or raise a ValueError if the iterator generates no values.
YAML Utility
- class confuse.yaml_util.Dumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)[source]
Bases:
SafeDumper
A PyYAML Dumper that represents OrderedDicts as ordinary mappings (in order, of course).
- class confuse.yaml_util.Loader(stream)[source]
Bases:
SafeLoader
A customized YAML loader. This loader deviates from the official YAML spec in a few convenient ways:
All strings as are Unicode objects.
All maps are OrderedDicts.
Strings can begin with % without quotation.
- confuse.yaml_util.load_yaml(filename, loader=<class 'confuse.yaml_util.Loader'>)[source]
Read a YAML document from a file. If the file cannot be read or parsed, a ConfigReadError is raised. loader is the PyYAML Loader class to use to parse the YAML. By default, this is Confuse’s own Loader class, which is like SafeLoader with extra constructors.
- confuse.yaml_util.load_yaml_string(yaml_string, name, loader=<class 'confuse.yaml_util.Loader'>)[source]
Read a YAML document from a string. If the string cannot be parsed, a ConfigReadError is raised. yaml_string is a string to be parsed as a YAML document. name is the name to use in error messages. loader is the PyYAML Loader class to use to parse the YAML. By default, this is Confuse’s own Loader class, which is like SafeLoader with extra constructors.
- confuse.yaml_util.parse_as_scalar(value, loader=<class 'confuse.yaml_util.Loader'>)[source]
Parse a value as if it were a YAML scalar to perform type conversion that is consistent with YAML documents. value should be a string. Non-string inputs or strings that raise YAML errors will be returned unchanged. Loader is the PyYAML Loader class to use for parsing, defaulting to Confuse’s own Loader class.
- Examples with the default Loader:
‘1’ will return 1 as an integer
‘1.0’ will return 1 as a float
‘true’ will return True
The empty string ‘’ will return None