| 652 | |
| 653 | |
| 654 | class ConfigLoader: |
| 655 | def __init__(self, default_path: str = None): |
| 656 | if default_path is None: |
| 657 | default_path = Path(__file__).parent / "config.yaml" |
| 658 | self._default_dict = self._load_yaml(default_path) |
| 659 | |
| 660 | @staticmethod |
| 661 | def _load_yaml(path): |
| 662 | with open(path, "r", encoding="utf-8") as f: |
| 663 | return yaml.safe_load(f) or {} |
| 664 | |
| 665 | def _validate_keys(self, user_dict): |
| 666 | unknown_keys = set(user_dict) - set(self._default_dict) |
| 667 | if unknown_keys: |
| 668 | raise ValueError(f"Unknown config keys: {unknown_keys}") |
| 669 | |
| 670 | def load(self, user_opt=None) -> config: |
| 671 | """ |
| 672 | Load the configuration, merging user options with default values. |
| 673 | """ |
| 674 | if user_opt is None: |
| 675 | user_dict = {} |
| 676 | elif isinstance(user_opt, config): |
| 677 | user_dict = vars(user_opt) |
| 678 | elif isinstance(user_opt, dict): |
| 679 | user_dict = user_opt |
| 680 | else: |
| 681 | raise TypeError("user_opt must be dict, config(SimpleNamespace) or None") |
| 682 | |
| 683 | self._validate_keys(user_dict) |
| 684 | merged = {**self._default_dict, **user_dict} |
| 685 | return config(**merged) |
| 686 | |
| 687 | def create_node_mapping(tree): |
| 688 | """Create a flat dict mapping node_id to node for quick lookup.""" |
no outgoing calls
no test coverage detected