diff --git a/swh/core/config.py b/swh/core/config.py --- a/swh/core/config.py +++ b/swh/core/config.py @@ -286,69 +286,22 @@ return conf -class SWHConfig: - """Mixin to add configuration parsing abilities to classes +def load_from_envvar(default_config: Dict[str, Any] = {}) -> Dict[str, Any]: + """Load configuration yaml file from the environment variable SWH_CONFIG_FILENAME, + eventually enriched with default configuration key/value from the default_config + dict if provided. - The class should override the class attributes: - - DEFAULT_CONFIG (default configuration to be parsed) - - CONFIG_BASE_FILENAME (the filename of the configuration to be used) + Returns: + Configuration dict - This class defines one classmethod, parse_config_file, which - parses a configuration file using the default config as set in the - class attribute. + Raises: + ValueError if SWH_CONFIG_FILENAME is undefined """ + if "SWH_CONFIG_FILENAME" not in os.environ: + raise ValueError("SWH_CONFIG_FILENAME environment variable is undefined.") - DEFAULT_CONFIG = {} # type: Dict[str, Tuple[str, Any]] - CONFIG_BASE_FILENAME = "" # type: Optional[str] - - @classmethod - def parse_config_file( - cls, - base_filename=None, - config_filename=None, - additional_configs=None, - global_config=True, - ): - """Parse the configuration file associated to the current class. - - By default, parse_config_file will load the configuration - cls.CONFIG_BASE_FILENAME from one of the Software Heritage - configuration directories, in order, unless it is overridden - by base_filename or config_filename (which shortcuts the file - lookup completely). - - Args: - - base_filename (str): overrides the default - cls.CONFIG_BASE_FILENAME - - config_filename (str): sets the file to parse instead of - the defaults set from cls.CONFIG_BASE_FILENAME - - additional_configs: (list of default configuration dicts) - allows to override or extend the configuration set in - cls.DEFAULT_CONFIG. - - global_config (bool): Load the global configuration (default: - True) - """ - - if config_filename: - config_filenames = [config_filename] - elif "SWH_CONFIG_FILENAME" in os.environ: - config_filenames = [os.environ["SWH_CONFIG_FILENAME"]] - else: - if not base_filename: - base_filename = cls.CONFIG_BASE_FILENAME - config_filenames = swh_config_paths(base_filename) - if not additional_configs: - additional_configs = [] - - full_default_config = merge_default_configs( - cls.DEFAULT_CONFIG, *additional_configs - ) - - config = {} - if global_config: - config = load_global_config() - - config.update(priority_read(config_filenames, full_default_config)) - - return config + cfg_path = os.environ["SWH_CONFIG_FILENAME"] + cfg = read_raw_config(config_basepath(cfg_path)) + cfg = merge_configs(default_config, cfg) + return cfg diff --git a/swh/core/tests/test_config.py b/swh/core/tests/test_config.py --- a/swh/core/tests/test_config.py +++ b/swh/core/tests/test_config.py @@ -326,3 +326,39 @@ config.merge_configs({"a": v}, {"a": {}}) with pytest.raises(TypeError): config.merge_configs({"a": {}}, {"a": v}) + + +def test_load_from_envvar_no_environment_var_swh_config_filename_set(): + """Without SWH_CONFIG_FILENAME set, load_from_envvar raises""" + + with pytest.raises(ValueError, match="SWH_CONFIG_FILENAME environment"): + config.load_from_envvar() + + +def test_load_from_envvar_no_default_config(swh_config, monkeypatch): + config_path = str(swh_config) + monkeypatch.setenv("SWH_CONFIG_FILENAME", config_path) + + actual_config = config.load_from_envvar() + + expected_config = config.read(config_path) + assert actual_config == expected_config + + +def test_load_from_envvar_with_default_config(swh_config, monkeypatch): + default_config = { + "number": 666, + "something-cool": ["something", "cool"], + } + + config_path = str(swh_config) + monkeypatch.setenv("SWH_CONFIG_FILENAME", config_path) + + actual_config = config.load_from_envvar(default_config) + + expected_config = config.read(config_path) + expected_config.update( + {"number": 666, "something-cool": ["something", "cool"],} + ) + + assert actual_config == expected_config