Skip to content

api

config

twonms.config module to manage application configuration

The module uses OmegaConf to combine and read configuration items.

Configuration parameters can be provided in different ways and will be processessed in a certain order where each step may override the parameters of the previous step. In the end all parameters of all steps will be combined and accessible in the Configure() class

  1. no parameters provided - use application default parameters if configured (via REQUIRED_CONFIG_PARAMETERS parameter)
  2. from envvars (system env vars or .env file)
  3. from *.yml config files
  4. from cli

Typical usage example:

from twonms.config import Config

config = Config.create()

debug = config.debug
my_other_param = config.my_other_param

Config

Main class of the module

Manages application configuration parameters and this actually overrides from OmegaConf so the same rules apply, check the OmegaConf docs for more details.

This class implements some default inheritance to take parameters from different sources (envvars, cli, conf files)

Attributes:

Name Type Description
likes_spam

A boolean indicating if we like SPAM or not.

eggs

An integer count of the eggs we have laid.

cli_config: OmegaConf property readonly

Property for the CLI variables config

Returns the OmegaConf object

conf property readonly

Property to return the yaml config filename

If the config file is defined then return it Otherwise check if the file for the environment exists If not then return None

env property readonly

Property for the environment variable

Returns either a predefined environment (DEV, PROD, DEBUG, TEST) or None

environment_config: OmegaConf property readonly

Property for the environment variables config

Returns the OmegaConf object

file_config: OmegaConf property readonly

Property that returs the OmegaConf based on the yaml configuration file

path property readonly

Property for path variable

Returns the path if the folder exists, otherwise returns None

required_config: OmegaConf property readonly

Property for the required config

Returns the OmegaConf

create(path='./conf', conf=None, env='PROD', required_config={}, allowed_envvars=['environment', 'debug']) staticmethod

Creates the Config object

This method should be used as the constructor

Attributes:

Name Type Description
path

(optional) the path name where yaml config files can be found, default=./conf

conf

(optional) the filename including extension that should be loaded if blank then the environmont default config file will be loaded if the "env" variable is given and the file exists default=None

env

(optional) if the application is using the standard config file naming conventions then the env variable can be specified and the correct config file is automatically loaded default=PROD

allowed_envvars

(optional) a list of allowed environment variables that can be loaded via system env vars

required_config

(optional) dictionary or string with all the required parameters, meaning that these parameters will always be present in the Config object If this is a string then it should be in yaml format

Example usage:

REQUIRED_CONFIG_PARAMETERS = """
environment: PRODUCTION
debug: false
"""

Config.create()
    creates a Config object with default settings, if "./conf/production.yaml"
    exists then this will be loaded

Config.create(path="./configs",
              conf="myconfig.yaml",
)
    creates a Config object and load "./configs/myconfig.yaml"

Config.create(path="./configs",
              env="DEV",
)
    creates a Config object and load "./configs/development.yaml"

Config.create(required_config=REQUIRED_CONFIG_PARAMETERS,
)
    creates a Config object loads a set of predefined required config parameters
Source code in config/config.py
@staticmethod
def create(
    path: str = "./conf",
    conf: str = None,
    env: str = "PROD",
    required_config: Union[str, dict] = {},
    allowed_envvars=DEFAULT_ALLOWED_ENVVARS,
) -> OmegaConf:
    """Creates the Config object

    This method should be used as the constructor

    Attributes:

        path: (optional) the path name where yaml config files can be found,
              default=./conf

        conf: (optional) the filename including extension that should be loaded
              if blank then the environmont default config file will be loaded
              if the "env" variable is given and the file exists
              default=None

        env: (optional) if the application is using the standard config file
             naming conventions then the env variable can be specified and the
             correct config file is automatically loaded
             default=PROD

        allowed_envvars: (optional) a list of allowed environment variables that can be loaded
                         via system env vars

        required_config: (optional) dictionary or string with all the required parameters,
                         meaning that these parameters will always be present in
                         the Config object
                         If this is a string then it should be in yaml format

    Example usage:

        REQUIRED_CONFIG_PARAMETERS = \"\"\"
        environment: PRODUCTION
        debug: false
        \"\"\"

        Config.create()
            creates a Config object with default settings, if "./conf/production.yaml"
            exists then this will be loaded

        Config.create(path="./configs",
                      conf="myconfig.yaml",
        )
            creates a Config object and load "./configs/myconfig.yaml"

        Config.create(path="./configs",
                      env="DEV",
        )
            creates a Config object and load "./configs/development.yaml"

        Config.create(required_config=REQUIRED_CONFIG_PARAMETERS,
        )
            creates a Config object loads a set of predefined required config parameters

    """
    obj = Config(path, conf, env, required_config, allowed_envvars)
    conf = obj.__create()
    return conf