# -*- coding: utf-8 -*-
"""
@author: yq
@time: 2024/11/1
@desc: 数据库配置类
"""
import json
import os

from commom import GeneralException
from enums import ResultCodesEnum


class DbConfigEntity():
    def __init__(self, host: str, port: int, user: str, passwd: str, db: str):
        self._host = host
        self._port = port
        self._user = user
        self._passwd = passwd
        self._db = db

    @property
    def host(self):
        return self._host

    @property
    def port(self):
        return self._port

    @property
    def user(self):
        return self._user

    @property
    def passwd(self):
        return self._passwd

    @property
    def db(self):
        return self._db

    @staticmethod
    def from_config(config_path: str):
        """
        从配置文件生成实体类
        """
        if os.path.exists(config_path):
            with open(config_path, mode="r", encoding="utf-8") as f:
                j = json.loads(f.read())
        else:
            raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")

        return DbConfigEntity(**j)


if __name__ == "__main__":
    pass
