readConf.py 1.67 KB
# -*- coding: utf-8 -*-

# @Time    : 2021/7/16 10:36
# @Author  : Ljq
# @File    : readConf.py
# @Software: PyCharm

"""
配置文件读取
"""

import configparser
import os

class readConfig(object):
    ROBOT_LIBRARY_SCOPE = 'TEST CASE'
    ROBOT_LIBRARY_VERSION = '0.1'

    def __init__(self):
        # 按市场读取配置文件数据
        self.conf = configparser.ConfigParser()
        self.evn_name = os.name
        self.file_name = r'test_config_hg.conf'
        self.relative_path = r'/config/marketConfig/'
        self.file_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "../../")) + self.relative_path + self.file_name
        print(self.file_path)
        self.conf.read(self.file_path,encoding="utf-8")

    def returnSections(self):
        # 读取所有的options选项
        sections = self.conf.sections()
        print(sections)
        return sections

    def returnOptions(self,options):
        # 通过options读取所有items
        options = self.conf.options(options)
        print(options)
        return options

    def returnOptionsInfo(self,options):
        # 通过options读取相关的items和value
        items = self.conf.items(options)
        print(items)

    def returnOptionsItems(self,options,items):
        # 通过options和items的组合关系读取对应的value
        value = self.conf.get(options,items)
        print(value)
        return value

    def ReturnFilePath(self):
        print(self.file_path)
        return self.file_path

# rc = readConfig()
# rc.returnSections()
# rc.returnOptions("loginInfo")
# rc.returnOptionsInfo("loginInfo")
# rc.returnOptionsItems("loginInfo","username")
# rc.ReturnFilePath()