File size: 1,341 Bytes
558c90a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
from typing import Any, Optional, Dict, List, Literal
from pydantic import BaseModel
import os, json

class ConfigItem(BaseModel):
    value : Optional[Any] = None
    default : Optional[Any] = None
    type : Optional[str] = None
    description : Optional[str] = None
    
    def __init__(self, **data):
        super().__init__(**data)
        if (self.value is None) and self.default is not None:
            self.value = self.default

def is_config_item(item:Dict[str, Any])->bool:
    """判断是否为配置项"""
    return isinstance(item, dict) and ("value" in item or "default" in item)

def parse_config_dict(input_config:Dict[str, Any], output_config)->Dict[str, Any]:
    
    for key, res in input_config.items():
        if is_config_item(res):
            value = ConfigItem(**res).value
        else:
            if isinstance(res, dict):
                value = parse_config_dict(res, {})
            else:
                value = res
        output_config[key] = value
    return output_config
      
def load_config(config_path:str)->Dict[str, Any]:
    """加载配置文件"""
    assert os.path.exists(config_path), f"配置文件不存在: {config_path}"
    config:Dict[str, Any] = {}
    with open(config_path, 'r', encoding='utf-8') as f:
        config = parse_config_dict(json.load(f), {})
    return config