tlcconfig.option_loader#

This module contains the OptionLoader class, which is used to load and store the 3LC options.

Module Contents#

Classes#

Class

Description

ConfigSource

The source of an option’s value.

OptionValue

Holds the value of a 3LC option

OptionLoader

Load the 3LC options.

Functions#

Function

Description

get_default_config_file

Return the default config file.

get_default_config_file_locations

Return the default config file location.

API#

tlcconfig.option_loader.get_default_config_file() str#

Return the default config file.

tlcconfig.option_loader.get_default_config_file_locations() list[str]#

Return the default config file location.

class tlcconfig.option_loader.ConfigSource#

Bases: enum.Enum

The source of an option’s value.

This enum is used during option resolution to track and store from which source the option was set.

DEFAULT = 0#

The option was set with the default value.

CONFIG_FILE = 1#

The option was loaded from a configuration file.

ENVIRONMENT = 2#

The option was loaded from an environment variable.

COMMAND_LINE = 3#

The option was set on the command line.

API = 4#

The option was set using the API.

class tlcconfig.option_loader.OptionValue(cls: type[tlcconfig.options.OPTION])#

Holds the value of a 3LC option

class tlcconfig.option_loader.OptionLoader(config_file: str | None = None, initialize: bool = True)#

Load the 3LC options.

Note

Even if config_file is passed in, values from environment variables will override values from the config file.
Parameters:
  • config_file – The config file to use. If None, the default config files will be searched for and used if they exist.

  • initialize – If True, the options will be read from the config file and environment variables. This is the default.

Raises:

FileNotFoundError – If config_file is set and does not exists.

property config_file: str | None#
get_config() dict[type[tlcconfig.options.OPTION], Any]#

Return the current configuration as a dictionary.

get_value(option: type[tlcconfig.options.OPTION]) Any#

Return the value of an option.

get_config_source(option: type[tlcconfig.options.OPTION]) tlcconfig.option_loader.ConfigSource#

Return the value of an option.

set_value(option: type[tlcconfig.options.OPTION], value: Any, config_source: tlcconfig.option_loader.ConfigSource = ConfigSource.API) None#

Set the value of an option.

to_yaml() str#

Return the current configuration as a YAML string.

write_to_yaml_file(filename: str, overwrite: bool = False) None#

Write the current configuration to a YAML file.

Parameters:
  • filename – The name of the file to write to, if not set the default config file will be used as returned by get_default_config_file.

  • overwrite – If True, the file will be overwritten if it exists. If False, the file will not be written.

update_from_yaml_file(config_file: str) None#

Update the configuration from a YAML file.

update_from_environment() None#

Update the configuration from the environment.

static set_instance(option_loader: tlcconfig.option_loader.OptionLoader, write_config_file: bool = True) None#

Set the instance of the OptionLoader to be used by the rest of the package.

Parameters:
  • option_loader – The OptionLoader instance to be used by the rest of the application.

  • write_config_file – If True, the default config file will be written if no config file was set.

static instance() tlcconfig.option_loader.OptionLoader#

Return the instance of the OptionLoader to be used by the rest of the application.

If the instance has not been set, a new instance will be created using the defaults.

Returns:

The OptionLoader instance to be used by the rest of the application.

Raises:

ValueError – If no valid OptionLoader can be created.

static resolve_effective_config_file(path: str | None = None, env: dict[str, str] | None = None) str | None#

Return the effective config file path.

This method will search for a valid config file in the following order:

  • Path specified by the path argument

  • Path specified by the TLC_CONFIG_FILE environment variable

  • Default config file locations

Parameters:
  • path – The path to the config file to use. If None, the default config files will be searched for.

  • env – Dict for environment variables. If None, os.environ will be used.

Returns:

A string with a path to a config file. Returns None if no config file can be found.

Raises:

FileNotFoundError – If the path argument or TLC_CONFIG_FILE environment variable is specified but the file does not exist.