1
0

configure.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import json
  2. import logging
  3. import os
  4. from typing import Dict
  5. conf: Dict[str, any] = {
  6. "DEBUG_PROFILE": False,
  7. "SMTP_JSON": "smtp.json",
  8. "IMAP_JSON": "imap.json",
  9. "SECRET_KEY": "HuanMail-R-Salt",
  10. "LOG_HOME": "",
  11. "LOG_FORMAT": "[%(levelname)s]:%(name)s:%(asctime)s "
  12. "(%(filename)s:%(lineno)d %(funcName)s) "
  13. "%(process)d %(thread)d "
  14. "%(message)s",
  15. "LOG_LEVEL": logging.INFO,
  16. "LOG_STDERR": True,
  17. "LOGO": "HuanMail.ico",
  18. }
  19. def configure(conf_file: str, encoding="utf-8"):
  20. """ 运行配置程序, 该函数需要在其他模块被执行前调用 """
  21. with open(conf_file, mode="r", encoding=encoding) as f:
  22. json_str = f.read()
  23. conf.update(json.loads(json_str))
  24. if type(conf["LOG_LEVEL"]) is str:
  25. conf["LOG_LEVEL"] = {"debug": logging.DEBUG,
  26. "info": logging.INFO,
  27. "warning": logging.WARNING,
  28. "error": logging.ERROR}.get(conf["LOG_LEVEL"])
  29. if len(conf["LOG_HOME"]) > 0:
  30. os.makedirs(conf["LOG_HOME"], exist_ok=True)