zhaoqingang
2024-11-19 9c275b214f9619a64cd2998596ce696610185eb4
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
42
43
44
45
46
47
48
49
50
51
52
from pathlib import Path
import yaml
 
 
class Settings:
    secret_key: str = ''
    sgb_base_url: str = ''
    sgb_websocket_url: str = ''
    fwr_base_url: str = ''
    database_url: str = ''
    sgb_db_url: str = ''
    fwr_db_url: str = ''
    fetch_sgb_agent: str = ''
    fetch_fwr_agent: str = ''
    PUBLIC_KEY: str
    PRIVATE_KEY: str
    PASSWORD_KEY: str
    def __init__(self, **kwargs):
        # Check if all required fields are provided and set them
        for field in self.__annotations__.keys():
            if field not in kwargs:
                raise ValueError(f"Missing setting: {field}")
            setattr(self, field, kwargs[field])
 
    def to_dict(self):
        """Return the settings as a dictionary."""
        return {k: getattr(self, k) for k in self.__annotations__.keys()}
 
    def __repr__(self):
        """Return a string representation of the settings."""
        return f"Settings({self.to_dict()})"
 
 
def load_yaml(file_path: Path) -> dict:
    with file_path.open('r', encoding="utf-8") as fr:
        try:
            data = yaml.safe_load(fr)
            return data
        except yaml.YAMLError as e:
            print(f"Error loading YAML file {file_path}: {e}")
            return {}
 
 
# Use pathlib to handle file paths
config_yaml_path = Path(__file__).parent / 'config.yaml'
settings_data = load_yaml(config_yaml_path)
 
# Initialize settings object
settings = Settings(**settings_data)
 
# Print the loaded settings
print(f"Loaded settings: {settings}")