Load a single config file with specified file path (currently support JSON and YAML files). Args: filepath: path of target file to load, supported postfixes: `.json`, `.yml`, `.yaml`. kwargs: other arguments for ``json.load`` or ```yaml.safe_load``, depends
(cls, filepath: PathLike, **kwargs: Any)
| 590 | |
| 591 | @classmethod |
| 592 | def load_config_file(cls, filepath: PathLike, **kwargs: Any) -> dict: |
| 593 | """ |
| 594 | Load a single config file with specified file path (currently support JSON and YAML files). |
| 595 | |
| 596 | Args: |
| 597 | filepath: path of target file to load, supported postfixes: `.json`, `.yml`, `.yaml`. |
| 598 | kwargs: other arguments for ``json.load`` or ```yaml.safe_load``, depends on the file format. |
| 599 | |
| 600 | """ |
| 601 | if not filepath: |
| 602 | return {} |
| 603 | _filepath: str = str(Path(filepath)) |
| 604 | if not re.compile(cls.path_match, re.IGNORECASE).findall(_filepath): |
| 605 | raise ValueError(f'unknown file input: "{filepath}"') |
| 606 | with open(_filepath) as f: |
| 607 | if _filepath.lower().endswith(cls.suffixes[0]): |
| 608 | return json.load(f, object_pairs_hook=check_key_duplicates, **kwargs) # type: ignore[no-any-return] |
| 609 | if _filepath.lower().endswith(cls.suffixes[1:]): |
| 610 | return yaml.load(f, CheckKeyDuplicatesYamlLoader, **kwargs) # type: ignore[no-any-return] |
| 611 | raise ValueError(f"only support JSON or YAML config file so far, got name {_filepath}.") |
| 612 | |
| 613 | @classmethod |
| 614 | def load_config_files(cls, files: PathLike | Sequence[PathLike] | dict, **kwargs: Any) -> dict: |
no test coverage detected