| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import os
- from dotenv import load_dotenv
- load_dotenv()
- from pathlib import Path
- from typing import Optional
- from pydantic import BaseModel
- import yaml
- WORK_DIR = Path(__file__).resolve().parent.parent
- OUTPUT_DIR=WORK_DIR / "output"
- DB_URL = os.environ.get('DB_URL') or "postgresql+psycopg2://user:password@sv-v2.lan:5435/copywriting_production"
- MONGO_URL = os.environ.get('MONGO_URL', 'mongodb://sv-v2:27017')
- MONGO_DB_NAME = os.environ.get('MONGO_DB_NAME', 'amazone')
- LOG_LEVEL = 'DEBUG'
- LOG_DIR = Path(__file__).resolve().parent.parent/'output/logs'
- CONFIG_DIR = Path(__file__).resolve().parent.parent/'config'
- BROWSER_CONFIG_DIR = CONFIG_DIR / 'dp_conf'
- TEMP_PAGE_DIR = OUTPUT_DIR / 'page' / 'temp'
- TEMP_PAGE_DIR.mkdir(parents=True, exist_ok=True)
- class Config(BaseModel):
- storage: str = "local"
- s3_access_key: Optional[str] = os.environ.get("S3_ACCESS_KEY", 'bh9LbfsPHRJgQ44wXIlv')
- s3_secret_key: Optional[str] = os.environ.get("S3_SECRET_KEY", 'N744RZ60T1b4zlcWG2MROCzjEE2mPTdNQCc7Pk3M')
- s3_endpoint: Optional[str] = os.environ.get("S3_ENDPOINT", 'http://s3.vs1.lan')
- s3_prefix: Optional[str] = 's3://public/amazone/copywriting_production'
- chrome_config_ini: Optional[str] = r'G:\code\amazone\copywriting_production\config\dp_conf\9321.ini'
- redis_url: Optional[str] = os.environ.get("REDIS_URL", 'redis://localhost:6379/0')
- version: Optional[str] = "0.0.1-alpha"
- def save(self, config_path: Path = None):
- config_path = config_path or get_config_path()
- with open(config_path, "w", encoding="utf-8") as file:
- yaml.dump(self.model_dump(), file)
- return self
-
- def get_config_path():
- return os.environ.get('CONFIG_PATH',CONFIG_DIR / "config.yaml")
- def read_config(config_path: Path):
- if isinstance(config_path, str):
- config_path = Path(config_path)
- if not config_path.exists():
- config = Config()
- config.save(config_path)
- return config
- with open(config_path, "r", encoding="utf-8") as file:
- config_dict = yaml.safe_load(file)
- return Config(**config_dict)
- CFG = read_config(get_config_path())
- def main():
- print(CFG)
- CFG.save()
- if __name__ == "__main__":
- main()
|