configure.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "IMAP_USERNAME": "{0}",
  14. "IMAP_PASSWD": "{0}",
  15. "SMTP_HOST": "localhost",
  16. "SMTP_PORT": 25,
  17. "SMTP_SSL": False,
  18. "SMTP_START_SSL": False,
  19. "SMTP_USERNAME": "{0}",
  20. "SMTP_PASSWD": "{0}",
  21. "REDIS_HOST": "localhost",
  22. "REDIS_PORT": 6379,
  23. "REDIS_NAME": "localhost",
  24. "REDIS_PASSWD": "123456",
  25. "REDIS_DATABASE": 0,
  26. "LOG_HOME": "",
  27. "LOG_FORMAT": "[%(levelname)s]:%(name)s:%(asctime)s "
  28. "(%(filename)s:%(lineno)d %(funcName)s) "
  29. "%(process)d %(thread)d "
  30. "%(message)s",
  31. "LOG_LEVEL": logging.INFO,
  32. "LOG_STDERR": True,
  33. "DEBUG_PROFILE": False,
  34. "LOGO": "HuanMail.ico",
  35. }
  36. def configure(conf_file: str, encoding="utf-8"):
  37. """ 运行配置程序, 该函数需要在其他模块被执行前调用 """
  38. with open(conf_file, mode="r", encoding=encoding) as f:
  39. json_str = f.read()
  40. conf.update(json.loads(json_str))
  41. if type(conf["LOG_LEVEL"]) is str:
  42. conf["LOG_LEVEL"] = {"debug": logging.DEBUG,
  43. "info": logging.INFO,
  44. "warning": logging.WARNING,
  45. "error": logging.ERROR}.get(conf["LOG_LEVEL"])
  46. if len(conf["LOG_HOME"]) > 0:
  47. os.makedirs(conf["LOG_HOME"], exist_ok=True)
  48. env_dict = os.environ
  49. huan_mail_conf = env_dict.get("HUAN_MAIL_CONF")
  50. if huan_mail_conf is None:
  51. logging.info("Configure file ./etc/conf.json")
  52. configure("./etc/conf.json")
  53. else:
  54. logging.info(f"Configure file {huan_mail_conf}")
  55. configure(huan_mail_conf)