pip_services3_commons.config package

Submodules

Module contents

pip_services3_commons.config.__init__

Contains the implementation of the config design pattern. The IConfigurable configurable interface contains just one method - “configure”, which takes ConfigParams as a parameter (extends StringValueMap class). If any object needs to be configurable, we implement this interface and parse the ConfigParams that the method received.

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_commons.config.ConfigParams(values: Any = None)

Bases: pip_services3_commons.data.StringValueMap.StringValueMap

Contains a key-args map with configuration parameters. All values stored as strings and can be serialized as JSON or string forms. When retrieved the values can be automatically converted on read using GetAsXXX methods.

The keys are case-sensitive, so it is recommended to use consistent C-style as: “my_param”

Configuration parameters can be broken into sections and subsections using dot notation as: “section1.subsection1.param1”. Using GetSection method all parameters from specified section can be extracted from a ConfigMap.

The ConfigParams supports serialization from/to plain strings as: “key1=123;key2=ABC;key3=2016-09-16T00:00:00.00Z”

ConfigParams are used to pass configurations to IConfigurable objects. They also serve as a basis for more concrete configurations such as ConnectionParams or CredentialParams (in the Pip.Services components package).

Example:

config = ConfigParams.fromTuples("section1.key1", "AAA",
                                 "section1.key2", 123,
                                 "section2.key1", true)
config.get_as_string("section1.key1")  # Result: AAA
config.get_as_integer("section1.key1") # Result: 0

section1 = config.get_section("section1")
section1.__str__() # Result: key1=AAA;key2=123
add_section(section: str, section_params: pip_services3_commons.config.ConfigParams.ConfigParams)

Adds parameters into this ConfigParams under specified section. Keys for the new parameters are appended with section dot prefix.

Parameters
  • section – name of the section where add new parameters

  • section_params – new parameters to be added.

static from_string(line: str)pip_services3_commons.config.ConfigParams.ConfigParams

Creates a new ConfigParams object filled with key-args pairs serialized as a string.

Parameters

line – a string with serialized key-args pairs as “key1=value1;key2=value2;…”

Returns

a new ConfigParams object.

static from_tuples(*tuples: Any)pip_services3_commons.config.ConfigParams.ConfigParams

Creates a new ConfigParams object filled with provided key-args pairs called tuples. Tuples parameters contain a sequence of key1, value1, key2, value2, … pairs.

Parameters

tuples – the tuples to fill a new ConfigParams object.

Returns

a new ConfigParams object.

static from_value(value: Any)pip_services3_commons.config.ConfigParams.ConfigParams

Creates a new ConfigParams object filled with key-args pairs from specified object.

Parameters

value – an object with key-args pairs used to initialize a new ConfigParams.

Returns

a new ConfigParams object.

get_section(section: str)pip_services3_commons.config.ConfigParams.ConfigParams

Gets parameters from specific section stored in this ConfigMap. The section name is removed from parameter keys.

Parameters

section – name of the section to retrieve configuration parameters from.

Returns

all configuration parameters that belong to the section named ‘section’.

get_section_names() → List[str]

Gets a list with all 1st level section names.

Returns

a list of section names stored in this ConfigMap.

static merge_configs(*configs: pip_services3_commons.config.ConfigParams.ConfigParams)pip_services3_commons.config.ConfigParams.ConfigParams

Merges two or more ConfigParams into one. The following ConfigParams override previously defined parameters.

Parameters

configs – a list of ConfigParams objects to be merged.

Returns

a new ConfigParams object.

override(config_params: pip_services3_commons.config.ConfigParams.ConfigParams)pip_services3_commons.config.ConfigParams.ConfigParams

Overrides parameters with new values from specified ConfigParams and returns a new ConfigParams object.

Parameters

config_params – ConfigMap with parameters to override the current values.

Returns

a new ConfigParams object.

set_defaults(default_config_params: pip_services3_commons.config.ConfigParams.ConfigParams)pip_services3_commons.config.ConfigParams.ConfigParams

Set default values from specified ConfigParams and returns a new ConfigParams object.

Parameters

default_config_params – ConfigMap with default parameter values.

Returns

a new ConfigParams object.

class pip_services3_commons.config.IConfigurable

Bases: abc.ABC

An interface to set configuration parameters to an object.

It can be added to any existing class by implementing a single configure() method.

If you need to emphasis the fact that configure() method can be called multiple times to change object configuration in runtime, use IReconfigurable interface instead.

Example:

class MyClass(IConfigurable):
   _myParam = "default args"

def configure(self, config):
   self._myParam = config.get_as_string_with_default("options.param", myParam)
configure(config: pip_services3_commons.config.ConfigParams.ConfigParams)

Configures object by passing configuration parameters.

Parameters

config – configuration parameters to be set.

class pip_services3_commons.config.IReconfigurable

Bases: pip_services3_commons.config.IConfigurable.IConfigurable

An interface to set configuration parameters to an object. It is similar to IConfigurable interface, but emphasises the fact that configure() method can be called more than once to change object configuration in runtime.

class pip_services3_commons.config.NameResolver

Bases: object

A helper class that allows to extract component name from configuration parameters. The name can be defined in “id”, “name” parameters or inside a component descriptor.

Examples:

config = ConfigParams.fromTuples("descriptor", "myservice:connector:aws:connector1:1.0",
                                 "param1", "ABC",
                                 "param2", 123)

name = NameResolver.resolve(config)
static resolve(config: pip_services3_commons.config.ConfigParams.ConfigParams, default_name: str = None)str

Resolves a component name from configuration parameters. The name can be stored in “id”, “name” fields or inside a component descriptor. If name cannot be determined it returns a defaultName.

Parameters
  • config – configuration parameters that may contain a component name.

  • default_name – (optional) a default component name.

Returns

resolved name or default name if the name cannot be determined.

class pip_services3_commons.config.OptionsResolver

Bases: object

A helper class to parameters from “options” configuration section.

Example:

config = ConfigParams.fromTuples(
  ...
  "options.param1", "ABC",
  "options.param2", 123)

options = OptionsResolver.resolve(config)
static resolve(config: pip_services3_commons.config.ConfigParams.ConfigParams, config_as_default: bool = False)pip_services3_commons.config.ConfigParams.ConfigParams

Resolves an “options” configuration section from component configuration parameters.

Parameters
  • config – configuration parameters

  • config_as_default – (optional) When set true the method returns the entire parameter set when “options” section is not found. Default: false

Returns

configuration parameters from “options” section