Skip to content

Commit

Permalink
Added ac_context parameter to config.common (#2109)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Rodriguez Gutierrez <miguel7r@hotmail.com>

Signed-off-by: Miguel Rodriguez Gutierrez <miguel7r@hotmail.com>
Co-authored-by: Ivan Danov <idanov@users.noreply.github.com>
  • Loading branch information
MigQ2 and idanov authored Dec 20, 2022
1 parent 7a56ce4 commit 9e4e427
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
## Bug fixes and other changes
* Fix bug where `micropkg` manifest section in `pyproject.toml` isn't recognised as allowed configuration.
* Added `omegaconf` syntax as option for `--params`. Keys and values can now be separated by colons or equals signs.
* Added anyconfig's `ac_context` parameter to `kedro.config.commons` module functions for more flexible `ConfigLoader` customizations.

## Breaking changes to the API

Expand Down
26 changes: 21 additions & 5 deletions kedro/config/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def _get_config_from_patterns(
conf_paths: Iterable[str],
patterns: Iterable[str] = None,
ac_template: bool = False,
ac_context: Dict[str, Any] = None,
) -> Dict[str, Any]:
"""Recursively scan for configuration files, load and merge them, and
return them in the form of a config dictionary.
Expand All @@ -39,6 +40,8 @@ def _get_config_from_patterns(
ac_template: Boolean flag to indicate whether to use the `ac_template`
argument of the ``anyconfig.load`` method. Used in the context of
`_load_config_file` function.
ac_context: anyconfig context to pass to ``anyconfig.load`` method.
Used in the context of `_load_config_file` function.
Raises:
ValueError: If 2 or more configuration files inside the same
Expand Down Expand Up @@ -75,7 +78,9 @@ def _get_config_from_patterns(
Path(conf_path), patterns, processed_files, _config_logger
)
new_conf = _load_configs(
config_filepaths=config_filepaths, ac_template=ac_template
config_filepaths=config_filepaths,
ac_template=ac_template,
ac_context=ac_context,
)

common_keys = config.keys() & new_conf.keys()
Expand All @@ -98,13 +103,16 @@ def _get_config_from_patterns(
return config


def _load_config_file(config_file: Path, ac_template: bool = False) -> Dict[str, Any]:
def _load_config_file(
config_file: Path, ac_template: bool = False, ac_context: Dict[str, Any] = None
) -> Dict[str, Any]:
"""Load an individual config file using `anyconfig` as a backend.
Args:
config_file: Path to a config file to process.
ac_template: Boolean flag to indicate whether to use the `ac_template`
argument of the ``anyconfig.load`` method.
ac_context: anyconfig context to pass to ``anyconfig.load`` method.
Raises:
BadConfigException: If configuration is poorly formatted and
Expand All @@ -123,7 +131,9 @@ def _load_config_file(config_file: Path, ac_template: bool = False) -> Dict[str,
_config_logger.debug("Loading config file: '%s'", config_file)
return {
k: v
for k, v in anyconfig.load(yml, ac_template=ac_template).items()
for k, v in anyconfig.load(
yml, ac_template=ac_template, ac_context=ac_context
).items()
if not k.startswith("_")
}
except AttributeError as exc:
Expand All @@ -138,7 +148,9 @@ def _load_config_file(config_file: Path, ac_template: bool = False) -> Dict[str,
) from exc


def _load_configs(config_filepaths: List[Path], ac_template: bool) -> Dict[str, Any]:
def _load_configs(
config_filepaths: List[Path], ac_template: bool, ac_context: Dict[str, Any] = None
) -> Dict[str, Any]:
"""Recursively load all configuration files, which satisfy
a given list of glob patterns from a specific path.
Expand All @@ -147,6 +159,8 @@ def _load_configs(config_filepaths: List[Path], ac_template: bool) -> Dict[str,
ac_template: Boolean flag to indicate whether to use the `ac_template`
argument of the ``anyconfig.load`` method. Used in the context of
`_load_config_file` function.
ac_context: anyconfig context to pass to ``anyconfig.load`` method.
Used in the context of `_load_config_file` function.
Raises:
ValueError: If 2 or more configuration files contain the same key(s).
Expand All @@ -162,7 +176,9 @@ def _load_configs(config_filepaths: List[Path], ac_template: bool) -> Dict[str,
seen_file_to_keys = {} # type: Dict[Path, AbstractSet[str]]

for config_filepath in config_filepaths:
single_config = _load_config_file(config_filepath, ac_template=ac_template)
single_config = _load_config_file(
config_filepath, ac_template=ac_template, ac_context=ac_context
)
_check_duplicate_keys(seen_file_to_keys, config_filepath, single_config)
seen_file_to_keys[config_filepath] = single_config.keys()
aggregate_config.update(single_config)
Expand Down

0 comments on commit 9e4e427

Please sign in to comment.