# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test62
# FileName:      read_ini.py
# Author:       lao_zhao
# Datetime:     2024/9/4 14:17
# Description:
# 
# ---------------------------------------------------------------------------
import configparser
import os
from apiAutoTest_v3 import log


class ReadUserIni:
    def __init__(self, user_dir_name):
        """获取ini文件的路径，并读取ini"""
        # 获取数据配置层的目录路径
        data_config = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data_config")
        # 获取存放用户数据的目录路径
        self.user_dir = os.path.join(data_config, user_dir_name)
        # 拼接用户的ini文件路径
        ini_path = os.path.join(self.user_dir, "config.ini")
        self.conf = configparser.ConfigParser()
        self.conf.read(ini_path, encoding="utf-8")

    def get_file_path(self, key):
        """根据key获取file节点下key对应文件的路径"""
        try:
            log.info(f"执行方法get_file_path为：根据key获取file节点下key对应文件的路径，形参key的传参为{key},")
            file_name = self.conf.get("file", key)
        except Exception as e:
            log.error(f"方法get_file_path执行失败，形参key传参为：{key},错误为：{e}")
            raise e
        else:
            return os.path.join(self.user_dir, file_name)

    def get_table_name(self, key):
        """根据key获取table节点下key对应的工作表名"""
        try:
            return self.conf.get("table", key)
        except Exception as e:
            log.error(f"方法get_table_name执行失败，形参key传参为：{key},错误为：{e}")
            raise e


if __name__ == '__main__':
    ini = ReadUserIni("demo")
    # D:\Project\PythonDoc\test62\test62\apiAutoTest_v3\data_config\demo\apiAutoTest.xlsx
    # D:\Project\PythonDoc\test62\test62\apiAutoTest_v3\data_config\demo\apiAutoTest.xlsx
    print(ini.get_file_path("excel"))