2.1.1.1. wc_utils.config package

2.1.1.1.1. Submodules

2.1.1.1.2. wc_utils.config.core module

Read configuration settings from files, environment variables, and function arguments

Author

Jonathan Karr <karr@mssm.edu>

Author

Arthur Goldberg <Arthur.Goldberg@mssm.edu>

Date

2016-10-25

Copyright

2016-2018, Karr Lab

License

MIT

class wc_utils.config.core.AltResourceName(file_in_package)[source]

Bases: object

Get pathname of resource file; a substitute for pkg_resources.resource_filename

Finds paths to resource files in a package that cannot be imported by the standard Python import system.

Usage:

  • `pkg_resources.resource_filename(package, *path_components_of_resource_in_package)

  • `AltResourceName(any_file_in_package).resource_filename(*path_components_of_resource_in_package)

package_root[source]

root directory of a package

Type

str

static get_package_root(file_in_package)[source]

Obtain root directory of a package by following __init__.py files up the file hierarchy

Parameters

file_in_package (str) – pathname of a file in a package

Returns

pathname of root of package

Return type

str

Raises

ValueError – if file_in_package is not the pathname of a file in a package

resource_filename(*args)[source]

Get pathname of resource file; replaces pkg_resources.resource_filename

Parameters

args (list) – pathname components of resource file

Returns

pathname of resource file

Return type

str

class wc_utils.config.core.ConfigManager(paths=None)[source]

Bases: object

Obtain configuration information from ini files, environment variables, and/or function arguments.

Load configuration information from an ini format file, environment variables, and/or function arguments. Validate the configuration against a configuration schema. Return the configuration as a nested dictionary.

Optionally, configuration values can be templates for substitution with string.Template.

paths[source]

paths to configuration files and schema

Type

ConfigPaths

get_config(extra=None, context=None)[source]

Setup configuration from config file(s), environment variables, and/or function arguments.

  1. Setup configuration from default values specified in paths.default.

  2. If paths.user is set, find the first file in it that exists, and override the default configuration with the values specified in the file.

  3. Override configuration with values from environment variables. Environment variables can be set with the following syntax:

    CONFIG.level1.level2...=val
    
  4. Override configuration with additional configuration in extra.

  5. Substitute context into templates

  6. Validate configuration against the schema specified in paths.schema.

Parameters
  • extra (dict, optional) – additional configuration to override

  • context (dict, optional) – context for template substitution

Returns

nested dictionary with the configuration settings loaded from the configuration source(s).

Return type

configobj.ConfigObj

Raises
  • InvalidConfigError – if configuration doesn’t validate against schema

  • ValueError – if no configuration is found

validate(config, value_sources)[source]

Validate configuration

Parameters

config (ConfigObj) – configuration

Raises
  • InvalidConfigError – if configuration doesn’t validate against schema

  • ValueError – if no configuration is found

class wc_utils.config.core.ConfigPaths(default=None, schema=None, user=None)[source]

Bases: object

Paths to configuration files and schema

default[source]

the default config filename

Type

str

schema[source]

the config schema filename

Type

str

user[source]

an iterable of other config files

Type

list

deepcopy()[source]

Returns a deep copy of the object

Returns

deep copy of the object

Return type

ConfigPaths

exception wc_utils.config.core.ExtraValuesError(sources, config)[source]

Bases: Exception

Represents an error due to extra configuration that is not part of the schema

sources[source]

list of sources of configuration values

Type

list of str

config[source]

configuration

Type

configobj.ConfigObj

msg[source]

string representation of message

Type

str

__str__()[source]

Get string representation of error

Returns

string representation of error

Return type

str

exception wc_utils.config.core.InvalidConfigError(sources, config, result)[source]

Bases: Exception

Represents an error due to reading an invalid configuration that doesn’t adhere to the schema

sources[source]

list of sources of configuration values

Type

list of str

config[source]

configuration

Type

configobj.ConfigObj

result[source]

dictionary of configuration errors

Type

dict

msg[source]

string representation of message

Type

str

__str__()[source]

Get string representation of error

Returns

string representation of error

Return type

str

wc_utils.config.core.any_checker(value)[source]

Convert value to its built-in data type if possible

Convert a string value to its built-in data type (integer, float, boolean, str or list of these) if possible

Parameters

value (object) – a value to be converted

Returns

the converted value

Return type

type

Raises

VdtTypeError – if the value cannot be converted

wc_utils.config.core.get_config(extra=None)[source]

Get configuration

Parameters

extra (dict, optional) – additional configuration to override

Returns

nested dictionary with the configuration settings loaded from the configuration source(s).

Return type

configobj.ConfigObj

2.1.1.1.3. Module contents