law.config#

law config parser implementation.

Classes:

Config([config_file, skip_defaults, ...])

Custom law configuration parser with a few additions on top of the standard python ConfigParser.

Functions:

sections(*args, **kwargs)

Shorthand for Config.sections() of the singleton instance Config.instance().

options(*args, **kwargs)

Shorthand for Config.options() of the singleton instance Config.instance().

keys(*args, **kwargs)

Shorthand for Config.keys() of the singleton instance Config.instance().

items(*args, **kwargs)

Shorthand for Config.items() of the singleton instance Config.instance().

update(*args, **kwargs)

Shorthand for Config.update() of the singleton instance Config.instance().

include(*args, **kwargs)

Shorthand for Config.include() of the singleton instance Config.instance().

get(*args, **kwargs)

Shorthand for Config.get() of the singleton instance Config.instance().

getint(*args, **kwargs)

Shorthand for Config.getint() of the singleton instance Config.instance().

getfloat(*args, **kwargs)

Shorthand for Config.getfloat() of the singleton instance Config.instance().

getboolean(*args, **kwargs)

Shorthand for Config.getboolean() of the singleton instance Config.instance().

get_default(*args, **kwargs)

Shorthand for Config.get_default() of the singleton instance Config.instance().

get_expanded(*args, **kwargs)

Shorthand for Config.get_expanded() of the singleton instance Config.instance().

get_expanded_int(*args, **kwargs)

Shorthand for Config.get_expanded_int() of the singleton instance Config.instance().

get_expanded_float(*args, **kwargs)

Shorthand for Config.get_expanded_float() of the singleton instance Config.instance().

get_expanded_bool(*args, **kwargs)

Shorthand for Config.get_expanded_bool() of the singleton instance Config.instance().

get_expanded_boolean(*args, **kwargs)

Shorthand for Config.get_expanded_boolean() of the singleton instance Config.instance().

is_missing_or_none(*args, **kwargs)

Shorthand for Config.is_missing_or_none() of the singleton instance Config.instance().

find_option(*args, **kwargs)

Shorthand for Config.find_option() of the singleton instance Config.instance().

add_section(*args, **kwargs)

Shorthand for Config.add_section() of the singleton instance Config.instance().

has_section(*args, **kwargs)

Shorthand for Config.has_section() of the singleton instance Config.instance().

remove_section(*args, **kwargs)

Shorthand for Config.remove_section() of the singleton instance Config.instance().

set(*args, **kwargs)

Shorthand for Config.set() of the singleton instance Config.instance().

has_option(*args, **kwargs)

Shorthand for Config.has_option() of the singleton instance Config.instance().

remove_option(*args, **kwargs)

Shorthand for Config.remove_option() of the singleton instance Config.instance().

class Config(config_file='', skip_defaults=False, skip_fallbacks=False, skip_includes=False, skip_env_sync=False, skip_luigi_sync=False)[source]#

Bases: ConfigParser

Custom law configuration parser with a few additions on top of the standard python ConfigParser. Most notably, this class adds config inheritance via update() and include(), a mechanism to synchronize with the luigi configuration parser, option referencing, and environment variable expansion.

When config_file is set, it is loaded during setup. When empty, and skip_fallbacks is False, the default config file locations defined in _config_files are checked. By default, the default configuration _default_config is loaded, which can be prevented by setting skip_defaults to True.

classattribute _instance#

type: Config

Global instance of this class.

classattribute _default_config#

type: dict

Default configuration.

classattribute _config_files#

type: list

List of configuration files that are checked during setup (unless skip_fallbacks is True). When a file exists, the check is stopped. Therefore, the order is important here.

Classes:

Deferred(func)

Wrapper around callables representing deferred options.

Methods:

instance(*args, **kwargs)

Creates an instance of this class with all args and kwargs, saves it in _instance, and returns it.

options(section[, prefix, expand_vars, ...])

Returns all options of a section in a list.

keys()

items(section[, prefix, expand_vars, ...])

Returns a dictionary of key-value pairs for the given section.

set(section, option[, value])

Sets an option of an existing section to value.

update(data[, overwrite, ...])

Updates the currently stored configuration with new data, given as a dictionary.

include(filename, *args, **kwargs)

Updates the current config by that found in filename.

get_default(section, option[, default, ...])

Returns the config value defined by section and option.

get_expanded(*args, **kwargs)

Same as get_default(), but expandvars and expanduser arguments are set to True by default.

get_expanded_int(*args, **kwargs)

Same as get_expanded() with type set to int.

get_expanded_float(*args, **kwargs)

Same as get_expanded() with type set to float.

get_expanded_bool(*args, **kwargs)

Same as get_expanded() with type set to bool.

get_expanded_boolean(*args, **kwargs)

Alias for get_expanded_bool() for backwards compatibility.

is_missing_or_none(section, option)

Returns True if the value defined by section and option is missing or "None" (case-insensitive), and False otherwise.

find_option(section, *options)

Returns the name of the first existing option for a given section.

sync_env()

Synchronizes options defined via environment variables in the format LAW__<section>__<option>.

sync_luigi_config([push, pull])

Synchronizes sections starting with "luigi_" with the luigi configuration parser.

resolve_deferred_defaults()

Traverses all options, checks whether they are deferred callables and if so, resolves and sets them.

class Deferred(func)[source]#

Bases: object

Wrapper around callables representing deferred options.

classmethod instance(*args, **kwargs)[source]#

Creates an instance of this class with all args and kwargs, saves it in _instance, and returns it. When _instance was already set before, no new instance is created.

options(section, prefix=None, expand_vars=True, expand_user=True)[source]#

Returns all options of a section in a list. When prefix is set, only options starting with that prefix are considered. Environment variable expansion is performed on every returned option name, depending on whether expand_vars and expand_user are True.

keys() a set-like object providing a view on D's keys[source]#
items(section, prefix=None, expand_vars=True, expand_user=True, **kwargs)[source]#

Returns a dictionary of key-value pairs for the given section. When prefix is set, only options starting with that prefix are considered. Environment variable expansion is performed on every returned option name and corresponding value, depending on whether expand_vars and expand_user are True. Internally, py:meth:get_expanded is used to perform value expansion and type interpolation, and is passed all kwargs.

set(section, option, value=None)[source]#

Sets an option of an existing section to value. When value is None.

update(data, overwrite=True, overwrite_sections=None, overwrite_options=None)[source]#

Updates the currently stored configuration with new data, given as a dictionary. When overwrite_sections is False, sections in data that are already present in the current config are skipped. When overwrite_options is False, existing options are not overwritten. When None, both overwrite_sections and overwrite_options default to overwrite.

include(filename, *args, **kwargs)[source]#

Updates the current config by that found in filename. All args and kwargs are forwarded to update().

get_default(section, option, default=no_value, type=None, expand_vars=False, expand_user=False, split_csv=False, dereference=True, default_when_none=True)[source]#

Returns the config value defined by section and option. When either the section or the option do not exist and a default value is provided, this value returned instead. When type is set, it must be either “str”, “int”, “float”, or “boolean”. When expand_vars is True, environment variables are expanded. When expand_user is True, user variables are expanded as well. Sequences of values can be identified, split by comma and returned as a list when split_csv is True, which will also trigger brace expansion.

Also, options retrieved by this method are allowed to refer to values of other options within the config, even to those in other sections. The syntax for config references is &[::section]::option. When no section is given, the value refers to an option in the same section. Example:

[my_section]
a: 123
b: &::a              # 123, refers to "a" in the same section

[bar_section]
a: &::my_section::a  # 123, refers to "a" in "my_section"

This behavior is the default and, if desired, can be disabled by setting dereference to False. When the reference is not resolvable, the default value is returned.

When default_when_none is True, a default value is provided, and the option was found but its value is None or "None" (case-insensitive), the default is returned.

get_expanded(*args, **kwargs)[source]#

Same as get_default(), but expandvars and expanduser arguments are set to True by default.

get_expanded_int(*args, **kwargs)[source]#

Same as get_expanded() with type set to int.

get_expanded_float(*args, **kwargs)[source]#

Same as get_expanded() with type set to float.

get_expanded_bool(*args, **kwargs)[source]#

Same as get_expanded() with type set to bool.

get_expanded_boolean(*args, **kwargs)[source]#

Alias for get_expanded_bool() for backwards compatibility.

is_missing_or_none(section, option)[source]#

Returns True if the value defined by section and option is missing or "None" (case-insensitive), and False otherwise. Options without values and those pointing to unresolvable references are considered missing. Example:

[my_section]
a: 123
b: &::a
c: &::not_there
d: None
e
is_missing_or_none("my_section", "a")  # False
is_missing_or_none("my_section", "b")  # False
is_missing_or_none("my_section", "c")  # True
is_missing_or_none("my_section", "d")  # True
is_missing_or_none("my_section", "e")  # True
is_missing_or_none("my_section", "f")  # True
find_option(section, *options)[source]#

Returns the name of the first existing option for a given section. is_missing_or_none() is used to check the existence. When none of the selected options exists, None is returned.

sync_env()[source]#

Synchronizes options defined via environment variables in the format LAW__<section>__<option>. The synchronization only works in case neither the section nor the option contain double underscores (which is anyway discouraged).

sync_luigi_config(push=True, pull=True)[source]#

Synchronizes sections starting with "luigi_" with the luigi configuration parser. First, when push is True, (variable-expanded and dereferenced) options that exist in law but not in luigi are stored as defaults in the luigi config. Then, when pull is True, all luigi-related options in the law config are overwritten with those from luigi. This way, options set via luigi defaults (environment variables, global configuration files, LUIGI_CONFIG_PATH) always have precendence.

resolve_deferred_defaults()[source]#

Traverses all options, checks whether they are deferred callables and if so, resolves and sets them.

sections(*args, **kwargs)#

Shorthand for Config.sections() of the singleton instance Config.instance().

options(*args, **kwargs)#

Shorthand for Config.options() of the singleton instance Config.instance().

keys(*args, **kwargs)#

Shorthand for Config.keys() of the singleton instance Config.instance().

items(*args, **kwargs)#

Shorthand for Config.items() of the singleton instance Config.instance().

update(*args, **kwargs)#

Shorthand for Config.update() of the singleton instance Config.instance().

include(*args, **kwargs)#

Shorthand for Config.include() of the singleton instance Config.instance().

get(*args, **kwargs)#

Shorthand for Config.get() of the singleton instance Config.instance().

getint(*args, **kwargs)#

Shorthand for Config.getint() of the singleton instance Config.instance().

getfloat(*args, **kwargs)#

Shorthand for Config.getfloat() of the singleton instance Config.instance().

getboolean(*args, **kwargs)#

Shorthand for Config.getboolean() of the singleton instance Config.instance().

get_default(*args, **kwargs)#

Shorthand for Config.get_default() of the singleton instance Config.instance().

get_expanded(*args, **kwargs)#

Shorthand for Config.get_expanded() of the singleton instance Config.instance().

get_expanded_int(*args, **kwargs)#

Shorthand for Config.get_expanded_int() of the singleton instance Config.instance().

get_expanded_float(*args, **kwargs)#

Shorthand for Config.get_expanded_float() of the singleton instance Config.instance().

get_expanded_bool(*args, **kwargs)#

Shorthand for Config.get_expanded_bool() of the singleton instance Config.instance().

get_expanded_boolean(*args, **kwargs)#

Shorthand for Config.get_expanded_boolean() of the singleton instance Config.instance().

is_missing_or_none(*args, **kwargs)#

Shorthand for Config.is_missing_or_none() of the singleton instance Config.instance().

find_option(*args, **kwargs)#

Shorthand for Config.find_option() of the singleton instance Config.instance().

add_section(*args, **kwargs)#

Shorthand for Config.add_section() of the singleton instance Config.instance().

has_section(*args, **kwargs)#

Shorthand for Config.has_section() of the singleton instance Config.instance().

remove_section(*args, **kwargs)#

Shorthand for Config.remove_section() of the singleton instance Config.instance().

set(*args, **kwargs)#

Shorthand for Config.set() of the singleton instance Config.instance().

has_option(*args, **kwargs)#

Shorthand for Config.has_option() of the singleton instance Config.instance().

remove_option(*args, **kwargs)#

Shorthand for Config.remove_option() of the singleton instance Config.instance().