config.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. from pathlib import Path
  3. import yaml
  4. class Settings:
  5. secret_key: str = ''
  6. bisheng_base_url: str = ''
  7. ragflow_base_url: str = ''
  8. database_url: str = ''
  9. PUBLIC_KEY: str
  10. PRIVATE_KEY: str
  11. def __init__(self, **kwargs):
  12. # Check if all required fields are provided and set them
  13. for field in self.__annotations__.keys():
  14. if field not in kwargs:
  15. raise ValueError(f"Missing setting: {field}")
  16. setattr(self, field, kwargs[field])
  17. def to_dict(self):
  18. """Return the settings as a dictionary."""
  19. return {k: getattr(self, k) for k in self.__annotations__.keys()}
  20. def __repr__(self):
  21. """Return a string representation of the settings."""
  22. return f"Settings({self.to_dict()})"
  23. def load_yaml(file_path: Path) -> dict:
  24. with file_path.open('r', encoding="utf-8") as fr:
  25. try:
  26. data = yaml.safe_load(fr)
  27. return data
  28. except yaml.YAMLError as e:
  29. print(f"Error loading YAML file {file_path}: {e}")
  30. return {}
  31. # Use pathlib to handle file paths
  32. config_yaml_path = Path(__file__).parent / 'config.yaml'
  33. settings_data = load_yaml(config_yaml_path)
  34. # Initialize settings object
  35. settings = Settings(**settings_data)
  36. # Print the loaded settings
  37. print(f"Loaded settings: {settings}")