# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test63
# FileName:      read_ini.py
# Author:       lao_zhao
# Datetime:     2024/11/5 14:09
# Description:
#
# ---------------------------------------------------------------------------
import configparser
import os

from APIAutoTest_v3_1 import log, logor


class ReadUserIni:
    def __init__(self, user):
        """读取ini文件, 需要指明读取哪个用户的ini配置文件"""
        # 获取数据配置层的路径
        data_config = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data_config")
        # 获取用户存放数据的目录路径
        self.user_data_config = os.path.join(data_config, user)
        # 获取用户的ini文件路径
        ini_path = os.path.join(self.user_data_config, "config.ini")

        self.conf = configparser.ConfigParser()
        res = self.conf.read(ini_path, encoding="utf-8")
        if not res:
            log.error(f"读取ini文件失败，请检查用户名是否传入正确，用户名为：{user}, 对应的ini文件路径为：{ini_path}")
            raise ValueError("用户名错误")

    @logor
    def get_file_path(self, key):
        """根据key获取file节点下文件的路径"""
        # log.info(f"执行get_file_path方法，功能为：根据key获取file节点下文件的路径， 传入的参数为：{key}")
        try:
            file_name = self.conf.get("file", key)
        except Exception as e:
            # log.info(f"执行get_file_path方法，功能为：根据key获取file节点下文件的路径， 传入的参数为：{key}"
            #          f"发生错误，错误的类型为：{type(e)}， 错误的描述为：{e}")
            raise e
        else:
            return os.path.join(self.user_data_config, file_name)

    @logor
    def get_table_name(self, key):
        """根据key获取table节点下工作表的名称"""
        try:
            return self.conf.get("table", key)
        except Exception as e:
            raise e




if __name__ == '__main__':
    ini = ReadUserIni("zs_data")
    print(ini.get_table_name("name"))
