The ConfigLoader
class is the main class of the onion_config
package. An instance of ConfigLoader
represents a configuration loader that can load and validate configuration data from environment variables and config files.
ConfigLoader
instances have the following properties:
- config: Main config object (based on
config_schema
) for the project. It is an instance of aBaseConfig
,BaseSettings
, orBaseModel
class, which holds the loaded and validated configuration data. Defaults toBaseConfig
. - config_schema: Main config schema class to load and validate configs. Defaults to
BaseConfig
. - config_data: Loaded data from config files as a dictionary. Defaults to
{}
. - configs_dirs: Main configs directories as list to load all config files. Defaults to
['./configs']
. - extra_dir: Extra configs directory to load extra config files. Defaults to
None
, but will use theONION_CONFIG_EXTRA_DIR
environment variable if set. - env_file_paths: Dotenv file paths as list to load. Defaults to
['.env']
. - required_envs: Required environment variables to check. Defaults to
[]
. - pre_load_hook: Custom pre-load method, this method will be executed before validating
config
. Defaults tolambda config_data: config_data
. - quiet: Quiet mode to suppress all warning logs. Defaults to
True
.
ConfigLoader
instances have the following methods:
- load(): 0. Load and validate every config into
config
. - _load_dotenv_files(): 1. Load all dotenv files from
env_file_paths
into environment variables. - _load_dotenv_file(): 1.1. Load each dotenv file into environment variables.
- _check_required_envs(): 2. Check if required environment variables exist.
- _load_configs_dirs(): 3. Load all config files from
configs_dirs
intoconfig_data
. - _load_configs_dir(): 3.1. Load config files from each config directory into
config_data
. - _load_yaml_file(): 3.1.a. Load each YAML config file into
config_data
. - _load_json_file(): 3.1.b. Load each JSON config file into
config_data
. - _load_extra_dir(): 4. Load extra config files from
extra_dir
intoconfig_data
.
- Load all dotenv files from
env_file_paths
into environment variables. - Check if required environment variables exist or not.
- Load all config files from
configs_dirs
intoconfig_data
. - Load extra config files from
extra_dir
intoconfig_data
. - Execute
pre_load_hook
method to modifyconfig_data
. - Init
config_schema
withconfig_data
into finalconfig
.
The BaseConfig
class is a subclass of pydantic_settings.BaseSettings
that is used as the default schema for validating configuration data in onion_config
. It allows for arbitrary types and additional properties (extra).
It also overrides the customise_sources
method to specify the order in which the configuration sources are checked. In onion_config
, the dotenv files are checked first, environment variables are second, followed by the initial settings and then the file secret settings.