config.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. import yaml
  3. from pathlib import Path
  4. from pydantic import BaseModel, Field
  5. from typing import List, Dict, Union,Optional,Any
  6. from utils.pydantic_auto_field import AutoLoadModel
  7. from dotenv import load_dotenv
  8. load_dotenv()
  9. class Config(BaseModel):
  10. storage: str = "local"
  11. s3_access_key: Optional[str] = os.environ.get("S3_ACCESS_KEY", 'bh9LbfsPHRJgQ44wXIlv')
  12. s3_secret_key: Optional[str] = os.environ.get("S3_SECRET_KEY", 'N744RZ60T1b4zlcWG2MROCzjEE2mPTdNQCc7Pk3M')
  13. s3_endpoint: Optional[str] = os.environ.get("S3_ENDPOINT", 'http://vs1.lan:9002')
  14. chrome_config_ini: Optional[str] = r'G:\code\amazone\copywriting_production\config\dp_conf\9321.ini'
  15. redis_url: Optional[str] = os.environ.get("REDIS_URL", 'redis://localhost:6379/0')
  16. def save(self, config_path: Path = None):
  17. config_path = config_path or get_config_path()
  18. with open(config_path, "w", encoding="utf-8") as file:
  19. yaml.dump(self.model_dump(), file)
  20. return self
  21. def get_config_path():
  22. return os.environ.get('CONFIG_PATH',CONFIG_DIR / "config.yaml")
  23. def read_config(config_path: Path):
  24. if isinstance(config_path, str):
  25. config_path = Path(config_path)
  26. if not config_path.exists():
  27. config = Config()
  28. config.save(config_path)
  29. return config
  30. with open(config_path, "r", encoding="utf-8") as file:
  31. config_dict = yaml.safe_load(file)
  32. return Config(**config_dict)
  33. CFG = read_config(get_config_path())
  34. def main():
  35. print(CFG)
  36. CFG.save()
  37. if __name__ == "__main__":
  38. main()