Configurables and configurators¶
In the context of this framework, configurables are entities that designate what properties of the microservice can be dynamically set and configurators are strategies that, on service startup, collects the required properties from the environment.
Currently, the available confugurators are:
gemstone.config.configurator.CommandLineConfigurator
- collects values from the command line argumentsgemstone.config.configurator.JsonFileConfigurator
- collects values from a JSON file
In order to specify configurables for the microservice, you have to provide
set the gemstone.MicroService.configurables
attribute to a list of
Configurable
objects.
Configurators are specified in the gemstone.MicroService.configurators
attribute.
On service startup, each configurator tries to extract the required values from the environment in
the order they are defined.
Configurable¶
-
class
gemstone.config.configurable.
Configurable
(name, *, template=None)[source]¶ Defines a configurable value for the application.
Example (You should not use configurables in this way unless you are writing a custom
Configurator
)c = Configurable("test", template=lambda x: x * 2) c.set_value("10") c.get_final_value() # int("10") * 2 -> 20 c2 = Configurable("list_of_ints", template=lambda x: [int(y) for y in x.split(",")]) c.set_value("1,2,3,4,5") c.get_final_value() # [1,2,3,4,5]
Parameters: - name – The name of the configurable parameter
- template – A callable template to apply over the extracted value
Configurators¶
-
class
gemstone.config.configurator.
BaseConfigurator
[source]¶ Base class for defining configurators. A configurator is a class that, starting from a set of name-configurable pairs, depending on the configurables’ options and the environment, builds a configuration for the application.
-
class
gemstone.config.configurator.
CommandLineConfigurator
[source]¶ Configurator that collects values from command line arguments. For each registered configurable, will attempt to get from command line the value designated by the argument
--name
wherename
is the name of the configurable.Example
For the configurables
- Configurator(“a”)
- Configurator(“b”, type=int)
- Configurator(“c”, type=bool)
the following command line interface will be exposed
usage: service.py [-h] [--a A] [--b B] [--c C] optional arguments: -h, --help show this help message and exit --a A --b B --c C
The
service.py
can be called like thispython service.py --a=1 --b=2 --c=true