config_resolver package

Submodules

config_resolver.core module

config_resolver provides a Config class, which looks up common locations for config files and loads them if found. It provides a framework independed way of handling configuration files. Additional care has been taken to allow the end-user of the application to override this lookup process.

class config_resolver.core.Config(group_name, app_name, search_path=None, filename='app.ini', require_load=False, version=None, **kwargs)[source]

Bases: config_resolver.core.ConfigResolverBase

Parameters:
  • group_name – an application group (f. ex.: your company name)
  • app_name – an application identifier (f.ex.: the application module name)
  • search_path – if specified, set the config search path to the given value. The path can use OS specific separators (f.ex.: : on posix, ; on windows) to specify multiple folders. These folders will be searched in the specified order. The config files will be loaded incrementally. This means that the each subsequent config file will extend/override existing values. This means that the last file will take precedence.
  • filename – if specified, this can be used to override the configuration filename.
  • require_load – If this is set to True, creation of the config instance will raise an OSError if not a single file could be loaded.
  • version – If specified (f.ex.: version='2.0'), this will create a versioned config instance. A versioned instance will only load config files which have the same major version. On mismatch an error is logged and the file is skipped. If the minor version differs the file will be loaded, but issue a warning log. Version numbers are parsed using distutils.version.StrictVersion
active_path
check_file(filename)[source]

Check if filename can be read. Will return boolean which is True if the file can be read, False otherwise.

get(section, option, **kwargs)[source]

Overrides configparser.ConfigParser.get().

In addition to section and option, this call takes an optional default value. This behaviour works in addition to the configparser.ConfigParser default mechanism. Note that a default value from ConfigParser takes precedence.

The reason this additional functionality is added, is because the defaults of configparser.ConfigParser are not dependent on sections. If you specify a default for the option test, then this value will be returned for both section1.test and for section2.test. Using the default on the get call gives you more fine-grained control over this.

Also note, that if a default value was used, it will be logged with level logging.DEBUG.

Parameters:
get_xdg_dirs()[source]

Returns a list of paths specified by the XDG_CONFIG_DIRS environment variable or the appropriate default.

The list is sorted by precedence, with the most important item coming last (required by the existing config_resolver logic).

get_xdg_home()[source]

Returns the value specified in the XDG_CONFIG_HOME environment variable or the appropriate default.

load(reload=False, require_load=False)[source]

Searches for an appropriate config file. If found, loads the file into the current instance. This method can also be used to reload a configuration. Note that you may want to set reload to True to clear the configuration before loading in that case. Without doing that, values will remain available even if they have been removed from the config files.

Parameters:
  • reload – if set to True, the existing values are cleared before reloading.
  • require_load – If set to True this will raise a IOError if no config file has been found to load.
loaded_files
class config_resolver.core.ConfigID(group, app)

Bases: tuple

app

Alias for field number 1

group

Alias for field number 0

class config_resolver.core.ConfigResolverBase(defaults=None, dict_type=<class 'collections.OrderedDict'>, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=<object object>, converters=<object object>)[source]

Bases: configparser.ConfigParser

A default “base” object simplifying Python 2 and Python 3 compatibility.

class config_resolver.core.LookupMetadata(active_path, loaded_files, config_id, prefix_filter)

Bases: tuple

active_path

Alias for field number 0

config_id

Alias for field number 2

loaded_files

Alias for field number 1

prefix_filter

Alias for field number 3

class config_resolver.core.LookupResult(config, meta)

Bases: tuple

config

Alias for field number 0

meta

Alias for field number 1

class config_resolver.core.SecuredConfig(group_name, app_name, search_path=None, filename='app.ini', require_load=False, version=None, **kwargs)[source]

Bases: config_resolver.core.Config

A subclass of Config which will refuse to load config files which are read able by other users than the owner.

check_file(filename)[source]

Overrides Config.check_file()

config_resolver.core.build_call_str(prefix, args, kwargs)[source]

Build a callable Python string for a function call. The output will be combined similar to this template:

<prefix>(<args>, <kwargs>)

Example:

>>> build_call_str('foo', (1, 2), {'a': '10'})
"foo(1, 2, a='10')"
config_resolver.core.get_config(app_name, group_name='', filename='', lookup_options=None, handler=None)[source]
config_resolver.core.get_new_call(group_name, app_name, search_path, filename, require_load, version, secure)[source]

Build a call to use the new get_config function from args passed to Config.__init__.

config_resolver.core.get_warn_location()[source]

Gets the location where the function was called or “<unknown>” if it was unable to get the location.

If this returns an empty string, we assume the warning can be ignored.

config_resolver.exc module

exception config_resolver.exc.IncompatibleVersion[source]

Bases: Exception

This exception is raised if a config file is loaded which has a different major version number than expected by the application.

exception config_resolver.exc.NoVersionError[source]

Bases: Exception

This exception is raised if the application expects a version number to be present in the config file but does not find one.

config_resolver.util module

class config_resolver.util.PrefixFilter(prefix, separator=' ')[source]

Bases: logging.Filter

A logging filter which prefixes each message with a given text.

Parameters:
  • prefix – The log prefix.
  • separator – A string to put between the prefix and the original log message.
filter(record)[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

Module contents