__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import json
  2. import logging
  3. import os
  4. conf = dict()
  5. def configure(conf_file: str, encoding="utf-8"):
  6. """ 运行配置程序, 该函数需要在其他模块被执行前调用 """
  7. with open(conf_file, mode="r", encoding=encoding) as f:
  8. json_str = f.read()
  9. _conf: dict = json.loads(json_str)
  10. _mysql = _conf["mysql"]
  11. conf["mysql_url"] = str(_mysql["url"])
  12. conf["mysql_name"] = str(_mysql["name"])
  13. conf["mysql_passwd"] = str(_mysql["passwd"])
  14. conf["mysql_port"] = int(_mysql.get("port", 3306))
  15. _email = _conf["email"]
  16. conf["email_server"] = str(_email["server"])
  17. conf["email_port"] = int(_email["port"])
  18. conf["email_tls"] = bool(_email.get("tls", False))
  19. conf["email_ssl"] = bool(_email.get("ssl", False))
  20. conf["email_name"] = str(_email["name"])
  21. conf["email_passwd"] = str(_email["passwd"])
  22. conf["email_prefix"] = str(_email.get("prefix", "[HBlog]"))
  23. conf["email_sender"] = str(_email["sender"])
  24. conf["secret-key"] = str(_conf.get("secret-key", "HBlog-R-Salt"))
  25. conf["server-name"] = _conf.get("server-name")
  26. introduce = _conf["info"]["introduce"]
  27. introduce_list = []
  28. for i in introduce:
  29. describe: str = introduce[i]
  30. describe = " ".join([f"<p>{i}</p>" for i in describe.split('\n')])
  31. introduce_list.append((i, describe))
  32. conf["describe-link"] = _conf["info"]["link"]
  33. conf["describe-info"] = introduce_list
  34. conf["blog-name"] = _conf["info"]["blog-name"]
  35. conf["blog-describe"] = _conf["info"]["blog-describe"]
  36. conf["about-me-name"] = _conf["info"]["about-me"]["name"]
  37. conf["about-me-describe"] = _conf["info"]["about-me"]["describe"]
  38. conf["about-me-project"] = _conf["info"]["project"]
  39. conf["about-me-skill"] = _conf["info"]["skill"]
  40. conf["about-me-read"] = _conf["info"]["read"]
  41. conf["foot-info"] = f'{_conf["info"]["foot-info"]} Power by HBlog'
  42. aliyun = _conf.get("aliyun")
  43. if aliyun is None:
  44. conf["aliyun"] = False
  45. else:
  46. conf["aliyun"] = True
  47. conf["aliyun-key"] = aliyun["Key"]
  48. conf["aliyun-secret"] = aliyun["Secret"]
  49. conf["aliyun-bucket-endpoint"] = aliyun["Bucket-Endpoint"]
  50. conf["aliyun-bucket-name"] = aliyun["Bucket-Name"]
  51. log = _conf.get("log")
  52. if log is None:
  53. conf["log-home"] = None
  54. conf["log-format"] = ("[%(levelname)s]:%(name)s:%(asctime)s "
  55. "(%(filename)s:%(lineno)d %(funcName)s) "
  56. "%(process)d %(thread)d "
  57. "%(message)s")
  58. conf["log-level"] = logging.INFO
  59. else:
  60. conf["log-home"] = log.get("home")
  61. if conf["log-home"]:
  62. os.makedirs(conf["log-home"], exist_ok=True)
  63. conf["log-level"] = {"debug": logging.DEBUG,
  64. "info": logging.INFO,
  65. "warning": logging.WARNING,
  66. "error": logging.ERROR}.get(log.get("level", "info"))
  67. conf["log-format"] = log.get("format", ("[%(levelname)s]:%(name)s:%(asctime)s "
  68. "(%(filename)s:%(lineno)d %(funcName)s) "
  69. "%(process)d %(thread)d "
  70. "%(message)s"))