File size: 1,246 Bytes
90ca518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 配置加载: 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)