config.py 1.5 KB

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