configure.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import json
  2. import logging
  3. import os
  4. from typing import Dict
  5. conf: Dict[str, any] = {
  6. "SECRET_KEY": "HuanMail-R-Salt",
  7. "WEBSITE_NAME": "HuanMail",
  8. "WEBSITE_TITLE": "HuanMail-在线邮件系统",
  9. "IMAP_HOST": "localhost",
  10. "IMAP_PORT": 143,
  11. "IMAP_SSL": False,
  12. "IMAP_START_SSL": False,
  13. "SMTP_HOST": "localhost",
  14. "SMTP_PORT": 25,
  15. "SMTP_SSL": False,
  16. "SMTP_START_SSL": False,
  17. "REDIS_HOST": "localhost",
  18. "REDIS_PORT": 6379,
  19. "REDIS_NAME": "localhost",
  20. "REDIS_PASSWD": "123456",
  21. "REDIS_DATABASE": 0,
  22. "LOG_HOME": "",
  23. "LOG_FORMAT": "[%(levelname)s]:%(name)s:%(asctime)s "
  24. "(%(filename)s:%(lineno)d %(funcName)s) "
  25. "%(process)d %(thread)d "
  26. "%(message)s",
  27. "LOG_LEVEL": logging.INFO,
  28. "LOG_STDERR": True,
  29. "DEBUG_PROFILE": False,
  30. "LOGO": "HuanMail.ico",
  31. }
  32. def configure(conf_file: str, encoding="utf-8"):
  33. """ 运行配置程序, 该函数需要在其他模块被执行前调用 """
  34. with open(conf_file, mode="r", encoding=encoding) as f:
  35. json_str = f.read()
  36. conf.update(json.loads(json_str))
  37. if type(conf["LOG_LEVEL"]) is str:
  38. conf["LOG_LEVEL"] = {"debug": logging.DEBUG,
  39. "info": logging.INFO,
  40. "warning": logging.WARNING,
  41. "error": logging.ERROR}.get(conf["LOG_LEVEL"])
  42. if len(conf["LOG_HOME"]) > 0:
  43. os.makedirs(conf["LOG_HOME"], exist_ok=True)