settings.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. from dotenv import load_dotenv
  3. load_dotenv()
  4. from pathlib import Path
  5. from typing import Optional
  6. from pydantic import BaseModel
  7. import yaml
  8. WORK_DIR = Path(__file__).resolve().parent.parent
  9. OUTPUT_DIR=WORK_DIR / "output"
  10. DB_URL = os.environ.get('DB_URL') or "postgresql+psycopg2://user:password@sv-v2.lan:5435/copywriting_production"
  11. LOG_LEVEL = 'DEBUG'
  12. LOG_DIR = Path(__file__).resolve().parent.parent/'output/logs'
  13. CONFIG_DIR = Path(__file__).resolve().parent.parent/'config'
  14. BROWSER_CONFIG_DIR = CONFIG_DIR / 'dp_conf'
  15. class Config(BaseModel):
  16. storage: str = "local"
  17. s3_access_key: Optional[str] = os.environ.get("S3_ACCESS_KEY", 'bh9LbfsPHRJgQ44wXIlv')
  18. s3_secret_key: Optional[str] = os.environ.get("S3_SECRET_KEY", 'N744RZ60T1b4zlcWG2MROCzjEE2mPTdNQCc7Pk3M')
  19. s3_endpoint: Optional[str] = os.environ.get("S3_ENDPOINT", 'http://vs1.lan:9002')
  20. s3_prefix: Optional[str] = 'amazone/copywriting_production'
  21. chrome_config_ini: Optional[str] = r'G:\code\amazone\copywriting_production\config\dp_conf\9321.ini'
  22. redis_url: Optional[str] = os.environ.get("REDIS_URL", 'redis://localhost:6379/0')
  23. version: Optional[str] = "0.0.1-alpha"
  24. def save(self, config_path: Path = None):
  25. config_path = config_path or get_config_path()
  26. with open(config_path, "w", encoding="utf-8") as file:
  27. yaml.dump(self.model_dump(), file)
  28. return self
  29. def get_config_path():
  30. return os.environ.get('CONFIG_PATH',CONFIG_DIR / "config.yaml")
  31. def read_config(config_path: Path):
  32. if isinstance(config_path, str):
  33. config_path = Path(config_path)
  34. if not config_path.exists():
  35. config = Config()
  36. config.save(config_path)
  37. return config
  38. with open(config_path, "r", encoding="utf-8") as file:
  39. config_dict = yaml.safe_load(file)
  40. return Config(**config_dict)
  41. CFG = read_config(get_config_path())
  42. def main():
  43. print(CFG)
  44. CFG.save()
  45. if __name__ == "__main__":
  46. main()