Skip to content

4167 Enhance bundle parser when loading multiple config files #4192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions monai/bundle/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,20 +338,24 @@ def load_config_file(cls, filepath: PathLike, **kwargs):
raise ValueError(f"only support JSON or YAML config file so far, got name {_filepath}.")

@classmethod
def load_config_files(cls, files: Union[PathLike, Sequence[PathLike], dict], **kwargs) -> dict:
def load_config_files(cls, files: Union[PathLike, Sequence[PathLike], dict], **kwargs) -> Dict:
"""
Load config files into a single config dict.
The latter config file in the list will override or add the former config file.
``"#"`` in the config keys are interpreted as special characters to go one level
further into the nested structures.

Args:
files: path of target files to load, supported postfixes: `.json`, `.yml`, `.yaml`.
kwargs: other arguments for ``json.load`` or ```yaml.safe_load``, depends on the file format.
"""
if isinstance(files, dict): # already a config dict
return files
content = {}
parser = ConfigParser(config={})
for i in ensure_tuple(files):
content.update(cls.load_config_file(i, **kwargs))
return content
for k, v in (cls.load_config_file(i, **kwargs)).items():
parser[k] = v
return parser.get() # type: ignore

@classmethod
def export_config_file(cls, config: Dict, filepath: PathLike, fmt="json", **kwargs):
Expand Down