diff --git a/swh/core/config.py b/swh/core/config.py --- a/swh/core/config.py +++ b/swh/core/config.py @@ -287,68 +287,38 @@ class SWHConfig: - """Mixin to add configuration parsing abilities to classes + """Mixin to add configuration parsing abilities to classes. - 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) + The class should override the class attribute DEFAULT_CONFIG to provide default + key/value entries which need to be set if not defined in the configuration file. - This class defines one classmethod, parse_config_file, which - parses a configuration file using the default config as set in the - class attribute. + This class defines one classmethod, func:`parse_config_file`, which parses a + configuration file, provided through the SWH_CONFIG_FILENAME environment variable. + Eventually enriched with default configuration as per the class attribute + DEFAULT_CONFIG. """ - DEFAULT_CONFIG = {} # type: Dict[str, Tuple[str, Any]] - CONFIG_BASE_FILENAME = "" # type: Optional[str] + DEFAULT_CONFIG: Dict[str, Any] = {} @classmethod - def parse_config_file( - cls, - base_filename=None, - config_filename=None, - additional_configs=None, - global_config=True, - ): + def parse_config_file(cls): """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 = [] + By default, func:`parse_config_file` will load the configuration from the file + referenced by the environment variable SWH_CONFIG_FILENAME. If not set, this + will raise a ValueError requesting the environment variable to be set. - full_default_config = merge_default_configs( - cls.DEFAULT_CONFIG, *additional_configs - ) + Raises:" + ValueError if SWH_CONFIG_FILENAME is undefined - config = {} - if global_config: - config = load_global_config() + """ - config.update(priority_read(config_filenames, full_default_config)) + if "SWH_CONFIG_FILENAME" not in os.environ: + raise ValueError("SWH_CONFIG_FILENAME environment variable is undefined.") - return config + cfg_path = os.environ["SWH_CONFIG_FILENAME"] + cfg = read_raw_config(config_basepath(cfg_path)) + if cls.DEFAULT_CONFIG: + cfg = merge_configs(cls.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,44 @@ config.merge_configs({"a": v}, {"a": {}}) with pytest.raises(TypeError): config.merge_configs({"a": {}}, {"a": v}) + + +def test_swhconfig_mixin_no_swh_config_filename(): + """Without SWH_CONFIG_FILENAME set, SWHConfig.parse_config_file raises""" + + with pytest.raises(ValueError, match="SWH_CONFIG_FILENAME environment"): + config.SWHConfig.parse_config_file() + + +def test_swhconfig_mixin_no_default(swh_config, monkeypatch): + config_path = str(swh_config) + monkeypatch.setenv("SWH_CONFIG_FILENAME", config_path) + + actual_config = config.SWHConfig.parse_config_file() + + expected_config = config.read(config_path) + assert actual_config == expected_config + + +def test_swhconfig_mixin_with_inheritance(swh_config, monkeypatch): + class TestConfig(config.SWHConfig): + """Inherited class with SWHConfig mixin enhanced. + + """ + + DEFAULT_CONFIG = { + "number": 666, + "something-cool": ["something", "cool"], + } + + config_path = str(swh_config) + monkeypatch.setenv("SWH_CONFIG_FILENAME", config_path) + + actual_config = TestConfig.parse_config_file() + + expected_config = config.read(config_path) + expected_config.update( + {"number": 666, "something-cool": ["something", "cool"],} + ) + + assert actual_config == expected_config