read_basic_ini.py 2.35 KB
Newer Older
L committed
1 2 3 4 5 6 7 8 9 10 11
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   ApiTest
# FileName:      read_basic_ini.py
# Author:       liu
# Datetime:     2024/9/7 上午9:30
# Description: 读取基础ini文件
# ---------------------------------------------------------------------------
import os
from configparser import ConfigParser

L committed
12
from ApiTest.report import log
L committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


class Read_basic_ini:
    def __init__(self):
        # config_data的路径
        self.main_path = os.path.dirname(os.path.dirname(__file__))
        data_path = os.path.join(self.main_path, 'config_data')
        # print(data_path)
        # ini 路径
        ini_path = os.path.join(data_path, 'basic_config.ini')
        # print(ini_path)
        # 初始化
        self.config = ConfigParser()
        # 读取文件
        self.config.read(ini_path, encoding='utf-8')

    def get_sql(self, key):
        try:
            # log.info(f"执行方法get_sql为:根据key获取sql节点下key对应文件的路径,形参key的传参为{key},")
            key_value = self.config.get('sql', key)
            return key_value
        except Exception as e:
            log.error('值错误,没有查询到值,请检查代码')
            raise e

    def get_host(self, key):
        try:
            key_value = self.config.get('host', key)
            return key_value
        except Exception as e:
            log.error('值错误,没有查询到值,请检查代码')
            raise e

    def get_report(self, key):
        try:
            # 返回路径
            report_path = os.path.join(self.main_path, 'report')
            key_path = os.path.join(report_path, self.config.get('report', key))
            # 判断是否存在,不存在则创建一个文件夹,无法创建则报错说路径不对
            if os.path.exists(key_path):
                pass
            else:
                os.makedirs(key_path)
        except Exception as e:
            # # log.error(f'{key_path}错误,请检查路径是否正确且为文件夹,错误为{e}')
            log.error(f"方法get_file_path执行失败,形参key传参为:{key},错误为:{e}")
            raise e


if __name__ == '__main__':
    read_ini = Read_basic_ini()
    print(read_ini.get_sql('host'))
    print(read_ini.get_host('bpm_host'))
    print(read_ini.get_report('log1'))