# 配置加载: YAML -> 递归 DictToObject(点号访问) import yaml import argparse class DictToObject: """将字典递归地转换为对象,允许使用点符号访问""" def __init__(self, data): for key, value in data.items(): if isinstance(value, dict): setattr(self, key, DictToObject(value)) else: setattr(self, key, value) def __repr__(self): return f'{self.__dict__}' def load_config(): """ 解析命令行参数以获取配置文件路径, 加载 YAML 文件并返回一个配置对象。 """ parser = argparse.ArgumentParser(description="Load training configuration from a YAML file.") parser.add_argument('--config', type=str, default='./configs/generator.yaml', help='Path to the configuration YAML file.') args = parser.parse_args() print(f"config path :{args.config}") try: with open(args.config, 'r') as f: config_dict = yaml.safe_load(f) print(f"Configuration loaded from {args.config}") except FileNotFoundError: print(f"Error: Configuration file not found at {args.config}") exit(1) return DictToObject(config_dict)