# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test65
# FileName:      read_ini.py
# Author:       lao_zhao
# Datetime:     2025/3/12 11:16
# Description:
# 
# ---------------------------------------------------------------------------
import configparser
import os

from APIAutoTest_v3 import log_decorator


class ReadUserIni:

    @log_decorator
    def __init__(self, username):
        """获取ini文件的路径，使用Configparser对象读取ini文件"""
        # 获取data_config的目录路径
        data_config = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data_config")
        # 获取用户存放数据文件的目录路径，需要传入用户名
        self.user_data_dir_path = os.path.join(data_config, username)
        # 获取用户的ini配置文件的路径
        user_ini_path = os.path.join(self.user_data_dir_path, "user_config.ini")
        # 创建Configparser对象
        self.conf = configparser.ConfigParser()
        # 读取用户ini配置文件
        self.conf.read(user_ini_path, encoding="utf-8")

    @log_decorator
    def get_file_path(self, key):
        """根据key，获取file节点下文件的路径"""
        value = self.conf.get("file", key)
        return os.path.join(self.user_data_dir_path, value)

    @log_decorator
    def get_table_name(self, key):
        """根据key，获取table节点下工作表的名称"""
        return self.conf.get("table", key)


if __name__ == '__main__':
    ini = ReadUserIni("张三")
    ini.get_table_name("tableName")
    ini.get_file_path("case")