settings.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. MONGO_URL = os.environ.get('MONGO_URL', 'mongodb://sv-v2:27017')
  12. MONGO_DB_NAME = os.environ.get('MONGO_DB_NAME', 'amazone')
  13. LOG_LEVEL = 'DEBUG'
  14. LOG_DIR = Path(__file__).resolve().parent.parent/'output/logs'
  15. CONFIG_DIR = Path(__file__).resolve().parent.parent/'config'
  16. BROWSER_CONFIG_DIR = CONFIG_DIR / 'dp_conf'
  17. TEMP_PAGE_DIR = OUTPUT_DIR / 'page' / 'temp'
  18. TEMP_PAGE_DIR.mkdir(parents=True, exist_ok=True)
  19. class Config(BaseModel):
  20. storage: str = "local"
  21. s3_access_key: Optional[str] = os.environ.get("S3_ACCESS_KEY", 'bh9LbfsPHRJgQ44wXIlv')
  22. s3_secret_key: Optional[str] = os.environ.get("S3_SECRET_KEY", 'N744RZ60T1b4zlcWG2MROCzjEE2mPTdNQCc7Pk3M')
  23. s3_endpoint: Optional[str] = os.environ.get("S3_ENDPOINT", 'http://s3.vs1.lan')
  24. s3_prefix: Optional[str] = 's3://public/amazone/copywriting_production'
  25. chrome_config_ini: Optional[str] = r'G:\code\amazone\copywriting_production\config\dp_conf\9321.ini'
  26. redis_url: Optional[str] = os.environ.get("REDIS_URL", 'redis://localhost:6379/0')
  27. version: Optional[str] = "0.0.1-alpha"
  28. def save(self, config_path: Path = None):
  29. config_path = config_path or get_config_path()
  30. with open(config_path, "w", encoding="utf-8") as file:
  31. yaml.dump(self.model_dump(), file)
  32. return self
  33. def get_config_path():
  34. return os.environ.get('CONFIG_PATH',CONFIG_DIR / "config.yaml")
  35. def read_config(config_path: Path):
  36. if isinstance(config_path, str):
  37. config_path = Path(config_path)
  38. if not config_path.exists():
  39. config = Config()
  40. config.save(config_path)
  41. return config
  42. with open(config_path, "r", encoding="utf-8") as file:
  43. config_dict = yaml.safe_load(file)
  44. return Config(**config_dict)
  45. CFG = read_config(get_config_path())
  46. def main():
  47. print(CFG)
  48. CFG.save()
  49. if __name__ == "__main__":
  50. main()