db_config_entity.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: yq
  4. @time: 2024/11/1
  5. @desc: 数据库配置类
  6. """
  7. import json
  8. import os
  9. from commom import GeneralException
  10. from enums import ResultCodesEnum
  11. class DbConfigEntity():
  12. def __init__(self, host: str, port: int, user: str, passwd: str, db: str):
  13. self._host = host
  14. self._port = port
  15. self._user = user
  16. self._passwd = passwd
  17. self._db = db
  18. @property
  19. def host(self):
  20. return self._host
  21. @property
  22. def port(self):
  23. return self._port
  24. @property
  25. def user(self):
  26. return self._user
  27. @property
  28. def passwd(self):
  29. return self._passwd
  30. @property
  31. def db(self):
  32. return self._db
  33. @staticmethod
  34. def from_config(config_path: str):
  35. """
  36. 从配置文件生成实体类
  37. """
  38. if os.path.exists(config_path):
  39. with open(config_path, mode="r", encoding="utf-8") as f:
  40. j = json.loads(f.read())
  41. else:
  42. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")
  43. return DbConfigEntity(**j)
  44. if __name__ == "__main__":
  45. pass